From f9559f83cac6ecffe90f8889a81cdabbdb345374 Mon Sep 17 00:00:00 2001 From: Sascha Grunert Date: Fri, 17 Jan 2020 12:32:00 +0100 Subject: [PATCH] Add word-wrap support based on terminal size Signed-off-by: Sascha Grunert --- go.mod | 2 ++ go.sum | 4 ++++ help.go | 40 +++++++++++++++++++++++++++++++++++++++- template.go | 2 +- 4 files changed, 46 insertions(+), 2 deletions(-) diff --git a/go.mod b/go.mod index c38d41c14b..f389f9b6f2 100644 --- a/go.mod +++ b/go.mod @@ -5,5 +5,7 @@ go 1.11 require ( github.com/BurntSushi/toml v0.3.1 github.com/cpuguy83/go-md2man/v2 v2.0.0-20190314233015-f79a8a8ca69d + github.com/mattn/go-runewidth v0.0.8 // indirect + github.com/nsf/termbox-go v0.0.0-20191229070316-58d4fcbce2a7 gopkg.in/yaml.v2 v2.2.2 ) diff --git a/go.sum b/go.sum index ef121ff5db..d2b4321ef0 100644 --- a/go.sum +++ b/go.sum @@ -2,6 +2,10 @@ github.com/BurntSushi/toml v0.3.1 h1:WXkYYl6Yr3qBf1K79EBnL4mak0OimBfB0XUf9Vl28OQ github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU= github.com/cpuguy83/go-md2man/v2 v2.0.0-20190314233015-f79a8a8ca69d h1:U+s90UTSYgptZMwQh2aRr3LuazLJIa+Pg3Kc1ylSYVY= github.com/cpuguy83/go-md2man/v2 v2.0.0-20190314233015-f79a8a8ca69d/go.mod h1:maD7wRr/U5Z6m/iR4s+kqSMx2CaBsrgA7czyZG/E6dU= +github.com/mattn/go-runewidth v0.0.8 h1:3tS41NlGYSmhhe/8fhGRzc+z3AYCw1Fe1WAyLuujKs0= +github.com/mattn/go-runewidth v0.0.8/go.mod h1:H031xJmbD/WCDINGzjvQ9THkh0rPKHF+m2gUSrubnMI= +github.com/nsf/termbox-go v0.0.0-20191229070316-58d4fcbce2a7 h1:OkWEy7aQeQTbgdrcGi9bifx+Y6bMM7ae7y42hDFaBvA= +github.com/nsf/termbox-go v0.0.0-20191229070316-58d4fcbce2a7/go.mod h1:IuKpRQcYE1Tfu+oAQqaLisqDeXgjyyltCfsaoYN18NQ= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/russross/blackfriday/v2 v2.0.1 h1:lPqVAte+HuHNfhJ/0LC98ESWRz8afy9tM/0RK8m9o+Q= diff --git a/help.go b/help.go index c1e974a481..aa1a7ca6a5 100644 --- a/help.go +++ b/help.go @@ -8,6 +8,8 @@ import ( "text/tabwriter" "text/template" "unicode/utf8" + + "github.com/nsf/termbox-go" ) var helpCommand = &Command{ @@ -263,7 +265,9 @@ func ShowCommandCompletions(ctx *Context, command string) { // allow using arbitrary functions in template rendering. func printHelpCustom(out io.Writer, templ string, data interface{}, customFuncs map[string]interface{}) { funcMap := template.FuncMap{ - "join": strings.Join, + "join": strings.Join, + "wrap": wrap, + "offset": offset, } for key, value := range customFuncs { funcMap[key] = value @@ -366,3 +370,37 @@ func checkCommandCompletions(c *Context, name string) bool { ShowCommandCompletions(c, name) return true } + +func wrap(input string, offset int) string { + if err := termbox.Init(); err != nil { + return input + } + w, _ := termbox.Size() + termbox.Close() + + if w > offset && len(input) > w-offset { + lineWidth := w - 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 + } + + return input +} + +func offset(input string, fixed int) int { + return len(input) + fixed +} diff --git a/template.go b/template.go index 7c651f1c80..720f1f55b9 100644 --- a/template.go +++ b/template.go @@ -4,7 +4,7 @@ package cli // cli.go uses text/template to render templates. You can // render custom help text by setting this variable. var AppHelpTemplate = `NAME: - {{.Name}}{{if .Usage}} - {{.Usage}}{{end}} + {{$v := offset .Name 6}}{{wrap .Name 3}}{{if .Usage}} - {{wrap .Usage $v}}{{end}} USAGE: {{if .UsageText}}{{.UsageText}}{{else}}{{.HelpName}} {{if .VisibleFlags}}[global options]{{end}}{{if .Commands}} command [command options]{{end}} {{if .ArgsUsage}}{{.ArgsUsage}}{{else}}[arguments...]{{end}}{{end}}{{if .Version}}{{if not .HideVersion}}