diff options
author | Junegunn Choi <junegunn.c@gmail.com> | 2017-01-08 01:30:31 +0900 |
---|---|---|
committer | Junegunn Choi <junegunn.c@gmail.com> | 2017-01-08 02:09:56 +0900 |
commit | 1448d631a7c72905f62dbb343a8f231a1c3cc52c (patch) | |
tree | 05abfedd2a0777c2640c8259267d3ad855879dfe /src/util | |
parent | fd137a9e875ba1fd9feed4903e102951f8098c33 (diff) |
Add --height option
Diffstat (limited to 'src/util')
-rw-r--r-- | src/util/util.go | 16 | ||||
-rw-r--r-- | src/util/util_unix.go | 6 | ||||
-rw-r--r-- | src/util/util_windows.go | 6 |
3 files changed, 28 insertions, 0 deletions
diff --git a/src/util/util.go b/src/util/util.go index 2a1607ce..29e80176 100644 --- a/src/util/util.go +++ b/src/util/util.go @@ -6,8 +6,24 @@ import ( "time" "github.com/junegunn/go-isatty" + "github.com/junegunn/go-runewidth" ) +var _runeWidths = make(map[rune]int) + +// RuneWidth returns rune width +func RuneWidth(r rune, prefixWidth int, tabstop int) int { + if r == '\t' { + return tabstop - prefixWidth%tabstop + } else if w, found := _runeWidths[r]; found { + return w + } else { + w := runewidth.RuneWidth(r) + _runeWidths[r] = w + return w + } +} + // Max returns the largest integer func Max(first int, second int) int { if first >= second { diff --git a/src/util/util_unix.go b/src/util/util_unix.go index 29e0d30d..bc1b7b52 100644 --- a/src/util/util_unix.go +++ b/src/util/util_unix.go @@ -5,6 +5,7 @@ package util import ( "os" "os/exec" + "syscall" ) // ExecCommand executes the given command with $SHELL @@ -20,3 +21,8 @@ func ExecCommand(command string) *exec.Cmd { func IsWindows() bool { return false } + +// SetNonBlock executes syscall.SetNonblock on file descriptor +func SetNonblock(file *os.File, nonblock bool) { + syscall.SetNonblock(int(file.Fd()), nonblock) +} diff --git a/src/util/util_windows.go b/src/util/util_windows.go index 3aa86606..9ba4f79e 100644 --- a/src/util/util_windows.go +++ b/src/util/util_windows.go @@ -5,6 +5,7 @@ package util import ( "os" "os/exec" + "syscall" "github.com/junegunn/go-shellwords" ) @@ -26,3 +27,8 @@ func ExecCommand(command string) *exec.Cmd { func IsWindows() bool { return true } + +// SetNonBlock executes syscall.SetNonblock on file descriptor +func SetNonblock(file *os.File, nonblock bool) { + syscall.SetNonblock(syscall.Handle(file.Fd()), nonblock) +} |