Skip to content

Commit 924695f

Browse files
Centralise the tabwriter creation (#182)
1 parent e7fbefc commit 924695f

7 files changed

Lines changed: 79 additions & 19 deletions

File tree

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@ coverage.html
2222
# Taskfile
2323
.task
2424

25+
# rumdl
26+
.rumdl-cache
27+
2528
# Comparative benchmarks
2629
before.txt
2730
after.txt

.rumdl.toml

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# rumdl configuration file
2+
3+
# Global configuration options
4+
[global]
5+
# List of rules to disable (uncomment and modify as needed)
6+
disable = [
7+
"MD033", # No inline html
8+
"MD013", # Line length
9+
]
10+
11+
# List of rules to enable exclusively (if provided, only these rules will run)
12+
# enable = ["MD001", "MD003", "MD004"]
13+
14+
# List of file/directory patterns to include for linting (if provided, only these will be linted)
15+
# include = [
16+
# "docs/*.md",
17+
# "src/**/*.md",
18+
# "README.md"
19+
# ]
20+
21+
# List of file/directory patterns to exclude from linting
22+
exclude = [
23+
# Common directories to exclude
24+
".git",
25+
".github",
26+
"node_modules",
27+
"vendor",
28+
"dist",
29+
"build",
30+
31+
# Specific files or patterns
32+
"CHANGELOG.md",
33+
"LICENSE.md",
34+
]

README.md

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -255,8 +255,12 @@ cli.New(
255255
)
256256
```
257257

258-
And just like [Flags](#flags), your argument types are all inferred and parsed automatically ✨, and you get nicer `--help` output too! Have a look at the [`./examples`](https://github.com/FollowTheProcess/cli/tree/main/examples)
259-
to see more!
258+
And just like [Flags](#flags), your argument types are all inferred and parsed automatically ✨, and you get nicer `--help` output too!
259+
260+
Have a look at the [`./examples`](https://github.com/FollowTheProcess/cli/tree/main/examples) to see more!
261+
262+
Other CLI libraries that do this sort of thing rely heavily on reflection and struct tags, `cli` does things differently! We prefer Go code
263+
to be 100% type safe and statically checkable, so all this is implemented using generics and compile time checks 🚀
260264

261265
> [!NOTE]
262266
> Just like flags, you can't really get this wrong. The types you can use for arguments are part of a generic constraint so using the wrong type results in a compiler error

Taskfile.yml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,19 @@ tasks:
2323

2424
fmt:
2525
desc: Run go fmt on all source files
26+
preconditions:
27+
- sh: command -v golangci-lint
28+
msg: golangci-lint not installed, see https://golangci-lint.run/usage/install/#local-installation
29+
30+
- sh: command -v rumdl
31+
msg: rumdl not installed, see https://github.com/rvben/rumdl#installation
2632
sources:
2733
- "**/*.go"
2834
- .golangci.yml
35+
- "**/*.md"
2936
cmds:
3037
- golangci-lint fmt ./...
38+
- rumdl fmt --fix .
3139

3240
test:
3341
desc: Run the test suite
@@ -65,8 +73,12 @@ tasks:
6573

6674
- sh: command -v typos
6775
msg: requires typos-cli, run `brew install typos-cli`
76+
77+
- sh: command -v rumdl
78+
msg: rumdl not installed, see https://github.com/rvben/rumdl#installation
6879
cmds:
6980
- golangci-lint run --fix
81+
- rumdl check --fix .
7082
- typos
7183
- nilaway ./...
7284

command.go

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,6 @@ import (
1313
"go.followtheprocess.codes/cli/internal/arg"
1414
"go.followtheprocess.codes/cli/internal/flag"
1515
"go.followtheprocess.codes/cli/internal/style"
16-
17-
"go.followtheprocess.codes/hue/tabwriter"
1816
)
1917

2018
const (
@@ -581,7 +579,7 @@ func writeArgumentsSection(cmd *Command, s *strings.Builder) error {
581579
s.WriteString("\n\n")
582580
s.WriteString(style.Title.Text("Arguments"))
583581
s.WriteString(":\n\n")
584-
tw := tabwriter.NewWriter(s, style.MinWidth, style.TabWidth, style.Padding, style.PadChar, style.Flags)
582+
tw := style.Tabwriter(s)
585583

586584
for _, arg := range cmd.args {
587585
switch arg.Default() {
@@ -646,7 +644,7 @@ func writeSubcommands(cmd *Command, s *strings.Builder) error {
646644
s.WriteByte(':')
647645
s.WriteString("\n\n")
648646

649-
tw := tabwriter.NewWriter(s, style.MinWidth, style.TabWidth, style.Padding, style.PadChar, style.Flags)
647+
tw := style.Tabwriter(s)
650648
for _, subcommand := range cmd.subcommands {
651649
fmt.Fprintf(tw, " %s\t%s\n", style.Bold.Text(subcommand.name), subcommand.short)
652650
}

internal/flag/set.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import (
1010
"go.followtheprocess.codes/cli/flag"
1111
"go.followtheprocess.codes/cli/internal/format"
1212
"go.followtheprocess.codes/cli/internal/style"
13-
"go.followtheprocess.codes/hue/tabwriter"
1413
)
1514

1615
// usageBufferSize is sufficient to hold most commands flag usage text.
@@ -201,7 +200,7 @@ func (s *Set) Usage() (string, error) {
201200

202201
slices.Sort(names)
203202

204-
tw := tabwriter.NewWriter(buf, style.MinWidth, style.TabWidth, style.Padding, style.PadChar, style.Flags)
203+
tw := style.Tabwriter(buf)
205204

206205
for _, name := range names {
207206
f := s.flags[name]

internal/style/style.go

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,12 @@
44
// [hue]: https://github.com/FollowTheProcess/hue
55
package style
66

7-
import "go.followtheprocess.codes/hue"
7+
import (
8+
"io"
9+
10+
"go.followtheprocess.codes/hue"
11+
"go.followtheprocess.codes/hue/tabwriter"
12+
)
813

914
const (
1015
// Title is the style for titles of help text sections like arguments or commands.
@@ -13,18 +18,23 @@ const (
1318
// Bold is simply plain bold text.
1419
Bold = hue.Bold
1520

16-
// MinWidth is the minimum cell width for hue's colour-enabled tabwriter.
17-
MinWidth = 1
21+
// minWidth is the minimum cell width for hue's colour-enabled tabwriter.
22+
minWidth = 1
1823

19-
// TabWidth is the width of tabs in spaces for tabwriter.
20-
TabWidth = 8
24+
// tabWidth is the width of tabs in spaces for tabwriter.
25+
tabWidth = 8
2126

22-
// Padding is the number of PadChars to pad table cells with.
23-
Padding = 2
27+
// padding is the number of PadChars to pad table cells with.
28+
padding = 2
2429

25-
// PadChar is the character with which to pad table cells.
26-
PadChar = ' '
30+
// padChar is the character with which to pad table cells.
31+
padChar = ' '
2732

28-
// Flags is the tabwriter config flags.
29-
Flags = 0
33+
// flags is the tabwriter config flags.
34+
flags = 0
3035
)
36+
37+
// Tabwriter returns a [hue.Tabwriter] configured with cli house style.
38+
func Tabwriter(w io.Writer) *tabwriter.Writer {
39+
return tabwriter.NewWriter(w, minWidth, tabWidth, padding, padChar, flags)
40+
}

0 commit comments

Comments
 (0)