-
Notifications
You must be signed in to change notification settings - Fork 2
/
ncurses.go
106 lines (92 loc) · 2.52 KB
/
ncurses.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
// Licensed under the GPL-v3
// Copyright: Sebastian Tilders <[email protected]> (c) 2019
/*
Provides an API binding for the TUI-Library libncurses.
The binding is designed to be used in multithreaded environments and therefore uses
an command channel to communicate with the ncurses library.
*/
package ncurses
import (
// #cgo LDFLAGS: -lncursesw
// #include <binding.h>
// #include <curses.h>
// #include <locale.h>
"C"
"errors"
)
// checks if ncurses is initialized
var initialized bool = false
type winList map[string]*Window
// Keeps track of allocated windows
var wins winList = make(winList)
func (wl winList) append(w *Window) {
wl[w.name] = w
}
func (wl winList) delete(w *Window) {
delete(wl,w.name)
}
// Initializes the ncurses library and install cleanup functions. The function returns a new Term struct.
// After calling this method no outputs should be done with fmt.Print*. Make sure to call Term.Endwin() before making any output with fmt.Print*.
func Initscr() (*Window,error) {
if(initialized) {
return nil,errors.New("Already initialized!")
}
C.bind_set_locale()
C.initscr()
w := &Window{
name: "stdscr",
chandle: C.bind_get_stdscr(),
begin: Position{0,0},
AutoRefresh: false,
}
C.keypad((*C.struct__win_st)(w.chandle),true)
C.move(0,0)
initialized = true
w.GetMaxYX()
wins.append(w)
go processCommands()
return w,nil;
}
// Closes an initialized terminal. Must be called before the program is about to exit.
func Endwin() error {
if !initialized {
return errors.New("Not initialized")
}
for _,w := range(wins) {
wins.delete(w)
}
close(GetComChannel())
C.endwin();
initialized = false
return nil;
}
// If enabled, typed runes are printed to terminal.
func SetEcho(on bool) error {
if !initialized {
return errors.New("Not initialized")
}
if on {
C.echo()
} else {
C.noecho()
}
return nil
}
// Defines visibility of the terminal cursor.
type CursorVisibility uint8
const (
// Cursor is hidden
CURSOR_HIDDEN CursorVisibility = iota
// Cursor is visible
CURSOR_VISIBLE
// Cursor is visible and highlighted (not supported on all terminals)
CURSOR_HIGHTLIGHTED
)
// Sets visiblity of the terminal cursor.
func SetCursor(choice CursorVisibility) error {
if !initialized {
return errors.New("Not initialized")
}
C.curs_set(C.int(choice))
return nil
}