@@ -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,23 @@ 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
+ 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
+
120
139
func init () {
121
140
longHelpTemplate = template .Must (template .New ("longHelp" ).Parse (longHelpFormat ))
122
141
shortHelpTemplate = template .Must (template .New ("shortHelp" ).Parse (shortHelpFormat ))
@@ -163,7 +182,7 @@ func LongHelp(rootName string, root *cmds.Command, path []string, out io.Writer)
163
182
MoreHelp : (cmd != root ),
164
183
}
165
184
166
- width := terminalWidth - len (indentStr )
185
+ width := getTerminalWidth ( out ) - len (indentStr )
167
186
168
187
if len (cmd .Helptext .LongDescription ) > 0 {
169
188
fields .Description = cmd .Helptext .LongDescription
@@ -224,7 +243,7 @@ func ShortHelp(rootName string, root *cmds.Command, path []string, out io.Writer
224
243
MoreHelp : (cmd != root ),
225
244
}
226
245
227
- width := terminalWidth - len (indentStr )
246
+ width := getTerminalWidth ( out ) - len (indentStr )
228
247
229
248
// autogen fields that are empty
230
249
if len (cmd .Helptext .Usage ) > 0 {
0 commit comments