Skip to content

Commit

Permalink
Gracefully handle non-tty stdout
Browse files Browse the repository at this point in the history
  • Loading branch information
sgreben committed Mar 30, 2018
1 parent d9d6da4 commit e95edd4
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 12 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
VERSION = 1.1.4
VERSION = 1.1.5

APP := jp
PACKAGES := $(shell go list -f {{.Dir}} ./...)
Expand Down
14 changes: 8 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,16 +37,16 @@ Or [download the binary](https://github.com/sgreben/jp/releases/latest) from the

```bash
# Linux
curl -LO https://github.com/sgreben/jp/releases/download/1.1.4/jp_1.1.4_linux_x86_64.zip
unzip jp_1.1.4_linux_x86_64.zip
curl -LO https://github.com/sgreben/jp/releases/download/1.1.5/jp_1.1.5_linux_x86_64.zip
unzip jp_1.1.5_linux_x86_64.zip

# OS X
curl -LO https://github.com/sgreben/jp/releases/download/1.1.4/jp_1.1.4_osx_x86_64.zip
unzip jp_1.1.4_osx_x86_64.zip
curl -LO https://github.com/sgreben/jp/releases/download/1.1.5/jp_1.1.5_osx_x86_64.zip
unzip jp_1.1.5_osx_x86_64.zip

# Windows
curl -LO https://github.com/sgreben/jp/releases/download/1.1.4/jp_1.1.4_windows_x86_64.zip
unzip jp_1.1.4_windows_x86_64.zip
curl -LO https://github.com/sgreben/jp/releases/download/1.1.5/jp_1.1.5_windows_x86_64.zip
unzip jp_1.1.5_windows_x86_64.zip
```

## Use it
Expand All @@ -69,6 +69,8 @@ Usage of jp:
Plot width (default 0 (auto))
-canvas value
Canvas type. One of [full quarter braille auto] (default auto)
-bins uint
Number of histogram bins (default 0 (auto))
-input value
Input type. One of [json csv] (default json)
```
Expand Down
4 changes: 2 additions & 2 deletions pkg/terminal/terminal.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ func Width() int {
col, _, err := getWinsize()

if err != nil {
return -1
return 79
}

return col
Expand All @@ -40,7 +40,7 @@ func Width() int {
func Height() int {
_, row, err := getWinsize()
if err != nil {
return -1
return 24
}
return row
}
16 changes: 13 additions & 3 deletions pkg/terminal/terminal_sysioctl.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,19 @@ import (
func getWinsize() (int, int, error) {

ws, err := unix.IoctlGetWinsize(int(os.Stdout.Fd()), unix.TIOCGWINSZ)
if err != nil {
return 0, 0, os.NewSyscallError("GetWinsize", err)
if err == nil {
return int(ws.Col), int(ws.Row), nil
}

return int(ws.Col), int(ws.Row), nil
ws, err = unix.IoctlGetWinsize(int(os.Stdin.Fd()), unix.TIOCGWINSZ)
if err == nil {
return int(ws.Col), int(ws.Row), nil
}

ws, err = unix.IoctlGetWinsize(int(os.Stderr.Fd()), unix.TIOCGWINSZ)
if err == nil {
return int(ws.Col), int(ws.Row), nil
}

return 0, 0, os.NewSyscallError("GetWinsize", err)
}

0 comments on commit e95edd4

Please sign in to comment.