From 69f06bfd97b902887c6f74056c3e7aad484dcaf4 Mon Sep 17 00:00:00 2001 From: Mostyn Bramley-Moore Date: Tue, 5 May 2020 08:55:09 +0200 Subject: [PATCH] fixup! Add word-wrap support based on terminal size try using low-level platform-specific implementation --- go.mod | 1 + help.go | 4 +--- internal/build/build.go | 2 +- termwidth_misc.go | 21 +++++++++++++++++++++ termwidth_unix.go | 19 +++++++++++++++++++ termwidth_windows.go | 18 ++++++++++++++++++ 6 files changed, 61 insertions(+), 4 deletions(-) create mode 100644 termwidth_misc.go create mode 100644 termwidth_unix.go create mode 100644 termwidth_windows.go diff --git a/go.mod b/go.mod index 61fe21f4bd..c860bbba64 100644 --- a/go.mod +++ b/go.mod @@ -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 ) diff --git a/help.go b/help.go index 6b5a5dcdda..b93754f60f 100644 --- a/help.go +++ b/help.go @@ -8,8 +8,6 @@ import ( "text/tabwriter" "text/template" "unicode/utf8" - - "golang.org/x/crypto/ssh/terminal" ) var helpCommand = &Command{ @@ -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) diff --git a/internal/build/build.go b/internal/build/build.go index 95ee96905b..197cfa5b60 100644 --- a/internal/build/build.go +++ b/internal/build/build.go @@ -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 = "✅" diff --git a/termwidth_misc.go b/termwidth_misc.go new file mode 100644 index 0000000000..2eb168e9e0 --- /dev/null +++ b/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") +} diff --git a/termwidth_unix.go b/termwidth_unix.go new file mode 100644 index 0000000000..617de3e4a5 --- /dev/null +++ b/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 +} diff --git a/termwidth_windows.go b/termwidth_windows.go new file mode 100644 index 0000000000..2060d7942b --- /dev/null +++ b/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 +}