Skip to content

Commit

Permalink
Add word-wrap support, with wrap length provided by the user
Browse files Browse the repository at this point in the history
We could try to automatically detect the terminal width and wrap at that
point, but this would increase the binary footprint for all users even
if not using this feature.

Instead, we can allow users to specify their preferred line length limit
(if any), and those who want to bear the cost of checking the terminal
size can do so if they wish. This also makes the feature more testable.

Original patch by Sascha Grunert <sgrunert@suse.com>
  • Loading branch information
mostynb committed May 13, 2020
1 parent 6102689 commit 02c1754
Show file tree
Hide file tree
Showing 5 changed files with 174 additions and 38 deletions.
2 changes: 2 additions & 0 deletions app.go
Expand Up @@ -74,6 +74,8 @@ type App struct {
Copyright string
// Writer writer to write output to
Writer io.Writer
// If greater than zero, wrap help text lines at this length (experimental).
WrapAt int
// ErrWriter writes error output
ErrWriter io.Writer
// ExitErrHandler processes any error encountered while running an App before
Expand Down
2 changes: 1 addition & 1 deletion app_test.go
Expand Up @@ -1197,7 +1197,7 @@ func TestAppHelpPrinter(t *testing.T) {
}()

var wasCalled = false
HelpPrinter = func(w io.Writer, template string, data interface{}) {
HelpPrinter = func(w io.Writer, template string, data interface{}, wrapAt int) {
wasCalled = true
}

Expand Down
83 changes: 73 additions & 10 deletions help.go
Expand Up @@ -42,10 +42,10 @@ var helpSubcommand = &Command{
}

// Prints help for the App or Command
type helpPrinter func(w io.Writer, templ string, data interface{})
type helpPrinter func(w io.Writer, templ string, data interface{}, wrapAt int)

// Prints help for the App or Command with custom template function.
type helpPrinterCustom func(w io.Writer, templ string, data interface{}, customFunc map[string]interface{})
type helpPrinterCustom func(w io.Writer, templ string, data interface{}, customFunc map[string]interface{}, wrapAt int)

// HelpPrinter is a function that writes the help output. If not set explicitly,
// this calls HelpPrinterCustom using only the default template functions.
Expand Down Expand Up @@ -78,7 +78,7 @@ func ShowAppHelp(c *Context) error {
}

if c.App.ExtraInfo == nil {
HelpPrinter(c.App.Writer, template, c.App)
HelpPrinter(c.App.Writer, template, c.App, c.App.WrapAt)
return nil
}

Expand All @@ -87,7 +87,7 @@ func ShowAppHelp(c *Context) error {
"ExtraInfo": c.App.ExtraInfo,
}
}
HelpPrinterCustom(c.App.Writer, template, c.App, customAppData())
HelpPrinterCustom(c.App.Writer, template, c.App, customAppData(), c.App.WrapAt)

return nil
}
Expand Down Expand Up @@ -189,7 +189,7 @@ func ShowCommandHelpAndExit(c *Context, command string, code int) {
func ShowCommandHelp(ctx *Context, command string) error {
// show the subcommand help for a command with subcommands
if command == "" {
HelpPrinter(ctx.App.Writer, SubcommandHelpTemplate, ctx.App)
HelpPrinter(ctx.App.Writer, SubcommandHelpTemplate, ctx.App, ctx.App.WrapAt)
return nil
}

Expand All @@ -200,7 +200,7 @@ func ShowCommandHelp(ctx *Context, command string) error {
templ = CommandHelpTemplate
}

HelpPrinter(ctx.App.Writer, templ, c)
HelpPrinter(ctx.App.Writer, templ, c, ctx.App.WrapAt)

return nil
}
Expand Down Expand Up @@ -261,9 +261,22 @@ func ShowCommandCompletions(ctx *Context, command string) {
//
// The customFuncs map will be combined with a default template.FuncMap to
// allow using arbitrary functions in template rendering.
func printHelpCustom(out io.Writer, templ string, data interface{}, customFuncs map[string]interface{}) {
func printHelpCustom(out io.Writer, templ string, data interface{}, customFuncs map[string]interface{}, wrapAt int) {

wrapFunc := func(input string, offset int) string {
return input
}

if wrapAt > 0 {
wrapFunc = func(input string, offset int) string {
return wrap(input, offset, wrapAt)
}
}

funcMap := template.FuncMap{
"join": strings.Join,
"join": strings.Join,
"wrap": wrapFunc,
"offset": offset,
}
for key, value := range customFuncs {
funcMap[key] = value
Expand All @@ -284,8 +297,8 @@ func printHelpCustom(out io.Writer, templ string, data interface{}, customFuncs
_ = w.Flush()
}

func printHelp(out io.Writer, templ string, data interface{}) {
HelpPrinterCustom(out, templ, data, nil)
func printHelp(out io.Writer, templ string, data interface{}, wrapAt int) {
HelpPrinterCustom(out, templ, data, nil, wrapAt)
}

func checkVersion(c *Context) bool {
Expand Down Expand Up @@ -366,3 +379,53 @@ func checkCommandCompletions(c *Context, name string) bool {
ShowCommandCompletions(c, name)
return true
}

func wrap(input string, offset int, wrapAt int) string {
var sb strings.Builder

lines := strings.Split(input, "\n")

for i, line := range lines {
if i != 0 {
sb.WriteString(strings.Repeat(" ", offset))
}

sb.WriteString(wrapLine(line, offset, wrapAt))

if i != len(lines)-1 {
sb.WriteString("\n")
}
}

return sb.String()
}

func wrapLine(input string, offset int, wrapAt int) string {
if wrapAt <= offset || len(input) <= wrapAt-offset {
return input
}

lineWidth := wrapAt - offset
words := strings.Fields(strings.TrimSpace(input))
if len(words) == 0 {
return input
}

wrapped := words[0]
spaceLeft := lineWidth - len(wrapped)
for _, word := range words[1:] {
if len(word)+1 > spaceLeft {
wrapped += "\n" + strings.Repeat(" ", offset) + word
spaceLeft = lineWidth - len(word)
} else {
wrapped += " " + word
spaceLeft -= 1 + len(word)
}
}

return wrapped
}

func offset(input string, fixed int) int {
return len(input) + fixed
}

0 comments on commit 02c1754

Please sign in to comment.