Skip to content
Browse files

Wrap help text to terminal width

  • Loading branch information...
1 parent 50efe78 commit fc6548b20fe4cdeabd00f43526edc1e96b07edf9 Jesse van den Kieboom committed Sep 3, 2012
Showing with 81 additions and 5 deletions.
  1. +36 −0 convert.go
  2. +22 −0 convert_unix.go
  3. +5 −0 convert_windows.go
  4. +18 −5 help.go
View
36 convert.go
@@ -169,3 +169,39 @@ func convert(val string, retval reflect.Value, options reflect.StructTag) error
return nil
}
+
+func wrapText(s string, l int, prefix string) string {
+ // Basic text wrapping of s at spaces to fit in l
+ var ret string
+
+ s = strings.TrimSpace(s)
+
+ for len(s) > l {
+ // Try to split on space
+ suffix := ""
+
+ pos := strings.LastIndex(s[:l], " ")
+
+ if pos < 0 {
+ pos = l - 1
+ suffix = "-\n"
+ }
+
+ if len(ret) != 0 {
+ ret += "\n" + prefix
+ }
+
+ ret += strings.TrimSpace(s[:pos]) + suffix
+ s = strings.TrimSpace(s[pos:])
+ }
+
+ if len(s) > 0 {
+ if len(ret) != 0 {
+ ret += "\n" + prefix
+ }
+
+ return ret + s
+ }
+
+ return ret
+}
View
22 convert_unix.go
@@ -0,0 +1,22 @@
+package flags
+
+import (
+ "syscall"
+ "unsafe"
+)
+
+type winsize struct {
+ ws_row, ws_col uint16
+ ws_xpixel, ws_ypixel uint16
+}
+
+func getTerminalColumns() int {
+ ws := winsize {}
+
+ syscall.Syscall(syscall.SYS_IOCTL,
+ uintptr(0),
+ uintptr(0x5413),
+ uintptr(unsafe.Pointer(&ws)))
+
+ return int(ws.ws_col)
+}
View
5 convert_windows.go
@@ -0,0 +1,5 @@
+package flags
+
+func getTerminalColumns() int {
+ return 80
+}
View
23 help.go
@@ -33,7 +33,7 @@ func (p *Parser) maxLongLen() (int, bool) {
return maxlonglen, hasshort
}
-func (p *Parser) showHelpOption(writer *bufio.Writer, option *Option, maxlen int, hasshort bool) {
+func (p *Parser) showHelpOption(writer *bufio.Writer, option *Option, maxlen int, hasshort bool, termcol int) {
if option.ShortName != 0 {
writer.WriteString(" -")
writer.WriteRune(option.ShortName)
@@ -42,6 +42,7 @@ func (p *Parser) showHelpOption(writer *bufio.Writer, option *Option, maxlen int
}
written := 0
+ prelen := 4
if option.LongName != "" {
if option.ShortName != 0 {
@@ -52,20 +53,30 @@ func (p *Parser) showHelpOption(writer *bufio.Writer, option *Option, maxlen int
fmt.Fprintf(writer, "--%s", option.LongName)
written = utf8.RuneCountInString(option.LongName)
+
+ prelen += written + 4
}
if option.Description != "" {
if written < maxlen {
- writer.WriteString(strings.Repeat(" ", maxlen-written))
+ dw := maxlen - written
+
+ writer.WriteString(strings.Repeat(" ", dw))
+ prelen += dw
}
def := convertToString(option.value, option.options)
+ var desc string
if def != "" {
- fmt.Fprintf(writer, "%s (%v)", option.Description, def)
+ desc = fmt.Sprintf("%s (%v)", option.Description, def)
} else {
- writer.WriteString(option.Description)
+ desc = option.Description
}
+
+ writer.WriteString(wrapText(desc,
+ termcol - prelen,
+ strings.Repeat(" ", prelen)))
}
writer.WriteString("\n")
@@ -97,13 +108,15 @@ func (p *Parser) ShowHelp(writer io.Writer) {
maxlonglen, hasshort := p.maxLongLen()
maxlen := maxlonglen + 4
+ termcol := getTerminalColumns()
+
for _, grp := range p.Groups {
wr.WriteString("\n")
fmt.Fprintf(wr, "%s:\n", grp.Name)
for _, info := range grp.Options {
- p.showHelpOption(wr, info, maxlen, hasshort)
+ p.showHelpOption(wr, info, maxlen, hasshort, termcol)
}
}

0 comments on commit fc6548b

Please sign in to comment.
Something went wrong with that request. Please try again.