2018-08-04 17:35:29 +00:00
|
|
|
/* Copyright (C) 2018 Curtis McEnroe <june@causal.agency>
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Affero 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 Affero General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Affero General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#define _XOPEN_SOURCE_EXTENDED
|
|
|
|
|
|
|
|
#include <curses.h>
|
|
|
|
#include <err.h>
|
|
|
|
#include <locale.h>
|
|
|
|
#include <stdarg.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <sysexits.h>
|
|
|
|
#include <wchar.h>
|
|
|
|
#include <wctype.h>
|
|
|
|
|
|
|
|
#include "chat.h"
|
|
|
|
|
2018-08-04 17:58:44 +00:00
|
|
|
#ifndef A_ITALIC
|
|
|
|
#define A_ITALIC A_NORMAL
|
|
|
|
#endif
|
|
|
|
|
2018-08-04 20:02:43 +00:00
|
|
|
#define MAX(a, b) ((a) > (b) ? (a) : (b))
|
2018-08-04 20:36:25 +00:00
|
|
|
#define MIN(a, b) ((a) < (b) ? (a) : (b))
|
2018-08-04 20:02:43 +00:00
|
|
|
|
2018-08-04 17:35:29 +00:00
|
|
|
static const int TOPIC_COLS = 512;
|
|
|
|
static const int INPUT_COLS = 512;
|
2018-08-04 21:59:43 +00:00
|
|
|
static const int LOG_LINES = 100;
|
2018-08-04 17:35:29 +00:00
|
|
|
|
|
|
|
static struct {
|
|
|
|
WINDOW *topic;
|
2018-08-04 21:59:43 +00:00
|
|
|
WINDOW *log;
|
2018-08-04 17:35:29 +00:00
|
|
|
WINDOW *input;
|
2018-08-04 20:02:43 +00:00
|
|
|
size_t cursor;
|
2018-08-04 17:35:29 +00:00
|
|
|
} ui;
|
|
|
|
|
|
|
|
void uiInit(void) {
|
|
|
|
setlocale(LC_CTYPE, "");
|
|
|
|
initscr();
|
|
|
|
cbreak();
|
|
|
|
noecho();
|
|
|
|
keypad(stdscr, true);
|
|
|
|
|
|
|
|
start_color();
|
|
|
|
use_default_colors();
|
|
|
|
for (short pair = 0; pair < 077; ++pair) {
|
|
|
|
if (pair < 010) {
|
|
|
|
init_pair(1 + pair, pair, -1);
|
|
|
|
} else {
|
|
|
|
init_pair(1 + pair, pair & 007, (pair & 070) >> 3);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ui.topic = newpad(2, TOPIC_COLS);
|
|
|
|
mvwhline(ui.topic, 1, 0, ACS_HLINE, TOPIC_COLS);
|
|
|
|
|
2018-08-04 21:59:43 +00:00
|
|
|
ui.log = newpad(LOG_LINES, COLS);
|
|
|
|
wsetscrreg(ui.log, 0, LOG_LINES - 1);
|
|
|
|
scrollok(ui.log, true);
|
|
|
|
wmove(ui.log, LOG_LINES - (LINES - 4) - 1, 0);
|
2018-08-04 17:35:29 +00:00
|
|
|
|
|
|
|
ui.input = newpad(2, INPUT_COLS);
|
|
|
|
mvwhline(ui.input, 0, 0, ACS_HLINE, INPUT_COLS);
|
2018-08-04 20:02:43 +00:00
|
|
|
wmove(ui.input, 1, ui.cursor);
|
2018-08-04 19:04:48 +00:00
|
|
|
nodelay(ui.input, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void uiResize(void) {
|
2018-08-04 21:59:43 +00:00
|
|
|
wresize(ui.log, LOG_LINES, COLS);
|
|
|
|
wmove(ui.log, LOG_LINES - 1, COLS - 1);
|
2018-08-04 17:35:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void uiHide(void) {
|
|
|
|
endwin();
|
|
|
|
}
|
|
|
|
|
|
|
|
void uiDraw(void) {
|
2018-08-04 20:02:43 +00:00
|
|
|
int lastCol = COLS - 1;
|
|
|
|
int lastLine = LINES - 1;
|
|
|
|
|
2018-08-04 21:08:15 +00:00
|
|
|
pnoutrefresh(
|
|
|
|
ui.topic,
|
|
|
|
0, 0,
|
|
|
|
0, 0,
|
|
|
|
1, lastCol
|
|
|
|
);
|
2018-08-04 17:35:29 +00:00
|
|
|
pnoutrefresh(
|
2018-08-04 21:59:43 +00:00
|
|
|
ui.log,
|
|
|
|
LOG_LINES - (LINES - 4), 0,
|
2018-08-04 21:08:15 +00:00
|
|
|
2, 0,
|
|
|
|
lastLine - 2, lastCol
|
2018-08-04 17:35:29 +00:00
|
|
|
);
|
|
|
|
pnoutrefresh(
|
|
|
|
ui.input,
|
2018-08-04 20:02:43 +00:00
|
|
|
0, MAX(0, ui.cursor - lastCol),
|
|
|
|
lastLine - 1, 0,
|
|
|
|
lastLine, lastCol
|
2018-08-04 17:35:29 +00:00
|
|
|
);
|
|
|
|
doupdate();
|
|
|
|
}
|
|
|
|
|
2018-08-04 20:36:25 +00:00
|
|
|
static const struct AttrColor {
|
2018-08-04 17:35:29 +00:00
|
|
|
attr_t attr;
|
2018-08-04 20:36:25 +00:00
|
|
|
short pair;
|
2018-08-04 17:35:29 +00:00
|
|
|
} MIRC_COLORS[16] = {
|
|
|
|
{ A_BOLD, COLOR_WHITE }, // white
|
|
|
|
{ A_NORMAL, COLOR_BLACK }, // black
|
|
|
|
{ A_NORMAL, COLOR_BLUE }, // blue
|
|
|
|
{ A_NORMAL, COLOR_GREEN }, // green
|
|
|
|
{ A_BOLD, COLOR_RED }, // red
|
|
|
|
{ A_NORMAL, COLOR_RED }, // "brown"
|
|
|
|
{ A_NORMAL, COLOR_MAGENTA }, // magenta
|
|
|
|
{ A_NORMAL, COLOR_YELLOW }, // "orange"
|
|
|
|
{ A_BOLD, COLOR_YELLOW }, // yellow
|
|
|
|
{ A_BOLD, COLOR_GREEN }, // light green
|
|
|
|
{ A_NORMAL, COLOR_CYAN }, // cyan
|
|
|
|
{ A_BOLD, COLOR_CYAN }, // light cyan
|
|
|
|
{ A_BOLD, COLOR_BLUE }, // light blue
|
|
|
|
{ A_BOLD, COLOR_MAGENTA }, // "pink"
|
|
|
|
{ A_BOLD, COLOR_BLACK }, // grey
|
|
|
|
{ A_NORMAL, COLOR_WHITE }, // light grey
|
|
|
|
};
|
|
|
|
|
2018-08-04 20:36:25 +00:00
|
|
|
static const char *parseColor(struct AttrColor *color, const char *str) {
|
|
|
|
short fg = 0;
|
|
|
|
size_t fgLen = MIN(strspn(str, "0123456789"), 2);
|
|
|
|
if (!fgLen) {
|
|
|
|
color->attr = A_NORMAL;
|
|
|
|
color->pair = -1;
|
|
|
|
return str;
|
|
|
|
}
|
|
|
|
for (size_t i = 0; i < fgLen; ++i) {
|
|
|
|
fg *= 10;
|
|
|
|
fg += str[i] - '0';
|
|
|
|
}
|
|
|
|
str = &str[fgLen];
|
|
|
|
|
|
|
|
short bg = 0;
|
|
|
|
size_t bgLen = 0;
|
|
|
|
if (str[0] == ',') {
|
|
|
|
bgLen = MIN(strspn(&str[1], "0123456789"), 2);
|
|
|
|
}
|
|
|
|
for (size_t i = 0; i < bgLen; ++i) {
|
|
|
|
bg *= 10;
|
|
|
|
bg += str[1 + i] - '0';
|
|
|
|
}
|
|
|
|
if (bgLen) str = &str[1 + bgLen];
|
|
|
|
|
|
|
|
if (color->pair == -1) color->pair = 0;
|
|
|
|
color->attr = MIRC_COLORS[fg].attr;
|
|
|
|
color->pair = (color->pair & 070) | MIRC_COLORS[fg].pair;
|
|
|
|
if (bgLen) {
|
|
|
|
color->pair = (color->pair & 007) | (MIRC_COLORS[bg].pair << 3);
|
|
|
|
}
|
|
|
|
|
|
|
|
return str;
|
|
|
|
}
|
|
|
|
|
2018-08-04 17:35:29 +00:00
|
|
|
static void uiAdd(WINDOW *win, const char *str) {
|
|
|
|
attr_t attr = A_NORMAL;
|
2018-08-04 20:36:25 +00:00
|
|
|
struct AttrColor color = { A_NORMAL, -1 };
|
2018-08-04 17:35:29 +00:00
|
|
|
for (;;) {
|
|
|
|
size_t cc = strcspn(str, "\2\3\35\37");
|
2018-08-04 20:36:25 +00:00
|
|
|
wattr_set(win, attr | color.attr, 1 + color.pair, NULL);
|
2018-08-04 17:35:29 +00:00
|
|
|
waddnstr(win, str, cc);
|
|
|
|
if (!str[cc]) break;
|
|
|
|
|
|
|
|
str = &str[cc];
|
|
|
|
switch (*str++) {
|
2018-08-04 20:36:25 +00:00
|
|
|
break; case '\2': attr ^= A_BOLD;
|
|
|
|
break; case '\3': str = parseColor(&color, str);
|
2018-08-04 17:35:29 +00:00
|
|
|
break; case '\35': attr ^= A_ITALIC;
|
|
|
|
break; case '\37': attr ^= A_UNDERLINE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void uiTopic(const char *topic) {
|
|
|
|
wmove(ui.topic, 0, 0);
|
|
|
|
wclrtoeol(ui.topic);
|
|
|
|
uiAdd(ui.topic, topic);
|
|
|
|
}
|
|
|
|
|
2018-08-04 21:59:43 +00:00
|
|
|
void uiLog(const char *line) {
|
|
|
|
waddch(ui.log, '\n');
|
|
|
|
uiAdd(ui.log, line);
|
2018-08-04 17:35:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void uiFmt(const char *format, ...) {
|
|
|
|
char *buf;
|
|
|
|
va_list ap;
|
|
|
|
va_start(ap, format);
|
|
|
|
vasprintf(&buf, format, ap);
|
|
|
|
va_end(ap);
|
|
|
|
if (!buf) err(EX_OSERR, "vasprintf");
|
2018-08-04 21:59:43 +00:00
|
|
|
uiLog(buf);
|
2018-08-04 17:35:29 +00:00
|
|
|
free(buf);
|
|
|
|
}
|
|
|
|
|
|
|
|
void uiRead(void) {
|
|
|
|
static wchar_t buf[512];
|
|
|
|
static size_t len;
|
|
|
|
|
|
|
|
wint_t ch;
|
2018-08-04 19:04:48 +00:00
|
|
|
while (wget_wch(ui.input, &ch) != ERR) {
|
|
|
|
switch (ch) {
|
|
|
|
break; case KEY_RESIZE: uiResize();
|
|
|
|
break; case '\b': case '\177': {
|
|
|
|
if (len) len--;
|
|
|
|
}
|
|
|
|
break; case '\n': {
|
|
|
|
if (!len) break;
|
|
|
|
buf[len] = '\0';
|
|
|
|
input(buf);
|
|
|
|
len = 0;
|
|
|
|
}
|
|
|
|
break; default: {
|
2018-08-04 20:02:43 +00:00
|
|
|
// TODO: Check overflow
|
2018-08-04 19:04:48 +00:00
|
|
|
if (iswprint(ch)) buf[len++] = ch;
|
|
|
|
}
|
2018-08-04 17:35:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
wmove(ui.input, 1, 0);
|
|
|
|
waddnwstr(ui.input, buf, len);
|
|
|
|
wclrtoeol(ui.input);
|
2018-08-04 20:02:43 +00:00
|
|
|
ui.cursor = len;
|
2018-08-04 17:35:29 +00:00
|
|
|
}
|