Skip to content

Commit

Permalink
fixup! Add word-wrap support based on terminal size
Browse files Browse the repository at this point in the history
try using low-level platform-specific implementation
  • Loading branch information
mostynb committed May 5, 2020
1 parent 8320a30 commit 69f06bf
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 4 deletions.
1 change: 1 addition & 0 deletions go.mod
Expand Up @@ -6,5 +6,6 @@ require (
github.com/BurntSushi/toml v0.3.1
github.com/cpuguy83/go-md2man/v2 v2.0.0-20190314233015-f79a8a8ca69d
golang.org/x/crypto v0.0.0-20200429183012-4b2356b1ed79
golang.org/x/sys v0.0.0-20190412213103-97732733099d
gopkg.in/yaml.v2 v2.2.2
)
4 changes: 1 addition & 3 deletions help.go
Expand Up @@ -8,8 +8,6 @@ import (
"text/tabwriter"
"text/template"
"unicode/utf8"

"golang.org/x/crypto/ssh/terminal"
)

var helpCommand = &Command{
Expand Down Expand Up @@ -269,7 +267,7 @@ func printHelpCustom(out io.Writer, templ string, data interface{}, customFuncs
return input
}

width, _, err := terminal.GetSize(int(os.Stdout.Fd()))
width, err := termWidth()
if err != nil && width > 0 {
wrapFunc = func(input string, offset int) string {
return wrap(input, offset, width)
Expand Down
2 changes: 1 addition & 1 deletion internal/build/build.go
Expand Up @@ -194,7 +194,7 @@ func checkBinarySizeActionFunc(c *cli.Context) (err error) {
helloSourceFilePath = "./internal/example-hello-world/example-hello-world.go"
helloBuiltFilePath = "./internal/example-hello-world/built-example"
desiredMinBinarySize = 2.0
desiredMaxBinarySize = 2.2
desiredMaxBinarySize = 2.1
badNewsEmoji = "🚨"
goodNewsEmoji = "✨"
checksPassedEmoji = "✅"
Expand Down
21 changes: 21 additions & 0 deletions termwidth_misc.go
@@ -0,0 +1,21 @@
// +build !aix
// +build !darwin
// +build !dragonfly
// +build !freebsd
// +build !linux,!appengine
// +build !netbsd
// +build !openbsd
// +build !windows

// The build constraints above are intended to be the negation of the
// single-line constraint in termwidth_unix.go and also !windows.

package cli

import (
"errors"
)

func termWidth() (int, error) {
return -1, errors.New("Unsupported platform")
}
19 changes: 19 additions & 0 deletions termwidth_unix.go
@@ -0,0 +1,19 @@
// +build aix darwin dragonfly freebsd linux,!appengine netbsd openbsd
// Only tested on linux and darwin so far...

package cli

import (
"os"

"golang.org/x/sys/unix"
)

func termWidth() (int, error) {
ws, err := unix.IoctlGetWinsize(int(os.Stdout.Fd()), unix.TIOCGWINSZ)
if err != nil {
return -1, err
}

return int(ws.Col), nil
}
18 changes: 18 additions & 0 deletions termwidth_windows.go
@@ -0,0 +1,18 @@
package cli

import (
"golang.org/x/sys/windows"
)

func termWidth() (int, error) {
var info windows.ConsoleScreenBufferInfo

// This seems to work in cmd.exe, powershell and cmder, but not in
// git-bash. (Tested on windows 10.)
err = windows.GetConsoleScreenBufferInfo(windows.Stdout, &info)
if err != nil {
return -1, err
}

return int(info.Size.X), nil
}

0 comments on commit 69f06bf

Please sign in to comment.