-
Notifications
You must be signed in to change notification settings - Fork 0
/
ncurses.c
202 lines (167 loc) · 2.96 KB
/
ncurses.c
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
#include <ncurses.h>
#include <signal.h>
#include <ctype.h>
#include <errno.h>
#include "ncurses.h"
#include "macros.h"
void nc_init()
{
static int init = 0;
if(!init){
init = 1;
initscr();
noecho();
cbreak();
raw();
scrollok(stdscr, 1);
nonl();
intrflush(stdscr, 0);
set_escdelay(0);
if(has_colors()){
start_color();
use_default_colors();
init_pair(1 + COLOR_BLACK, COLOR_BLACK, -1);
init_pair(1 + COLOR_GREEN, COLOR_GREEN, -1);
init_pair(1 + COLOR_WHITE, COLOR_WHITE, -1);
init_pair(1 + COLOR_RED, COLOR_RED, -1);
init_pair(1 + COLOR_CYAN, COLOR_CYAN, -1);
init_pair(1 + COLOR_MAGENTA, COLOR_MAGENTA, -1);
init_pair(1 + COLOR_BLUE, COLOR_BLUE, -1);
init_pair(1 + COLOR_YELLOW, COLOR_YELLOW, -1);
init_pair(9 + COLOR_BLACK, -1, COLOR_BLACK);
init_pair(9 + COLOR_GREEN, -1, COLOR_GREEN);
init_pair(9 + COLOR_WHITE, -1, COLOR_WHITE);
init_pair(9 + COLOR_RED, -1, COLOR_RED);
init_pair(9 + COLOR_CYAN, -1, COLOR_CYAN);
init_pair(9 + COLOR_MAGENTA, -1, COLOR_MAGENTA);
init_pair(9 + COLOR_BLUE, -1, COLOR_BLUE);
init_pair(9 + COLOR_YELLOW, -1, COLOR_YELLOW);
}
}else{
refresh();
}
}
void nc_term()
{
endwin();
}
void nc_clearall()
{
clear();
}
void nc_highlight(int on)
{
(on ? attron : attroff)(A_REVERSE);
}
void nc_status(const char *fmt, int right)
{
scrollok(stdscr, 0);
int x = 0;
if(right)
x = COLS / 2;
move(LINES - 1, x);
clrtoeol();
addstr(fmt);
scrollok(stdscr, 1);
}
void nc_get_yx(int *y, int *x)
{
getyx(stdscr, *y, *x);
}
void nc_set_yx(int y, int x)
{
move(y, x);
}
int nc_charlen(int ch)
{
int l = 1;
switch(ch){
case '\r':
l++; /* ^M */
break;
case '\t':
break;
default:
if(!isprint(ch))
l++; /* ^X */
}
return l;
}
int nc_getch(bool mapraw, bool *wasraw)
{
/* TODO: interrupts, winch */
bool ctrl_v = false;
int ch;
restart:
errno = 0;
ch = getch();
if(ch < 0 && errno == EINTR)
goto restart;
if(ctrl_v)
return ch;
if(ch == CTRL_AND('z')){
raise(SIGTSTP);
goto restart;
}
if(mapraw){
if(ch == '\r'){
ch = '\n';
}else if(ch == CTRL_AND('v')){
if(wasraw)
*wasraw = true;
ctrl_v = true;
goto restart;
}
if(wasraw)
*wasraw = false;
}
return ch;
}
void nc_addch(char c)
{
if(c == '\t'){
c = ' ';
}else if(c == '\r'){
/* TODO: all non-printables */
addch('^');
addch('M');
return;
}
addch(c & 0xff);
}
void nc_style(enum nc_style s)
{
int to_set = A_NORMAL;
int col = -1;
if(s & COL_BLUE)
col = COLOR_BLUE;
else if(s & COL_BROWN)
col = COLOR_YELLOW;
if(col != -1)
to_set |= COLOR_PAIR(1 + col);
if(s & COL_BG_RED)
to_set |= COLOR_PAIR(9 + COLOR_RED);
if(s & ATTR_BOLD)
to_set |= A_BOLD;
attrset(to_set);
}
void nc_addstr(const char *s)
{
addstr(s);
}
void nc_vprintf(const char *fmt, va_list l)
{
vwprintw(stdscr, fmt, l);
}
int nc_LINES(void)
{
return LINES;
}
int nc_COLS(void)
{
return COLS;
}
void nc_clrtoeol()
{
clrtoeol();
}