Skip to content

Commit f12776b

Browse files
Use hue for colouring and style (#171)
1 parent 0d64dbe commit f12776b

14 files changed

Lines changed: 64 additions & 194 deletions

File tree

command.go

Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,10 @@ import (
1010
"strings"
1111
"unicode/utf8"
1212

13-
"go.followtheprocess.codes/cli/internal/colour"
1413
"go.followtheprocess.codes/cli/internal/flag"
15-
"go.followtheprocess.codes/cli/internal/table"
14+
"go.followtheprocess.codes/cli/internal/style"
15+
16+
"go.followtheprocess.codes/hue/tabwriter"
1617
)
1718

1819
const (
@@ -518,9 +519,9 @@ func defaultHelp(cmd *Command) error {
518519
s.WriteString("\n\n")
519520
}
520521

521-
s.WriteString(colour.Title("Usage"))
522+
s.WriteString(style.Title.Text("Usage"))
522523
s.WriteString(": ")
523-
s.WriteString(colour.Bold(cmd.name))
524+
s.WriteString(style.Bold.Text(cmd.name))
524525

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

573-
s.WriteString(colour.Title("Options"))
574+
s.WriteString(style.Title.Text("Options"))
574575
s.WriteString(":\n\n")
575576
s.WriteString(usage)
576577

@@ -609,22 +610,22 @@ func writePositionalArgs(cmd *Command, s *strings.Builder) {
609610
// text string builder.
610611
func writeArgumentsSection(cmd *Command, s *strings.Builder) error {
611612
s.WriteString("\n\n")
612-
s.WriteString(colour.Title("Arguments"))
613+
s.WriteString(style.Title.Text("Arguments"))
613614
s.WriteString(":\n")
614-
tab := table.New(s)
615+
tw := tabwriter.NewWriter(s, style.MinWidth, style.TabWidth, style.Padding, style.PadChar, style.Flags)
615616

616617
for _, arg := range cmd.positionalArgs {
617618
switch arg.defaultValue {
618619
case requiredArgMarker:
619-
tab.Row(" %s\t%s\t[required]\n", colour.Bold(arg.name), arg.description)
620+
fmt.Fprintf(tw, " %s\t%s\t[required]\n", style.Bold.Text(arg.name), arg.description)
620621
case "":
621-
tab.Row(" %s\t%s\t[default %q]\n", colour.Bold(arg.name), arg.description, arg.defaultValue)
622+
fmt.Fprintf(tw, " %s\t%s\t[default %q]\n", style.Bold.Text(arg.name), arg.description, arg.defaultValue)
622623
default:
623-
tab.Row(" %s\t%s\t[default %s]\n", colour.Bold(arg.name), arg.description, arg.defaultValue)
624+
fmt.Fprintf(tw, " %s\t%s\t[default %s]\n", style.Bold.Text(arg.name), arg.description, arg.defaultValue)
624625
}
625626
}
626627

627-
if err := tab.Flush(); err != nil {
628+
if err := tw.Flush(); err != nil {
628629
return fmt.Errorf("could not format arguments: %w", err)
629630
}
630631

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

644-
s.WriteString(colour.Title("Examples"))
645+
s.WriteString(style.Title.Text("Examples"))
645646
s.WriteByte(':')
646647
s.WriteString("\n\n")
647648

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

675-
s.WriteString(colour.Title("Commands"))
676+
s.WriteString(style.Title.Text("Commands"))
676677
s.WriteByte(':')
677678
s.WriteString("\n\n")
678679

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

684-
if err := tab.Flush(); err != nil {
685+
if err := tw.Flush(); err != nil {
685686
return fmt.Errorf("could not format subcommands: %w", err)
686687
}
687688

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

708709
s := &strings.Builder{}
709710
s.Grow(versionBufferSize)
710-
s.WriteString(colour.Title(cmd.name))
711+
s.WriteString(style.Title.Text(cmd.name))
711712
s.WriteString("\n\n")
712-
s.WriteString(colour.Bold("Version:"))
713+
s.WriteString(style.Bold.Text("Version:"))
713714
s.WriteString(" ")
714715
s.WriteString(cmd.version)
715716
s.WriteString("\n")
716717

717718
if cmd.commit != "" {
718-
s.WriteString(colour.Bold("Commit:"))
719+
s.WriteString(style.Bold.Text("Commit:"))
719720
s.WriteString(" ")
720721
s.WriteString(cmd.commit)
721722
s.WriteString("\n")
722723
}
723724

724725
if cmd.buildDate != "" {
725-
s.WriteString(colour.Bold("BuildDate:"))
726+
s.WriteString(style.Bold.Text("BuildDate:"))
726727
s.WriteString(" ")
727728
s.WriteString(cmd.buildDate)
728729
s.WriteString("\n")

docs/img/namedargs.gif

-805 Bytes
Loading

docs/img/quickstart.gif

2.7 KB
Loading

docs/img/subcommands.gif

-727 Bytes
Loading

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,12 @@ ignore (
88
)
99

1010
require (
11+
go.followtheprocess.codes/hue v1.0.0
1112
go.followtheprocess.codes/snapshot v0.6.1
1213
go.followtheprocess.codes/test v0.23.1
1314
)
1415

1516
require (
16-
go.followtheprocess.codes/hue v0.7.0 // indirect
1717
golang.org/x/sys v0.36.0 // indirect
1818
golang.org/x/term v0.35.0 // indirect
1919
)

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
go.followtheprocess.codes/hue v0.7.0 h1:6HaZTGc4NYVnqqjYZTTBcVYRKb8MyfPe5yBHJiBfekg=
2-
go.followtheprocess.codes/hue v0.7.0/go.mod h1:gSn5xK6KJapih+eFgQk3woo1qg3/rx9XSrAanUIuDr8=
1+
go.followtheprocess.codes/hue v1.0.0 h1:0fYXAGR1o+w7Vja+Q+iVtqeEP3/CE6ET/pniyl8e9yo=
2+
go.followtheprocess.codes/hue v1.0.0/go.mod h1:gSn5xK6KJapih+eFgQk3woo1qg3/rx9XSrAanUIuDr8=
33
go.followtheprocess.codes/snapshot v0.6.1 h1:cZkQtEjL21BSrHn98Lm0S4yTzcOje/K60Iog/u/A5tM=
44
go.followtheprocess.codes/snapshot v0.6.1/go.mod h1:CM2E92Ah/j0XL4Z2UyOl7GlSuD0ZLLl8rJCpFylKcIg=
55
go.followtheprocess.codes/test v0.23.1 h1:VoucCC8qKb6tKnBOCRZ7Ln2Ex1oV+HMXHdZyJ6DURB8=

internal/colour/colour.go

Lines changed: 0 additions & 77 deletions
This file was deleted.

internal/flag/set.go

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ import (
77
"slices"
88
"strings"
99

10-
"go.followtheprocess.codes/cli/internal/colour"
11-
"go.followtheprocess.codes/cli/internal/table"
10+
"go.followtheprocess.codes/cli/internal/style"
11+
"go.followtheprocess.codes/hue/tabwriter"
1212
)
1313

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

200200
slices.Sort(names)
201201

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

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

217-
tab.Row(
217+
fmt.Fprintf(
218+
tw,
218219
" %s\t--%s\t%s\t%s\n",
219-
colour.Bold(shorthand),
220-
colour.Bold(name),
220+
style.Bold.Text(shorthand),
221+
style.Bold.Text(name),
221222
flag.Type(),
222223
flag.Usage(),
223224
)
224225
}
225226

226-
if err := tab.Flush(); err != nil {
227+
if err := tw.Flush(); err != nil {
227228
return "", fmt.Errorf("could not format flags: %w", err)
228229
}
229230

internal/flag/set_test.go

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import (
66
"slices"
77
"testing"
88

9-
"go.followtheprocess.codes/cli/internal/colour"
109
"go.followtheprocess.codes/cli/internal/flag"
1110
"go.followtheprocess.codes/snapshot"
1211
"go.followtheprocess.codes/test"
@@ -1304,8 +1303,6 @@ func TestUsage(t *testing.T) {
13041303
snap := snapshot.New(t, snapshot.Update(*update))
13051304
set := tt.newSet(t)
13061305

1307-
colour.Disable.Store(true) // For testing
1308-
13091306
got, err := set.Usage()
13101307
test.Ok(t, err)
13111308

internal/style/style.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
// Package style simply provides a uniform terminal printing style via [hue] for use across
2+
// the library.
3+
//
4+
// [hue]: https://github.com/FollowTheProcess/hue
5+
package style
6+
7+
import "go.followtheprocess.codes/hue"
8+
9+
const (
10+
// Title is the style for titles of help text sections like arguments or commands.
11+
Title = hue.Bold | hue.White | hue.Underline
12+
13+
// Bold is simply plain bold text.
14+
Bold = hue.Bold
15+
16+
// MinWidth is the minimum cell width for hue's colour-enabled tabwriter.
17+
MinWidth = 1
18+
19+
// TabWidth is the width of tabs in spaces for tabwriter.
20+
TabWidth = 8
21+
22+
// Padding is the number of PadChars to pad table cells with.
23+
Padding = 2
24+
25+
// PadChar is the character with which to pad table cells.
26+
PadChar = ' '
27+
28+
// Flags is the tabwriter config flags.
29+
Flags = 0
30+
)

0 commit comments

Comments
 (0)