Skip to content

Commit eae0186

Browse files
committed
feat(helptext): detect terminal width
If the io.writer given to help text is the terminal, read its width. otherwise, use a default.
1 parent 6a6a6f8 commit eae0186

File tree

4 files changed

+41
-11
lines changed

4 files changed

+41
-11
lines changed

cli/helptext.go

+29-10
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,23 @@ import (
44
"errors"
55
"fmt"
66
"io"
7+
"os"
78
"sort"
89
"strings"
910
"text/template"
1011

11-
"github.com/ipfs/go-ipfs-cmds"
12+
cmds "github.com/ipfs/go-ipfs-cmds"
13+
"golang.org/x/crypto/ssh/terminal"
1214
)
1315

1416
const (
15-
terminalWidth = 100
16-
requiredArg = "<%v>"
17-
optionalArg = "[<%v>]"
18-
variadicArg = "%v..."
19-
shortFlag = "-%v"
20-
longFlag = "--%v"
21-
optionType = "(%v)"
17+
defaultTerminalWidth = 100
18+
requiredArg = "<%v>"
19+
optionalArg = "[<%v>]"
20+
variadicArg = "%v..."
21+
shortFlag = "-%v"
22+
longFlag = "--%v"
23+
optionType = "(%v)"
2224

2325
whitespace = "\r\n\t "
2426

@@ -117,6 +119,23 @@ SUBCOMMANDS
117119
var longHelpTemplate *template.Template
118120
var shortHelpTemplate *template.Template
119121

122+
func getTerminalWidth(out io.Writer) int {
123+
file, ok := out.(*os.File)
124+
if ok {
125+
fmt.Println("got file")
126+
if terminal.IsTerminal(int(file.Fd())) {
127+
fmt.Println("is terminal")
128+
129+
width, _, err := terminal.GetSize(int(file.Fd()))
130+
if err == nil {
131+
fmt.Println(width)
132+
return width
133+
}
134+
}
135+
}
136+
return defaultTerminalWidth
137+
}
138+
120139
func init() {
121140
longHelpTemplate = template.Must(template.New("longHelp").Parse(longHelpFormat))
122141
shortHelpTemplate = template.Must(template.New("shortHelp").Parse(shortHelpFormat))
@@ -163,7 +182,7 @@ func LongHelp(rootName string, root *cmds.Command, path []string, out io.Writer)
163182
MoreHelp: (cmd != root),
164183
}
165184

166-
width := terminalWidth - len(indentStr)
185+
width := getTerminalWidth(out) - len(indentStr)
167186

168187
if len(cmd.Helptext.LongDescription) > 0 {
169188
fields.Description = cmd.Helptext.LongDescription
@@ -224,7 +243,7 @@ func ShortHelp(rootName string, root *cmds.Command, path []string, out io.Writer
224243
MoreHelp: (cmd != root),
225244
}
226245

227-
width := terminalWidth - len(indentStr)
246+
width := getTerminalWidth(out) - len(indentStr)
228247

229248
// autogen fields that are empty
230249
if len(cmd.Helptext.Usage) > 0 {

cli/helptext_test.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import (
44
"strings"
55
"testing"
66

7-
"github.com/ipfs/go-ipfs-cmds"
7+
cmds "github.com/ipfs/go-ipfs-cmds"
88
)
99

1010
func TestSynopsisGenerator(t *testing.T) {
@@ -22,6 +22,7 @@ func TestSynopsisGenerator(t *testing.T) {
2222
},
2323
},
2424
}
25+
terminalWidth := 100
2526
syn := generateSynopsis(terminalWidth, command, "cmd")
2627
t.Logf("Synopsis is: %s", syn)
2728
if !strings.HasPrefix(syn, "cmd ") {

go.mod

+1
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,5 @@ require (
66
github.com/ipfs/go-log v0.0.1
77
github.com/rs/cors v1.6.0
88
github.com/texttheater/golang-levenshtein v0.0.0-20180516184445-d188e65d659e
9+
golang.org/x/crypto v0.0.0-20190513172903-22d7a77e9e5f
910
)

go.sum

+9
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,18 @@ github.com/texttheater/golang-levenshtein v0.0.0-20180516184445-d188e65d659e h1:
2727
github.com/texttheater/golang-levenshtein v0.0.0-20180516184445-d188e65d659e/go.mod h1:XDKHRm5ThF8YJjx001LtgelzsoaEcvnA7lVWz9EeX3g=
2828
github.com/whyrusleeping/go-logging v0.0.0-20170515211332-0457bb6b88fc h1:9lDbC6Rz4bwmou+oE6Dt4Cb2BGMur5eR/GYptkKUVHo=
2929
github.com/whyrusleeping/go-logging v0.0.0-20170515211332-0457bb6b88fc/go.mod h1:bopw91TMyo8J3tvftk8xmU2kPmlrt4nScJQZU2hE5EM=
30+
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
31+
golang.org/x/crypto v0.0.0-20190513172903-22d7a77e9e5f h1:R423Cnkcp5JABoeemiGEPlt9tHXFfw5kvc0yqlxRPWo=
32+
golang.org/x/crypto v0.0.0-20190513172903-22d7a77e9e5f/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
3033
golang.org/x/net v0.0.0-20190227160552-c95aed5357e7 h1:C2F/nMkR/9sfUTpvR3QrjBuTdvMUC/cFajkphs1YLQo=
3134
golang.org/x/net v0.0.0-20190227160552-c95aed5357e7/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
35+
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3 h1:0GoQqolDA55aaLxZyTzK/Y2ePZzZTUrRacwib7cNsYQ=
36+
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
37+
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
3238
golang.org/x/sys v0.0.0-20190222072716-a9d3bda3a223/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
3339
golang.org/x/sys v0.0.0-20190302025703-b6889370fb10 h1:xQJI9OEiErEQ++DoXOHqEpzsGMrAv2Q2jyCpi7DmfpQ=
3440
golang.org/x/sys v0.0.0-20190302025703-b6889370fb10/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
41+
golang.org/x/sys v0.0.0-20190412213103-97732733099d h1:+R4KGOnez64A81RvjARKc4UT5/tI9ujCIVX+P5KiHuI=
42+
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
43+
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
3544
golang.org/x/tools v0.0.0-20180221164845-07fd8470d635/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=

0 commit comments

Comments
 (0)