-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconsole.go
More file actions
75 lines (63 loc) · 1.73 KB
/
console.go
File metadata and controls
75 lines (63 loc) · 1.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
// This command will report on the width and/or hight of the console
package main
import (
"fmt"
"flag"
"syscall"
"unsafe"
)
/******************************************************************************/
// MARK: Structures
type win_size struct {
Row uint16
Col uint16
Xpixel uint16
Ypixel uint16
}
/******************************************************************************/
// MARK: - Functions
func GetWidth() int {
ws := &win_size{}
retCode, _, errno := syscall.Syscall(syscall.SYS_IOCTL,
uintptr(syscall.Stdin),
uintptr(syscall.TIOCGWINSZ),
uintptr(unsafe.Pointer(ws)))
if int(retCode) == -1 {
panic(errno)
}
return int(ws.Col)
}
func GetHeight() int {
ws := &win_size{}
retCode, _, errno := syscall.Syscall(syscall.SYS_IOCTL,
uintptr(syscall.Stdin),
uintptr(syscall.TIOCGWINSZ),
uintptr(unsafe.Pointer(ws)))
if int(retCode) == -1 {
panic(errno)
}
return int(ws.Row)
}
func MaxInt(left, right int) int {
if left<right {
return right
}
return left
}
/******************************************************************************/
// MARK: - Application
func main() {
heightMode := flag.Bool("height", false, "Height mode")
widthMode := flag.Bool("width", false, "Width mode")
adjust := flag.Int("adjust", 0, "Value to add to height or width")
flag.Parse()
if *heightMode {
fmt.Printf("%d\n", MaxInt(0, GetHeight() + *adjust))
} else if *widthMode {
fmt.Printf("%d\n", MaxInt(0, GetWidth() + *adjust))
} else {
fmt.Printf("%dx%d\n",
MaxInt(0, GetWidth() + *adjust),
MaxInt(0, GetHeight() + *adjust))
}
}