Skip to content

Commit

Permalink
Switched terminal width detection
Browse files Browse the repository at this point in the history
Changed code to detect terminal width to be more operating system
independent.
  • Loading branch information
HeavyWombat committed Apr 12, 2018
1 parent 23ffbfa commit a9456ae
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 23 deletions.
2 changes: 1 addition & 1 deletion Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 0 additions & 4 deletions Gopkg.toml
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,6 @@
branch = "master"
name = "github.com/texttheater/golang-levenshtein"

[[constraint]]
branch = "master"
name = "golang.org/x/sys"

[prune]
go-tests = true
unused-packages = true
18 changes: 0 additions & 18 deletions core/core.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ import (
"github.com/HeavyWombat/yaml"
"github.com/mitchellh/hashstructure"
"github.com/texttheater/golang-levenshtein/levenshtein"
"golang.org/x/sys/unix"
)

// Debug log output
Expand Down Expand Up @@ -132,23 +131,6 @@ func Plural(amount int, text ...string) string {
}
}

// GetTerminalSize return the current terminal size with width and height (columns and rows)
func GetTerminalSize() (int, int) {
bounds, err := unix.IoctlGetWinsize(0, unix.TIOCGWINSZ)
if err != nil {
// TODO Add debug output that default terminal sizings were returned due to an error
return 80, 25
}

return int(bounds.Col), int(bounds.Row)
}

// GetTerminalWidth return the current terminal width
func GetTerminalWidth() int {
width, _ := GetTerminalSize()
return width
}

func colorEachLine(color *color.Color, text string) string {
var buf bytes.Buffer

Expand Down
33 changes: 33 additions & 0 deletions core/terminal_unix.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// +build !windows

package core

import (
"syscall"
"unsafe"
)

// GetTerminalWidth return the current terminal width
func GetTerminalWidth() int {
const defaultWidth = 80

type winsize struct {
Row uint16
Col uint16
Xpixel uint16
Ypixel uint16
}

ws := &winsize{}
retCode, _, _ := syscall.Syscall(syscall.SYS_IOCTL,
uintptr(syscall.Stdin),
uintptr(syscall.TIOCGWINSZ),
uintptr(unsafe.Pointer(ws)))

if int(retCode) == -1 {
// TODO Add debug output that default terminal sizings were returned due
return defaultWidth
}

return int(ws.Col)
}
9 changes: 9 additions & 0 deletions core/terminal_windows.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// +build windows

package core

// GetTerminalWidth return the current terminal width
func GetTerminalWidth() int {
// TODO Implement Windows terminal width if this is possible at all.
return 80
}

0 comments on commit a9456ae

Please sign in to comment.