@@ -4,21 +4,23 @@ import (
4
4
"errors"
5
5
"fmt"
6
6
"io"
7
+ "os"
7
8
"sort"
8
9
"strings"
9
10
"text/template"
10
11
11
- "github.com/ipfs/go-ipfs-cmds"
12
+ cmds "github.com/ipfs/go-ipfs-cmds"
13
+ "golang.org/x/crypto/ssh/terminal"
12
14
)
13
15
14
16
const (
15
- terminalWidth = 100
16
- requiredArg = "<%v>"
17
- optionalArg = "[<%v>]"
18
- variadicArg = "%v..."
19
- shortFlag = "-%v"
20
- longFlag = "--%v"
21
- optionType = "(%v)"
17
+ defaultTerminalWidth = 80
18
+ requiredArg = "<%v>"
19
+ optionalArg = "[<%v>]"
20
+ variadicArg = "%v..."
21
+ shortFlag = "-%v"
22
+ longFlag = "--%v"
23
+ optionType = "(%v)"
22
24
23
25
whitespace = "\r \n \t "
24
26
@@ -117,6 +119,19 @@ SUBCOMMANDS
117
119
var longHelpTemplate * template.Template
118
120
var shortHelpTemplate * template.Template
119
121
122
+ func getTerminalWidth (out io.Writer ) int {
123
+ file , ok := out .(* os.File )
124
+ if ok {
125
+ if terminal .IsTerminal (int (file .Fd ())) {
126
+ width , _ , err := terminal .GetSize (int (file .Fd ()))
127
+ if err == nil {
128
+ return width
129
+ }
130
+ }
131
+ }
132
+ return defaultTerminalWidth
133
+ }
134
+
120
135
func init () {
121
136
longHelpTemplate = template .Must (template .New ("longHelp" ).Parse (longHelpFormat ))
122
137
shortHelpTemplate = template .Must (template .New ("shortHelp" ).Parse (shortHelpFormat ))
@@ -163,7 +178,7 @@ func LongHelp(rootName string, root *cmds.Command, path []string, out io.Writer)
163
178
MoreHelp : (cmd != root ),
164
179
}
165
180
166
- width := terminalWidth - len (indentStr )
181
+ width := getTerminalWidth ( out ) - len (indentStr )
167
182
168
183
if len (cmd .Helptext .LongDescription ) > 0 {
169
184
fields .Description = cmd .Helptext .LongDescription
@@ -224,7 +239,7 @@ func ShortHelp(rootName string, root *cmds.Command, path []string, out io.Writer
224
239
MoreHelp : (cmd != root ),
225
240
}
226
241
227
- width := terminalWidth - len (indentStr )
242
+ width := getTerminalWidth ( out ) - len (indentStr )
228
243
229
244
// autogen fields that are empty
230
245
if len (cmd .Helptext .Usage ) > 0 {
0 commit comments