Skip to content

Commit 9880f3b

Browse files
committed
add SetHelpTabStop(tabStop)
1 parent ec99005 commit 9880f3b

File tree

4 files changed

+55
-13
lines changed

4 files changed

+55
-13
lines changed

def_test.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,10 @@ func TestSingleCommandLine1(t *testing.T) {
2828
}()
2929

3030
os.Args = []string{"consul-tags", "kv", "b"}
31+
3132
cmdr.SetInternalOutputStreams(nil, nil)
33+
cmdr.SetHelpTabStop(70)
34+
3235
_ = cmdr.Exec(rootCmd)
3336
_ = cmdr.ExecWith(rootCmd, nil, nil)
3437

examples/demo/demo/entry.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ func Entry() {
2525
// cmdr.EnableGenerateCommands = false
2626

2727
daemon.Enable(svr.NewDaemon(), nil, nil, nil)
28+
// cmdr.SetHelpTabStop(40)
2829

2930
if err := cmdr.Exec(rootCmd); err != nil {
3031
logrus.Errorf("Error: %v", err)

print.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ func ferr(fmtStr string, args ...interface{}) {
2323
}
2424

2525
func printHelp(command *Command, justFlags bool) {
26+
SetHelpTabStop(tabStop)
27+
2628
if GetIntP(getPrefix(), "help-zsh") > 0 {
2729
printHelpZsh(command, justFlags)
2830
} else if GetBoolP(getPrefix(), "help-bash") {

print_painter_help.go

Lines changed: 49 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ package cmdr
66

77
import (
88
"fmt"
9+
"strconv"
910
"strings"
1011
)
1112

@@ -82,9 +83,9 @@ func (s *helpPainter) FpCommandsTitle(command *Command) {
8283
func (s *helpPainter) FpCommandsGroupTitle(group string) {
8384
if group != UnsortedGroup {
8485
if GetBoolR("no-color") {
85-
s.Printf(" [%s]", StripOrderPrefix(group))
86+
s.Printf(fmtCmdGroupTitleNC, StripOrderPrefix(group))
8687
} else {
87-
s.Printf(" [\x1b[2m\x1b[%dm%s\x1b[0m]", CurrentGroupTitleColor, StripOrderPrefix(group))
88+
s.Printf(fmtCmdGroupTitle, CurrentGroupTitleColor, StripOrderPrefix(group))
8889
}
8990
}
9091
}
@@ -93,18 +94,18 @@ func (s *helpPainter) FpCommandsLine(command *Command) {
9394
if !command.Hidden {
9495
if len(command.Deprecated) > 0 {
9596
if GetBoolR("no-color") {
96-
s.Printf(" %-48s%s [deprecated since %v]", command.GetTitleNames(), command.Description, command.Deprecated)
97+
s.Printf(fmtCmdlineDepNC, command.GetTitleNames(), command.Description, command.Deprecated)
9798
} else {
98-
s.Printf(" \x1b[%dm\x1b[%dm%-48s%s\x1b[0m [deprecated since %v]", BgNormal, CurrentDescColor, command.GetTitleNames(), command.Description, command.Deprecated)
99+
s.Printf(fmtCmdlineDep, BgNormal, CurrentDescColor, command.GetTitleNames(), command.Description, command.Deprecated)
99100
}
100101
} else {
101102
if GetBoolR("no-color") {
102-
s.Printf(" %-48s%s", command.GetTitleNames(), command.Description)
103+
s.Printf(fmtCmdlineNC, command.GetTitleNames(), command.Description)
103104
} else {
104105
// s.Printf(" %-48s%v", command.GetTitleNames(), command.Description)
105106
// s.Printf("\n\x1b[%dm\x1b[%dm%s\x1b[0m", BgNormal, DarkColor, title)
106107
// s.Printf(" [\x1b[%dm\x1b[%dm%s\x1b[0m]", BgDim, DarkColor, StripOrderPrefix(group))
107-
s.Printf(" %-48s\x1b[%dm\x1b[%dm%s\x1b[0m", command.GetTitleNames(), BgNormal, CurrentDescColor, command.Description)
108+
s.Printf(fmtCmdline, command.GetTitleNames(), BgNormal, CurrentDescColor, command.Description)
108109
}
109110
}
110111
}
@@ -127,34 +128,69 @@ func (s *helpPainter) FpFlagsTitle(command *Command, flag *Flag, title string) {
127128
func (s *helpPainter) FpFlagsGroupTitle(group string) {
128129
if group != UnsortedGroup {
129130
if GetBoolR("no-color") {
130-
s.Printf(" [%s]", StripOrderPrefix(group))
131+
s.Printf(fmtGroupTitleNC, StripOrderPrefix(group))
131132
} else {
132133
// fp(" [%s]:", StripOrderPrefix(group))
133134
// // echo -e "Normal \e[2mDim"
134135
// _, _ = fmt.Fprintf(b, "\x1b[%dm%s\x1b[0m\x1b[2m\x1b[%dm[%04d]\x1b[0m%-48s \x1b[2m\x1b[%dm%s\x1b[0m ",
135136
// levelColor, levelText, DarkColor, int(entry.Time.Sub(baseTimestamp)/time.Second), entry.Message, DarkColor, caller)
136-
s.Printf(" [\x1b[2m\x1b[%dm%s\x1b[0m]", CurrentGroupTitleColor, StripOrderPrefix(group))
137+
s.Printf(fmtGroupTitle, CurrentGroupTitleColor, StripOrderPrefix(group))
137138
}
138139
}
139140
}
140141

141142
func (s *helpPainter) FpFlagsLine(command *Command, flg *Flag, defValStr string) {
142143
if len(flg.Deprecated) > 0 {
143144
if GetBoolR("no-color") {
144-
s.Printf(" %-48s%s%s [deprecated since %v]", flg.GetTitleFlagNames(), flg.Description,
145-
defValStr, flg.Deprecated)
145+
s.Printf(fmtFlagsDepNC, // " %-48s%s%s [deprecated since %v]",
146+
flg.GetTitleFlagNames(), flg.Description, defValStr, flg.Deprecated)
146147
} else {
147-
s.Printf(" \x1b[%dm\x1b[%dm%-48s%s\x1b[%dm\x1b[%dm%s\x1b[0m [deprecated since %v]",
148+
s.Printf(fmtFlagsDep, // " \x1b[%dm\x1b[%dm%-48s%s\x1b[%dm\x1b[%dm%s\x1b[0m [deprecated since %v]",
148149
BgNormal, CurrentDescColor, flg.GetTitleFlagNames(), flg.Description,
149150
BgItalic, CurrentDefaultValueColor, defValStr, flg.Deprecated)
150151
}
151152
} else {
152153
if GetBoolR("no-color") {
153-
s.Printf(" %-48s%s%s", flg.GetTitleFlagNames(), flg.Description, defValStr)
154+
s.Printf(fmtFlagsNC, flg.GetTitleFlagNames(), flg.Description, defValStr)
154155
} else {
155-
s.Printf(" %-48s\x1b[%dm\x1b[%dm%s\x1b[%dm\x1b[%dm%s\x1b[0m",
156+
s.Printf(fmtFlags, // " %-48s\x1b[%dm\x1b[%dm%s\x1b[%dm\x1b[%dm%s\x1b[0m",
156157
flg.GetTitleFlagNames(), BgNormal, CurrentDescColor, flg.Description,
157158
BgItalic, CurrentDefaultValueColor, defValStr)
158159
}
159160
}
160161
}
162+
163+
// SetHelpTabStop sets the tab stop for help screen output
164+
func SetHelpTabStop(tabStop int) {
165+
initTabStop(tabStop)
166+
}
167+
168+
func initTabStop(ts int) {
169+
tabStop = ts
170+
171+
var s = strconv.Itoa(tabStop)
172+
173+
fmtCmdGroupTitle = " [\x1b[2m\x1b[%dm%s\x1b[0m]"
174+
fmtCmdGroupTitleNC = " [%s]"
175+
176+
fmtCmdline = " %-" + s + "s\x1b[%dm\x1b[%dm%s\x1b[0m"
177+
fmtCmdlineDep = " \x1b[%dm\x1b[%dm%-" + s + "s%s\x1b[0m [deprecated since %v]"
178+
fmtCmdlineNC = " %-" + s + "s%s"
179+
fmtCmdlineDepNC = " %-" + s + "s%s [deprecated since %v]"
180+
181+
fmtGroupTitle = " [\x1b[2m\x1b[%dm%s\x1b[0m]"
182+
fmtGroupTitleNC = " [%s]"
183+
184+
fmtFlagsDep = " \x1b[%dm\x1b[%dm%-" + s + "s%s\x1b[%dm\x1b[%dm%s\x1b[0m [deprecated since %v]"
185+
fmtFlags = " %-" + s + "s\x1b[%dm\x1b[%dm%s\x1b[%dm\x1b[%dm%s\x1b[0m"
186+
fmtFlagsDepNC = " %-" + s + "s%s%s [deprecated since %v]"
187+
fmtFlagsNC = " %-" + s + "s%s%s"
188+
}
189+
190+
var (
191+
tabStop = 48
192+
fmtCmdGroupTitle, fmtCmdGroupTitleNC string
193+
fmtCmdline, fmtCmdlineDep, fmtCmdlineNC, fmtCmdlineDepNC string
194+
fmtGroupTitle, fmtGroupTitleNC string
195+
fmtFlags, fmtFlagsDep, fmtFlagsNC, fmtFlagsDepNC string
196+
)

0 commit comments

Comments
 (0)