added sysctl for terminal-width retrieval

This commit is contained in:
Özgür Kesim 2015-07-28 17:12:26 +02:00
parent 539175e639
commit f251231590
2 changed files with 41 additions and 3 deletions

15
dict.go
View File

@ -16,6 +16,9 @@ const (
SUFF = ");" SUFF = ");"
) )
// is going to be set in winsize.go, if run on unix
var Width = 80
func main() { func main() {
if len(os.Args) < 2 || os.Args[1][0] == '-' { if len(os.Args) < 2 || os.Args[1][0] == '-' {
println("usage:", os.Args[0], "word [{de|es|fr|en...}{de|es|fr|en|...}]") println("usage:", os.Args[0], "word [{de|es|fr|en...}{de|es|fr|en|...}]")
@ -59,7 +62,7 @@ func main() {
return return
} }
ml := 5 ml := 25
for _, v := range right { for _, v := range right {
if len(v) > ml { if len(v) > ml {
@ -67,8 +70,8 @@ func main() {
} }
} }
if ml > 40 { if ml > (Width / 2) {
ml = 40 ml = Width / 2
} }
re := regexp.MustCompile(`\\(['"„])`) re := regexp.MustCompile(`\\(['"„])`)
@ -86,6 +89,12 @@ func main() {
a = a[i:] 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 // TODO: wrap when len(a) > ml
fmt.Printf("%*s → %s\n", ml, a, b) fmt.Printf("%*s → %s\n", ml, a, b)

29
winsize.go Normal file
View File

@ -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)
}