-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhelp_builder.go
More file actions
67 lines (55 loc) 路 1.7 KB
/
Copy pathhelp_builder.go
File metadata and controls
67 lines (55 loc) 路 1.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
package command
import (
"fmt"
"io"
"github.com/ProCode-Software/klar/internal/cli"
"github.com/ProCode-Software/klar/internal/cli/ansi"
)
// A HelpBuilder writes a list of commands and flags to an [io.Writer].
type HelpBuilder struct {
TW *cli.TabWriter
Color func(string) string
}
func NewHelpBuilder(w io.Writer) *HelpBuilder {
tw := cli.NewTabWriterOutput(w)
tw.Margin = 2
tw.MinWidth = 8
tw.Spacing = 4
return &HelpBuilder{TW: tw}
}
// ShortTitle writes a newline then s in title style followed by a space.
func (hb *HelpBuilder) ShortTitle(s string) {
fmt.Fprintf(hb.TW.Output, "\n%s%s ", ansi.Bold(s), ansi.BoldDim(":"))
}
// ShortTitleNoNewline is ShortTitle, but does not print a newline before the title.
func (hb *HelpBuilder) ShortTitleNoNewline(s string) {
fmt.Fprintf(hb.TW.Output, "%s%s ", ansi.Bold(s), ansi.BoldDim(":"))
}
// Title ends the previous group and writes a header.
func (hb *HelpBuilder) Title(title string) {
hb.Flush()
fmt.Fprintf(hb.TW.Output, "\n%s%s\n", ansi.Bold(title), ansi.BoldDim(":"))
}
// Split flushes the tabwriter, writes a newline, and sets the color of the new group.
func (hb *HelpBuilder) Split(color func(string) string) {
hb.Color = color
hb.Flush()
fmt.Fprintln(hb.TW.Output)
}
// Command writes a group entry.
func (hb *HelpBuilder) Command(name, desc string) {
hb.TW.WriteCells(hb.Color(name), desc)
}
// Print writes to the output [io.Writer].
func (hb *HelpBuilder) Print(s ...any) {
fmt.Fprint(hb.TW.Output, s...)
}
func (hb *HelpBuilder) Println(s ...any) {
fmt.Fprintln(hb.TW.Output, s...)
}
// Flush flushes the tab writer, panicking if an error occurs.
func (hb *HelpBuilder) Flush() {
if _, err := hb.TW.Flush(); err != nil {
panic(err)
}
}