diff --git a/cli/helptext.go b/cli/helptext.go index 441e344a..3ff826e2 100644 --- a/cli/helptext.go +++ b/cli/helptext.go @@ -4,21 +4,23 @@ import ( "errors" "fmt" "io" + "os" "sort" "strings" "text/template" - "github.com/ipfs/go-ipfs-cmds" + cmds "github.com/ipfs/go-ipfs-cmds" + "golang.org/x/crypto/ssh/terminal" ) const ( - terminalWidth = 100 - requiredArg = "<%v>" - optionalArg = "[<%v>]" - variadicArg = "%v..." - shortFlag = "-%v" - longFlag = "--%v" - optionType = "(%v)" + defaultTerminalWidth = 100 + requiredArg = "<%v>" + optionalArg = "[<%v>]" + variadicArg = "%v..." + shortFlag = "-%v" + longFlag = "--%v" + optionType = "(%v)" whitespace = "\r\n\t " @@ -117,6 +119,23 @@ SUBCOMMANDS var longHelpTemplate *template.Template var shortHelpTemplate *template.Template +func getTerminalWidth(out io.Writer) int { + file, ok := out.(*os.File) + if ok { + fmt.Println("got file") + if terminal.IsTerminal(int(file.Fd())) { + fmt.Println("is terminal") + + width, _, err := terminal.GetSize(int(file.Fd())) + if err == nil { + fmt.Println(width) + return width + } + } + } + return defaultTerminalWidth +} + func init() { longHelpTemplate = template.Must(template.New("longHelp").Parse(longHelpFormat)) shortHelpTemplate = template.Must(template.New("shortHelp").Parse(shortHelpFormat)) @@ -163,7 +182,7 @@ func LongHelp(rootName string, root *cmds.Command, path []string, out io.Writer) MoreHelp: (cmd != root), } - width := terminalWidth - len(indentStr) + width := getTerminalWidth(out) - len(indentStr) if len(cmd.Helptext.LongDescription) > 0 { fields.Description = cmd.Helptext.LongDescription @@ -224,7 +243,7 @@ func ShortHelp(rootName string, root *cmds.Command, path []string, out io.Writer MoreHelp: (cmd != root), } - width := terminalWidth - len(indentStr) + width := getTerminalWidth(out) - len(indentStr) // autogen fields that are empty if len(cmd.Helptext.Usage) > 0 { diff --git a/cli/helptext_test.go b/cli/helptext_test.go index b362f8f0..c5f524c8 100644 --- a/cli/helptext_test.go +++ b/cli/helptext_test.go @@ -4,7 +4,7 @@ import ( "strings" "testing" - "github.com/ipfs/go-ipfs-cmds" + cmds "github.com/ipfs/go-ipfs-cmds" ) func TestSynopsisGenerator(t *testing.T) { @@ -22,6 +22,7 @@ func TestSynopsisGenerator(t *testing.T) { }, }, } + terminalWidth := 100 syn := generateSynopsis(terminalWidth, command, "cmd") t.Logf("Synopsis is: %s", syn) if !strings.HasPrefix(syn, "cmd ") { diff --git a/go.mod b/go.mod index 546c3675..abfc9321 100644 --- a/go.mod +++ b/go.mod @@ -6,4 +6,5 @@ require ( github.com/ipfs/go-log v0.0.1 github.com/rs/cors v1.6.0 github.com/texttheater/golang-levenshtein v0.0.0-20180516184445-d188e65d659e + golang.org/x/crypto v0.0.0-20190513172903-22d7a77e9e5f ) diff --git a/go.sum b/go.sum index 8ae2477c..346423ef 100644 --- a/go.sum +++ b/go.sum @@ -27,9 +27,18 @@ github.com/texttheater/golang-levenshtein v0.0.0-20180516184445-d188e65d659e h1: github.com/texttheater/golang-levenshtein v0.0.0-20180516184445-d188e65d659e/go.mod h1:XDKHRm5ThF8YJjx001LtgelzsoaEcvnA7lVWz9EeX3g= github.com/whyrusleeping/go-logging v0.0.0-20170515211332-0457bb6b88fc h1:9lDbC6Rz4bwmou+oE6Dt4Cb2BGMur5eR/GYptkKUVHo= github.com/whyrusleeping/go-logging v0.0.0-20170515211332-0457bb6b88fc/go.mod h1:bopw91TMyo8J3tvftk8xmU2kPmlrt4nScJQZU2hE5EM= +golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= +golang.org/x/crypto v0.0.0-20190513172903-22d7a77e9e5f h1:R423Cnkcp5JABoeemiGEPlt9tHXFfw5kvc0yqlxRPWo= +golang.org/x/crypto v0.0.0-20190513172903-22d7a77e9e5f/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI= golang.org/x/net v0.0.0-20190227160552-c95aed5357e7 h1:C2F/nMkR/9sfUTpvR3QrjBuTdvMUC/cFajkphs1YLQo= golang.org/x/net v0.0.0-20190227160552-c95aed5357e7/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= +golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3 h1:0GoQqolDA55aaLxZyTzK/Y2ePZzZTUrRacwib7cNsYQ= +golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= +golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20190222072716-a9d3bda3a223/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20190302025703-b6889370fb10 h1:xQJI9OEiErEQ++DoXOHqEpzsGMrAv2Q2jyCpi7DmfpQ= golang.org/x/sys v0.0.0-20190302025703-b6889370fb10/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= +golang.org/x/sys v0.0.0-20190412213103-97732733099d h1:+R4KGOnez64A81RvjARKc4UT5/tI9ujCIVX+P5KiHuI= +golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= golang.org/x/tools v0.0.0-20180221164845-07fd8470d635/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=