Skip to content

Commit 5db6772

Browse files
authored
Merge pull request #24 from xypwn/fix-terminal-width
Properly decide terminal width on non-unix systems
2 parents 14450b4 + c0acc73 commit 5db6772

File tree

3 files changed

+11
-13
lines changed

3 files changed

+11
-13
lines changed

go.mod

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
module github.com/hellflame/argparse
22

33
go 1.16
4+
5+
require golang.org/x/term v0.1.0

go.sum

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1 h1:SrN+KX8Art/Sf4HNj6Zcz06G7VEz+7w9tdXTPOZ7+l4=
2+
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
3+
golang.org/x/term v0.1.0 h1:g6Z6vPFA9dYBAF7DWcH6sCcOntplXsDKcliusYijMlw=
4+
golang.org/x/term v0.1.0/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=

utils.go

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,23 +3,15 @@ package argparse
33
import (
44
"fmt"
55
"os"
6-
"os/exec"
7-
"strconv"
86
"strings"
7+
8+
"golang.org/x/term"
99
)
1010

1111
func decideTerminalWidth() int {
12-
// decide terminal width
13-
cmd := exec.Command("stty", "size")
14-
cmd.Stdin = os.Stdin // this is important
15-
result, e := cmd.Output()
16-
if e != nil {
17-
result = []byte("0 80")
18-
}
19-
width := 80
20-
parse := strings.Split(strings.TrimRight(string(result), "\n"), " ")
21-
if w, e := strconv.Atoi(parse[1]); e == nil {
22-
width = w
12+
width, _, err := term.GetSize(int(os.Stdout.Fd()))
13+
if err != nil {
14+
return 80
2315
}
2416
return width
2517
}

0 commit comments

Comments
 (0)