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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
|
/* Copyright (C) 2020 C. McEnroe <june@causal.agency>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#include <assert.h>
#include <ctype.h>
#include <curses.h>
#include <err.h>
#include <stdarg.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sysexits.h>
#include <time.h>
#include <wchar.h>
#include <wctype.h>
#include "chat.h"
#ifndef A_ITALIC
#define A_ITALIC A_UNDERLINE
#endif
#define BOTTOM (LINES - 1)
#define RIGHT (COLS - 1)
#define WINDOW_LINES (LINES - 2)
static short colorPairs;
static void colorInit(void) {
start_color();
use_default_colors();
for (short pair = 0; pair < 16; ++pair) {
init_pair(1 + pair, pair % COLORS, -1);
}
colorPairs = 17;
}
static attr_t colorAttr(short fg) {
return (fg >= COLORS ? A_BOLD : A_NORMAL);
}
static short colorPair(short fg, short bg) {
if (bg == -1) return 1 + fg;
for (short pair = 17; pair < colorPairs; ++pair) {
short f, b;
pair_content(pair, &f, &b);
if (f == fg && b == bg) return pair;
}
init_pair(colorPairs, fg % COLORS, bg % COLORS);
return colorPairs++;
}
enum {
InputCols = 512,
PadLines = 512,
};
static WINDOW *status;
static WINDOW *input;
struct Window {
size_t id;
WINDOW *pad;
enum Heat heat;
int unread;
int scroll;
bool mark;
struct Window *prev;
struct Window *next;
};
static struct {
struct Window *active;
struct Window *other;
struct Window *head;
struct Window *tail;
} windows;
static void windowAdd(struct Window *window) {
if (windows.tail) windows.tail->next = window;
window->prev = windows.tail;
window->next = NULL;
windows.tail = window;
if (!windows.head) windows.head = window;
}
static void windowRemove(struct Window *window) {
if (window->prev) window->prev->next = window->next;
if (window->next) window->next->prev = window->prev;
if (windows.head == window) windows.head = window->next;
if (windows.tail == window) windows.tail = window->prev;
}
static struct Window *windowFor(size_t id) {
struct Window *window;
for (window = windows.head; window; window = window->next) {
if (window->id == id) return window;
}
window = malloc(sizeof(*window));
if (!window) err(EX_OSERR, "malloc");
window->id = id;
window->pad = newpad(PadLines, COLS);
scrollok(window->pad, true);
wmove(window->pad, PadLines - 1, 0);
window->heat = Cold;
window->unread = 0;
window->scroll = PadLines;
window->mark = true;
windowAdd(window);
return window;
}
static void errExit(int eval) {
(void)eval;
reset_shell_mode();
}
void uiInit(void) {
initscr();
cbreak();
noecho();
termInit();
termNoFlow();
def_prog_mode();
err_set_exit(errExit);
colorInit();
status = newwin(1, COLS, 0, 0);
input = newpad(1, InputCols);
keypad(input, true);
nodelay(input, true);
windows.active = windowFor(Network);
}
void uiDraw(void) {
wnoutrefresh(status);
pnoutrefresh(
windows.active->pad,
windows.active->scroll - WINDOW_LINES, 0,
1, 0,
BOTTOM - 1, RIGHT
);
// TODO: Input scrolling.
pnoutrefresh(
input,
0, 0,
BOTTOM, 0,
BOTTOM, RIGHT
);
doupdate();
}
struct Style {
attr_t attr;
enum Color fg, bg;
};
static const struct Style Reset = { A_NORMAL, Default, Default };
static short mapColor(enum Color color) {
switch (color) {
break; case White: return 8 + COLOR_WHITE;
break; case Black: return 0 + COLOR_BLACK;
break; case Blue: return 0 + COLOR_BLUE;
break; case Green: return 0 + COLOR_GREEN;
break; case Red: return 8 + COLOR_RED;
break; case Brown: return 0 + COLOR_RED;
break; case Magenta: return 0 + COLOR_MAGENTA;
break; case Orange: return 0 + COLOR_YELLOW;
break; case Yellow: return 8 + COLOR_YELLOW;
break; case LightGreen: return 8 + COLOR_GREEN;
break; case Cyan: return 0 + COLOR_CYAN;
break; case LightCyan: return 8 + COLOR_CYAN;
break; case LightBlue: return 8 + COLOR_BLUE;
break; case Pink: return 8 + COLOR_MAGENTA;
break; case Gray: return 8 + COLOR_BLACK;
break; case LightGray: return 0 + COLOR_WHITE;
break; default: return -1;
}
}
static void styleParse(struct Style *style, const char **str, size_t *len) {
switch (**str) {
break; case '\2': (*str)++; style->attr ^= A_BOLD;
break; case '\17': (*str)++; *style = Reset;
break; case '\26': (*str)++; style->attr ^= A_REVERSE;
break; case '\35': (*str)++; style->attr ^= A_ITALIC;
break; case '\37': (*str)++; style->attr ^= A_UNDERLINE;
break; case '\3': {
(*str)++;
if (!isdigit(**str)) {
style->fg = Default;
style->bg = Default;
break;
}
style->fg = *(*str)++ - '0';
if (isdigit(**str)) style->fg = style->fg * 10 + *(*str)++ - '0';
if ((*str)[0] != ',' || !isdigit((*str)[1])) break;
(*str)++;
style->bg = *(*str)++ - '0';
if (isdigit(**str)) style->bg = style->bg * 10 + *(*str)++ - '0';
}
}
*len = strcspn(*str, "\2\3\17\26\35\37");
}
static int wordWidth(const char *str) {
size_t len = strcspn(str, " ");
int width = 0;
while (len) {
wchar_t wc;
int n = mbtowc(&wc, str, len);
if (n < 1) return width + len;
width += (iswprint(wc) ? wcwidth(wc) : 0);
str += n;
len -= n;
}
return width;
}
static void styleAdd(WINDOW *win, const char *str) {
int y, x, width;
getmaxyx(win, y, width);
size_t len;
struct Style style = Reset;
while (*str) {
if (*str == ' ') {
getyx(win, y, x);
const char *word = &str[strspn(str, " ")];
if (width - x - 1 <= wordWidth(word)) {
waddch(win, '\n');
str = word;
}
}
styleParse(&style, &str, &len);
size_t sp = strspn(str, " ");
sp += strcspn(&str[sp], " ");
if (sp < len) len = sp;
wattr_set(
win,
style.attr | colorAttr(mapColor(style.fg)),
colorPair(mapColor(style.fg), mapColor(style.bg)),
NULL
);
waddnstr(win, str, len);
str += len;
}
}
static void statusUpdate(void) {
wmove(status, 0, 0);
int num;
const struct Window *window;
for (num = 0, window = windows.head; window; ++num, window = window->next) {
if (!window->unread && window != windows.active) continue;
enum Color color = hash(idNames[window->id]); // FIXME: queries.
int unread;
char buf[256];
snprintf(
buf, sizeof(buf), C"%d%s %d %s %n("C"%02d%d"C"%d) ",
color, (window == windows.active ? V : ""),
num, idNames[window->id],
&unread, (window->heat > Warm ? White : color), window->unread,
color
);
if (!window->unread) buf[unread] = '\0';
styleAdd(status, buf);
}
wclrtoeol(status);
}
void uiShowID(size_t id) {
struct Window *window = windowFor(id);
window->heat = Cold;
window->unread = 0;
window->mark = false;
if (windows.active) windows.active->mark = true;
windows.other = windows.active;
windows.active = window;
touchwin(window->pad);
statusUpdate();
}
void uiWrite(size_t id, enum Heat heat, const struct tm *time, const char *str) {
(void)time;
struct Window *window = windowFor(id);
waddch(window->pad, '\n');
styleAdd(window->pad, str);
}
void uiFormat(
size_t id, enum Heat heat, const struct tm *time, const char *format, ...
) {
char buf[1024];
va_list ap;
va_start(ap, format);
int len = vsnprintf(buf, sizeof(buf), format, ap);
va_end(ap);
assert((size_t)len < sizeof(buf));
uiWrite(id, heat, time, buf);
}
|