added sysctl for terminal-width retrieval
This commit is contained in:
parent
539175e639
commit
f251231590
15
dict.go
15
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)
|
||||
|
29
winsize.go
Normal file
29
winsize.go
Normal 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)
|
||||
}
|
Loading…
Reference in New Issue
Block a user