From f251231590b188681b33705feaf5156d88f09197 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=96zg=C3=BCr=20Kesim?= Date: Tue, 28 Jul 2015 17:12:26 +0200 Subject: [PATCH] added sysctl for terminal-width retrieval --- dict.go | 15 ++++++++++++--- winsize.go | 29 +++++++++++++++++++++++++++++ 2 files changed, 41 insertions(+), 3 deletions(-) create mode 100644 winsize.go diff --git a/dict.go b/dict.go index f1d3a01..20b109b 100644 --- a/dict.go +++ b/dict.go @@ -16,6 +16,9 @@ const ( SUFF = ");" ) +// is going to be set in winsize.go, if run on unix +var Width = 80 + func main() { if len(os.Args) < 2 || os.Args[1][0] == '-' { println("usage:", os.Args[0], "word [{de|es|fr|en...}{de|es|fr|en|...}]") @@ -59,7 +62,7 @@ func main() { return } - ml := 5 + ml := 25 for _, v := range right { if len(v) > ml { @@ -67,8 +70,8 @@ func main() { } } - if ml > 40 { - ml = 40 + if ml > (Width / 2) { + ml = Width / 2 } re := regexp.MustCompile(`\\(['"„])`) @@ -86,6 +89,12 @@ func main() { a = a[i:] } } + if len(b) > ml { + if i := strings.LastIndex(b[:ml], " "); i > 0 { + fmt.Printf("%*s\n", ml, a[:i]) + a = a[i:] + } + } // TODO: wrap when len(a) > ml fmt.Printf("%*s → %s\n", ml, a, b) diff --git a/winsize.go b/winsize.go new file mode 100644 index 0000000..1a3bdaf --- /dev/null +++ b/winsize.go @@ -0,0 +1,29 @@ +package main + +// +build linux + +import ( + "syscall" + "unsafe" +) + +type winsize struct { + Row uint16 + Col uint16 + Xpixel uint16 + Ypixel uint16 +} + +func init() { + ws := &winsize{} + retCode, _, errno := syscall.Syscall(syscall.SYS_IOCTL, + uintptr(syscall.Stdin), + uintptr(syscall.TIOCGWINSZ), + uintptr(unsafe.Pointer(ws))) + + if int(retCode) == -1 { + panic(errno) + } + + Width = int(ws.Col) +}