Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 21 additions & 20 deletions command.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,10 @@ import (
"strings"
"unicode/utf8"

"go.followtheprocess.codes/cli/internal/colour"
"go.followtheprocess.codes/cli/internal/flag"
"go.followtheprocess.codes/cli/internal/table"
"go.followtheprocess.codes/cli/internal/style"

"go.followtheprocess.codes/hue/tabwriter"
)

const (
Expand Down Expand Up @@ -518,9 +519,9 @@ func defaultHelp(cmd *Command) error {
s.WriteString("\n\n")
}

s.WriteString(colour.Title("Usage"))
s.WriteString(style.Title.Text("Usage"))
s.WriteString(": ")
s.WriteString(colour.Bold(cmd.name))
s.WriteString(style.Bold.Text(cmd.name))

if len(cmd.subcommands) == 0 {
// We don't have any subcommands so usage will be:
Expand Down Expand Up @@ -570,7 +571,7 @@ func defaultHelp(cmd *Command) error {
s.WriteString("\n\n")
}

s.WriteString(colour.Title("Options"))
s.WriteString(style.Title.Text("Options"))
s.WriteString(":\n\n")
s.WriteString(usage)

Expand Down Expand Up @@ -609,22 +610,22 @@ func writePositionalArgs(cmd *Command, s *strings.Builder) {
// text string builder.
func writeArgumentsSection(cmd *Command, s *strings.Builder) error {
s.WriteString("\n\n")
s.WriteString(colour.Title("Arguments"))
s.WriteString(style.Title.Text("Arguments"))
s.WriteString(":\n")
tab := table.New(s)
tw := tabwriter.NewWriter(s, style.MinWidth, style.TabWidth, style.Padding, style.PadChar, style.Flags)

for _, arg := range cmd.positionalArgs {
switch arg.defaultValue {
case requiredArgMarker:
tab.Row(" %s\t%s\t[required]\n", colour.Bold(arg.name), arg.description)
fmt.Fprintf(tw, " %s\t%s\t[required]\n", style.Bold.Text(arg.name), arg.description)
case "":
tab.Row(" %s\t%s\t[default %q]\n", colour.Bold(arg.name), arg.description, arg.defaultValue)
fmt.Fprintf(tw, " %s\t%s\t[default %q]\n", style.Bold.Text(arg.name), arg.description, arg.defaultValue)
default:
tab.Row(" %s\t%s\t[default %s]\n", colour.Bold(arg.name), arg.description, arg.defaultValue)
fmt.Fprintf(tw, " %s\t%s\t[default %s]\n", style.Bold.Text(arg.name), arg.description, arg.defaultValue)
}
}

if err := tab.Flush(); err != nil {
if err := tw.Flush(); err != nil {
return fmt.Errorf("could not format arguments: %w", err)
}

Expand All @@ -641,7 +642,7 @@ func writeExamples(cmd *Command, s *strings.Builder) {
s.WriteString("\n\n")
}

s.WriteString(colour.Title("Examples"))
s.WriteString(style.Title.Text("Examples"))
s.WriteByte(':')
s.WriteString("\n\n")

Expand Down Expand Up @@ -672,16 +673,16 @@ func writeSubcommands(cmd *Command, s *strings.Builder) error {
s.WriteString("\n\n")
}

s.WriteString(colour.Title("Commands"))
s.WriteString(style.Title.Text("Commands"))
s.WriteByte(':')
s.WriteString("\n\n")

tab := table.New(s)
tw := tabwriter.NewWriter(s, style.MinWidth, style.TabWidth, style.Padding, style.PadChar, style.Flags)
for _, subcommand := range cmd.subcommands {
tab.Row(" %s\t%s\n", colour.Bold(subcommand.name), subcommand.short)
fmt.Fprintf(tw, " %s\t%s\n", style.Bold.Text(subcommand.name), subcommand.short)
}

if err := tab.Flush(); err != nil {
if err := tw.Flush(); err != nil {
return fmt.Errorf("could not format subcommands: %w", err)
}

Expand All @@ -707,22 +708,22 @@ func defaultVersion(cmd *Command) error {

s := &strings.Builder{}
s.Grow(versionBufferSize)
s.WriteString(colour.Title(cmd.name))
s.WriteString(style.Title.Text(cmd.name))
s.WriteString("\n\n")
s.WriteString(colour.Bold("Version:"))
s.WriteString(style.Bold.Text("Version:"))
s.WriteString(" ")
s.WriteString(cmd.version)
s.WriteString("\n")

if cmd.commit != "" {
s.WriteString(colour.Bold("Commit:"))
s.WriteString(style.Bold.Text("Commit:"))
s.WriteString(" ")
s.WriteString(cmd.commit)
s.WriteString("\n")
}

if cmd.buildDate != "" {
s.WriteString(colour.Bold("BuildDate:"))
s.WriteString(style.Bold.Text("BuildDate:"))
s.WriteString(" ")
s.WriteString(cmd.buildDate)
s.WriteString("\n")
Expand Down
Binary file modified docs/img/namedargs.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified docs/img/quickstart.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified docs/img/subcommands.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@ ignore (
)

require (
go.followtheprocess.codes/hue v1.0.0
go.followtheprocess.codes/snapshot v0.6.1
go.followtheprocess.codes/test v0.23.1
)

require (
go.followtheprocess.codes/hue v0.7.0 // indirect
golang.org/x/sys v0.36.0 // indirect
golang.org/x/term v0.35.0 // indirect
)
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
go.followtheprocess.codes/hue v0.7.0 h1:6HaZTGc4NYVnqqjYZTTBcVYRKb8MyfPe5yBHJiBfekg=
go.followtheprocess.codes/hue v0.7.0/go.mod h1:gSn5xK6KJapih+eFgQk3woo1qg3/rx9XSrAanUIuDr8=
go.followtheprocess.codes/hue v1.0.0 h1:0fYXAGR1o+w7Vja+Q+iVtqeEP3/CE6ET/pniyl8e9yo=
go.followtheprocess.codes/hue v1.0.0/go.mod h1:gSn5xK6KJapih+eFgQk3woo1qg3/rx9XSrAanUIuDr8=
go.followtheprocess.codes/snapshot v0.6.1 h1:cZkQtEjL21BSrHn98Lm0S4yTzcOje/K60Iog/u/A5tM=
go.followtheprocess.codes/snapshot v0.6.1/go.mod h1:CM2E92Ah/j0XL4Z2UyOl7GlSuD0ZLLl8rJCpFylKcIg=
go.followtheprocess.codes/test v0.23.1 h1:VoucCC8qKb6tKnBOCRZ7Ln2Ex1oV+HMXHdZyJ6DURB8=
Expand Down
77 changes: 0 additions & 77 deletions internal/colour/colour.go

This file was deleted.

15 changes: 8 additions & 7 deletions internal/flag/set.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ import (
"slices"
"strings"

"go.followtheprocess.codes/cli/internal/colour"
"go.followtheprocess.codes/cli/internal/table"
"go.followtheprocess.codes/cli/internal/style"
"go.followtheprocess.codes/hue/tabwriter"
)

// usageBufferSize is sufficient to hold most commands flag usage text.
Expand Down Expand Up @@ -199,7 +199,7 @@ func (s *Set) Usage() (string, error) {

slices.Sort(names)

tab := table.New(buf)
tw := tabwriter.NewWriter(buf, style.MinWidth, style.TabWidth, style.Padding, style.PadChar, style.Flags)

for _, name := range names {
flag := s.flags[name]
Expand All @@ -214,16 +214,17 @@ func (s *Set) Usage() (string, error) {
shorthand = "N/A"
}

tab.Row(
fmt.Fprintf(
tw,
" %s\t--%s\t%s\t%s\n",
colour.Bold(shorthand),
colour.Bold(name),
style.Bold.Text(shorthand),
style.Bold.Text(name),
flag.Type(),
flag.Usage(),
)
}

if err := tab.Flush(); err != nil {
if err := tw.Flush(); err != nil {
return "", fmt.Errorf("could not format flags: %w", err)
}

Expand Down
3 changes: 0 additions & 3 deletions internal/flag/set_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"slices"
"testing"

"go.followtheprocess.codes/cli/internal/colour"
"go.followtheprocess.codes/cli/internal/flag"
"go.followtheprocess.codes/snapshot"
"go.followtheprocess.codes/test"
Expand Down Expand Up @@ -1304,8 +1303,6 @@ func TestUsage(t *testing.T) {
snap := snapshot.New(t, snapshot.Update(*update))
set := tt.newSet(t)

colour.Disable.Store(true) // For testing

got, err := set.Usage()
test.Ok(t, err)

Expand Down
30 changes: 30 additions & 0 deletions internal/style/style.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// Package style simply provides a uniform terminal printing style via [hue] for use across
// the library.
//
// [hue]: https://github.com/FollowTheProcess/hue
package style

import "go.followtheprocess.codes/hue"

const (
// Title is the style for titles of help text sections like arguments or commands.
Title = hue.Bold | hue.White | hue.Underline

// Bold is simply plain bold text.
Bold = hue.Bold

// MinWidth is the minimum cell width for hue's colour-enabled tabwriter.
MinWidth = 1

// TabWidth is the width of tabs in spaces for tabwriter.
TabWidth = 8

// Padding is the number of PadChars to pad table cells with.
Padding = 2

// PadChar is the character with which to pad table cells.
PadChar = ' '

// Flags is the tabwriter config flags.
Flags = 0
)
42 changes: 0 additions & 42 deletions internal/table/table.go

This file was deleted.

37 changes: 0 additions & 37 deletions internal/table/table_test.go

This file was deleted.

3 changes: 0 additions & 3 deletions internal/table/testdata/snapshots/TestTable.snap.txt

This file was deleted.

Loading
Loading