forked from lye/curses
/
curses.go
254 lines (206 loc) · 5.46 KB
/
curses.go
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
package curses
// #cgo LDFLAGS: -lncurses
// #include <stdlib.h>
// #include <ncurses.h>
// void Printw (const char* str) { printw (str); }
// void Wprintw (WINDOW* win, const char* str) { wprintw (win, str); }
// void Mvprintw (int y, int x, const char* str) { mvprintw (y, x, str); }
// void Mvwprintw (WINDOW* win, int y, int x, const char* str) { mvwprintw (win, y, x, str); }
// void go_attr_get(int *attrs, short *colorpair) { attr_get((attr_t*)attrs, colorpair, NULL); }
import "C"
import (
"fmt"
"time"
"unsafe"
)
// Curses window type.
type Window struct {
cwin *C.WINDOW
}
// Standard window.
var Stdscr *Window = &Window{C.stdscr}
// Initializes curses.
// This function should be called before using the package.
func Initscr() *Window {
Stdscr = &Window{C.initscr()}
return Stdscr
}
// Raw input. No buffering.
// CTRL+Z and CTRL+C passed to the application.
func Raw() {
C.raw()
}
// No buffering.
func Cbreak() {
C.cbreak()
}
// Enable character echoing while reading.
func Echo() {
C.echo()
}
// Disables character echoing while reading.
func Noecho() {
C.noecho()
}
// Copies blanks to every position on the window.
func Erase() {
C.erase()
}
func (win *Window) Erase() {
C.werase(win.cwin)
}
// Copies blanks to every position on the window buffer.
func Clear() {
C.clear()
}
func (win *Window) Clear() {
C.wclear(win.cwin)
}
// Sets global read timeout; if negative input will be blocking.
func Timeout(d time.Duration) {
C.timeout(C.int(d / time.Millisecond))
}
// Sets read timeout for specific window; if negative input will block.
func (win *Window) Timeout(d time.Duration) {
C.wtimeout(win.cwin, C.int(d/time.Millisecond))
}
// Enable reading of function keys.
func (win *Window) Keypad(on bool) {
C.keypad(win.cwin, C.bool(on))
}
// Get char from the standard in.
func (win *Window) Getch() int {
return int(C.wgetch(win.cwin))
}
// Get char from the standard in.
func Getch() int {
return int(C.getch())
}
// Enable attribute
func Attron(attr int) {
C.attron(C.int(attr))
}
// Disable attribute
func Attroff(attr int) {
C.attroff(C.int(attr))
}
// Set attribute
func Attrset(attr int) {
C.attrset(C.int(attr))
}
// Get current attributes
func Attrget() (int, ColorPair) {
var attrs C.int
var color C.short
C.go_attr_get(&attrs, &color)
return int(attrs), ColorPair(color)
}
func (win *Window) Attron(attr int) {
C.wattron(win.cwin, C.int(attr))
}
func (win *Window) Attroff(attr int) {
C.wattroff(win.cwin, C.int(attr))
}
func (win *Window) Attrset(attr int) {
win.cwin._attrs = C.attr_t(attr)
}
// Refresh screen.
func Refresh() {
C.refresh()
}
// Refresh given window.
func (win *Window) Refresh() {
C.wrefresh(win.cwin)
}
// Finalizes curses.
func End() {
C.endwin()
}
// Create new window.
func NewWindow(height, width, starty, startx int) *Window {
return &Window{C.newwin(C.int(height), C.int(width),
C.int(starty), C.int(startx))}
}
// Set box lines.
func (win *Window) Box(v, h int) {
C.box(win.cwin, C.chtype(v), C.chtype(h))
}
// Set border characters.
// 1. ls: character to be used for the left side of the window
// 2. rs: character to be used for the right side of the window
// 3. ts: character to be used for the top side of the window
// 4. bs: character to be used for the bottom side of the window
// 5. tl: character to be used for the top left corner of the window
// 6. tr: character to be used for the top right corner of the window
// 7. bl: character to be used for the bottom left corner of the window
// 8. br: character to be used for the bottom right corner of the window
func (win *Window) Border(ls, rs, ts, bs, tl, tr, bl, br int) {
C.wborder(win.cwin, C.chtype(ls), C.chtype(rs), C.chtype(ts), C.chtype(bs), C.chtype(tl), C.chtype(tr), C.chtype(bl), C.chtype(br))
}
// Delete current window.
func (win *Window) Del() {
C.delwin(win.cwin)
}
// Get windows sizes.
func (win *Window) Getmaxyx() (row, col int) {
row = int(win.cwin._maxx)
col = int(win.cwin._maxy)
return row, col
}
func (win *Window) Setscrreg(top, bot int) {
C.wsetscrreg(win.cwin, C.int(top), C.int(bot))
}
func Addstr(str ...interface{}) {
res := (*C.char)(C.CString(fmt.Sprint(str...)))
defer C.free(unsafe.Pointer(res))
C.addstr(res)
}
func Mvaddstr(y, x int, str ...interface{}) {
res := (*C.char)(C.CString(fmt.Sprint(str...)))
defer C.free(unsafe.Pointer(res))
C.mvaddstr(C.int(y), C.int(x), res)
}
func Addch(ch int) {
C.addch(C.chtype(ch))
}
func Mvaddch(y, x int, ch int) {
C.mvaddch(C.int(y), C.int(x), C.chtype(ch))
}
func (win *Window) Addstr(str ...interface{}) {
res := (*C.char)(C.CString(fmt.Sprint(str...)))
defer C.free(unsafe.Pointer(res))
C.waddstr(win.cwin, res)
}
func (win *Window) Mvaddstr(y, x int, str ...interface{}) {
res := (*C.char)(C.CString(fmt.Sprint(str...)))
defer C.free(unsafe.Pointer(res))
C.mvwaddstr(win.cwin, C.int(y), C.int(x), res)
}
func (win *Window) Addch(ch int) {
C.waddch(win.cwin, C.chtype(ch))
}
func (win *Window) Mvaddch(y, x int, ch int) {
C.mvwaddch(win.cwin, C.int(y), C.int(x), C.chtype(ch))
}
// Hardware insert/delete feature.
func (win *Window) Idlok(bf bool) {
C.idlok(win.cwin, C.bool(bf))
}
// Enable window scrolling.
func (win *Window) Scrollok(bf bool) {
C.scrollok(win.cwin, C.bool(bf))
}
// Scroll given window.
func (win *Window) Scroll() {
C.scroll(win.cwin)
}
// Get terminal size.
func Getmaxyx() (row, col int) {
row = int(C.LINES)
col = int(C.COLS)
return row, col
}
// Erases content from cursor to end of line inclusive.
func (win *Window) Clrtoeol() {
C.wclrtoeol(win.cwin)
}