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>
|
2018-08-08 04:55:45 +00:00
|
|
|
#include <stdbool.h>
|
2018-08-04 17:35:29 +00:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <sysexits.h>
|
|
|
|
#include <wchar.h>
|
|
|
|
#include <wctype.h>
|
|
|
|
|
2018-08-11 23:30:30 +00:00
|
|
|
#ifndef A_ITALIC
|
|
|
|
#define A_ITALIC A_NORMAL
|
|
|
|
#endif
|
|
|
|
|
2018-08-04 17:35:29 +00:00
|
|
|
#include "chat.h"
|
2018-08-07 18:11:19 +00:00
|
|
|
#undef uiFmt
|
2018-08-04 17:35:29 +00:00
|
|
|
|
2018-08-05 18:59:51 +00:00
|
|
|
#define MIN(a, b) ((a) < (b) ? (a) : (b))
|
|
|
|
#define MAX(a, b) ((a) > (b) ? (a) : (b))
|
|
|
|
|
2018-08-08 20:59:26 +00:00
|
|
|
#define CTRL(c) ((c) & 037)
|
2018-08-08 04:55:45 +00:00
|
|
|
|
2018-08-05 18:59:51 +00:00
|
|
|
static void colorInit(void) {
|
|
|
|
start_color();
|
|
|
|
use_default_colors();
|
|
|
|
if (COLORS >= 16) {
|
2018-08-05 22:34:35 +00:00
|
|
|
for (short pair = 0; pair < 0x100; ++pair) {
|
2018-08-05 18:59:51 +00:00
|
|
|
if (pair < 0x10) {
|
|
|
|
init_pair(1 + pair, pair, -1);
|
|
|
|
} else {
|
|
|
|
init_pair(1 + pair, pair & 0x0F, (pair & 0xF0) >> 4);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
2018-08-05 22:34:35 +00:00
|
|
|
for (short pair = 0; pair < 0100; ++pair) {
|
2018-08-05 18:59:51 +00:00
|
|
|
if (pair < 010) {
|
|
|
|
init_pair(1 + pair, pair, -1);
|
|
|
|
} else {
|
|
|
|
init_pair(1 + pair, pair & 007, (pair & 070) >> 3);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-08-05 00:54:50 +00:00
|
|
|
|
2018-08-05 18:59:51 +00:00
|
|
|
static attr_t attr8(short pair) {
|
|
|
|
if (COLORS >= 16 || pair < 0) return A_NORMAL;
|
|
|
|
return (pair & 0x08) ? A_BOLD : A_NORMAL;
|
|
|
|
}
|
|
|
|
static short pair8(short pair) {
|
|
|
|
if (COLORS >= 16 || pair < 0) return pair;
|
|
|
|
return (pair & 0x70) >> 1 | (pair & 0x07);
|
|
|
|
}
|
2018-08-04 20:02:43 +00:00
|
|
|
|
2018-08-13 17:49:03 +00:00
|
|
|
struct View {
|
2018-08-11 23:30:30 +00:00
|
|
|
struct Tag tag;
|
|
|
|
WINDOW *topic;
|
|
|
|
WINDOW *log;
|
|
|
|
int scroll;
|
2018-08-17 18:00:08 +00:00
|
|
|
int unread;
|
|
|
|
bool hot;
|
2018-08-11 23:30:30 +00:00
|
|
|
bool mark;
|
|
|
|
struct View *prev;
|
|
|
|
struct View *next;
|
2018-08-13 17:49:03 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
static struct {
|
|
|
|
struct View *head;
|
|
|
|
struct View *tail;
|
|
|
|
struct View *tags[TAGS_LEN];
|
|
|
|
} views;
|
2018-08-11 23:30:30 +00:00
|
|
|
|
|
|
|
static void viewAppend(struct View *view) {
|
2018-08-13 17:49:03 +00:00
|
|
|
if (views.tail) views.tail->next = view;
|
|
|
|
view->prev = views.tail;
|
2018-08-11 23:30:30 +00:00
|
|
|
view->next = NULL;
|
2018-08-13 17:49:03 +00:00
|
|
|
if (!views.head) views.head = view;
|
|
|
|
views.tail = view;
|
|
|
|
views.tags[view->tag.id] = view;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void viewRemove(struct View *view) {
|
|
|
|
if (view->prev) view->prev->next = view->next;
|
|
|
|
if (view->next) view->next->prev = view->prev;
|
|
|
|
if (views.head == view) views.head = view->next;
|
|
|
|
if (views.tail == view) views.tail = view->prev;
|
|
|
|
views.tags[view->tag.id] = NULL;
|
2018-08-11 23:30:30 +00:00
|
|
|
}
|
2018-08-04 17:35:29 +00:00
|
|
|
|
2018-08-13 17:49:03 +00:00
|
|
|
static const int LOG_LINES = 256;
|
|
|
|
static const int TOPIC_COLS = 512;
|
|
|
|
static const int INPUT_COLS = 512;
|
|
|
|
|
2018-08-11 23:30:30 +00:00
|
|
|
static int logHeight(const struct View *view) {
|
|
|
|
return LINES - (view->topic ? 2 : 0) - 2;
|
|
|
|
}
|
|
|
|
static int lastLogLine(void) {
|
|
|
|
return LOG_LINES - 1;
|
|
|
|
}
|
2018-08-05 00:54:50 +00:00
|
|
|
static int lastLine(void) {
|
|
|
|
return LINES - 1;
|
|
|
|
}
|
|
|
|
static int lastCol(void) {
|
|
|
|
return COLS - 1;
|
|
|
|
}
|
2018-08-11 03:31:20 +00:00
|
|
|
|
2018-08-11 23:30:30 +00:00
|
|
|
static struct View *viewTag(struct Tag tag) {
|
2018-08-13 17:49:03 +00:00
|
|
|
struct View *view = views.tags[tag.id];
|
2018-08-11 23:30:30 +00:00
|
|
|
if (view) return view;
|
|
|
|
|
|
|
|
view = calloc(1, sizeof(*view));
|
|
|
|
if (!view) err(EX_OSERR, "calloc");
|
|
|
|
|
|
|
|
view->tag = tag;
|
|
|
|
view->log = newpad(LOG_LINES, COLS);
|
|
|
|
wsetscrreg(view->log, 0, lastLogLine());
|
|
|
|
scrollok(view->log, true);
|
2018-08-12 02:45:53 +00:00
|
|
|
wmove(view->log, lastLogLine() - logHeight(view) + 2, 0);
|
2018-08-11 23:30:30 +00:00
|
|
|
view->scroll = LOG_LINES;
|
2018-09-02 05:01:41 +00:00
|
|
|
view->mark = true;
|
2018-08-11 23:30:30 +00:00
|
|
|
|
|
|
|
viewAppend(view);
|
|
|
|
return view;
|
|
|
|
}
|
|
|
|
|
2018-08-13 17:49:03 +00:00
|
|
|
static void viewClose(struct View *view) {
|
|
|
|
viewRemove(view);
|
|
|
|
if (view->topic) delwin(view->topic);
|
|
|
|
delwin(view->log);
|
|
|
|
free(view);
|
|
|
|
}
|
|
|
|
|
2018-08-17 18:00:08 +00:00
|
|
|
static void viewMark(struct View *view) {
|
|
|
|
view->mark = true;
|
|
|
|
}
|
|
|
|
static void viewUnmark(struct View *view) {
|
|
|
|
view->unread = 0;
|
|
|
|
view->hot = false;
|
|
|
|
view->mark = false;
|
|
|
|
}
|
|
|
|
|
2018-08-13 17:49:03 +00:00
|
|
|
static struct {
|
|
|
|
bool hide;
|
|
|
|
struct View *view;
|
|
|
|
WINDOW *input;
|
|
|
|
} ui;
|
|
|
|
|
2018-08-13 00:41:13 +00:00
|
|
|
void uiHide(void) {
|
|
|
|
ui.hide = true;
|
|
|
|
termMode(TERM_FOCUS, false);
|
|
|
|
endwin();
|
|
|
|
}
|
|
|
|
|
|
|
|
static void uiShow(void) {
|
|
|
|
ui.hide = false;
|
|
|
|
termMode(TERM_FOCUS, true);
|
|
|
|
}
|
|
|
|
|
2018-08-04 17:35:29 +00:00
|
|
|
void uiInit(void) {
|
|
|
|
setlocale(LC_CTYPE, "");
|
|
|
|
initscr();
|
|
|
|
cbreak();
|
|
|
|
noecho();
|
|
|
|
|
2018-08-05 18:59:51 +00:00
|
|
|
colorInit();
|
2018-08-14 03:09:53 +00:00
|
|
|
termInit();
|
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-07 02:14:59 +00:00
|
|
|
wmove(ui.input, 1, 0);
|
2018-08-05 00:54:50 +00:00
|
|
|
keypad(ui.input, true);
|
2018-08-04 19:04:48 +00:00
|
|
|
nodelay(ui.input, true);
|
2018-08-11 23:30:30 +00:00
|
|
|
|
|
|
|
ui.view = viewTag(TAG_STATUS);
|
2018-08-14 03:09:53 +00:00
|
|
|
termTitle(TAG_STATUS.name);
|
2018-08-04 19:04:48 +00:00
|
|
|
|
2018-08-13 00:41:13 +00:00
|
|
|
uiShow();
|
2018-08-09 04:24:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void uiExit(void) {
|
|
|
|
uiHide();
|
2018-08-05 01:23:28 +00:00
|
|
|
printf(
|
|
|
|
"This program is AGPLv3 free software!\n"
|
2018-08-05 15:00:01 +00:00
|
|
|
"The source is available at <" SOURCE_URL ">.\n"
|
2018-08-05 01:23:28 +00:00
|
|
|
);
|
2018-08-04 17:35:29 +00:00
|
|
|
}
|
|
|
|
|
2018-08-11 03:31:20 +00:00
|
|
|
static void uiResize(void) {
|
2018-08-13 17:49:03 +00:00
|
|
|
for (struct View *view = views.head; view; view = view->next) {
|
2018-08-11 03:31:20 +00:00
|
|
|
wresize(view->log, LOG_LINES, COLS);
|
2018-08-11 23:30:30 +00:00
|
|
|
wmove(view->log, lastLogLine(), lastCol());
|
2018-08-11 03:31:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-04 17:35:29 +00:00
|
|
|
void uiDraw(void) {
|
2018-08-09 04:24:49 +00:00
|
|
|
if (ui.hide) return;
|
2018-08-11 23:30:30 +00:00
|
|
|
if (ui.view->topic) {
|
|
|
|
pnoutrefresh(
|
|
|
|
ui.view->topic,
|
|
|
|
0, 0,
|
|
|
|
0, 0,
|
|
|
|
1, lastCol()
|
|
|
|
);
|
|
|
|
}
|
2018-08-04 17:35:29 +00:00
|
|
|
pnoutrefresh(
|
2018-08-11 23:30:30 +00:00
|
|
|
ui.view->log,
|
|
|
|
ui.view->scroll - logHeight(ui.view), 0,
|
|
|
|
(ui.view->topic ? 2 : 0), 0,
|
2018-08-05 00:54:50 +00:00
|
|
|
lastLine() - 2, lastCol()
|
2018-08-04 17:35:29 +00:00
|
|
|
);
|
2018-08-07 02:14:59 +00:00
|
|
|
int _, x;
|
|
|
|
getyx(ui.input, _, x);
|
2018-08-04 17:35:29 +00:00
|
|
|
pnoutrefresh(
|
|
|
|
ui.input,
|
2018-08-07 04:09:50 +00:00
|
|
|
0, MAX(0, x - lastCol() + 3),
|
2018-08-05 00:54:50 +00:00
|
|
|
lastLine() - 1, 0,
|
|
|
|
lastLine(), lastCol()
|
2018-08-04 17:35:29 +00:00
|
|
|
);
|
|
|
|
doupdate();
|
|
|
|
}
|
|
|
|
|
2018-08-05 18:59:51 +00:00
|
|
|
static void uiRedraw(void) {
|
|
|
|
clearok(curscr, true);
|
|
|
|
}
|
|
|
|
|
2018-08-11 23:30:30 +00:00
|
|
|
static void uiView(struct View *view) {
|
2018-08-14 03:09:53 +00:00
|
|
|
termTitle(view->tag.name);
|
2018-08-11 23:30:30 +00:00
|
|
|
if (view->topic) touchwin(view->topic);
|
2018-08-11 03:31:20 +00:00
|
|
|
touchwin(view->log);
|
2018-08-17 18:00:08 +00:00
|
|
|
viewMark(ui.view);
|
|
|
|
viewUnmark(view);
|
2018-08-11 23:30:30 +00:00
|
|
|
ui.view = view;
|
2018-08-11 03:31:20 +00:00
|
|
|
}
|
|
|
|
|
2018-08-13 17:49:03 +00:00
|
|
|
static void uiClose(struct View *view) {
|
|
|
|
if (ui.view == view) {
|
|
|
|
if (view->next) {
|
|
|
|
uiView(view->next);
|
|
|
|
} else if (view->prev) {
|
|
|
|
uiView(view->prev);
|
|
|
|
} else {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
viewClose(view);
|
|
|
|
}
|
|
|
|
|
2018-08-11 23:30:30 +00:00
|
|
|
void uiViewTag(struct Tag tag) {
|
|
|
|
uiView(viewTag(tag));
|
2018-08-07 21:51:23 +00:00
|
|
|
}
|
|
|
|
|
2018-08-13 17:49:03 +00:00
|
|
|
void uiCloseTag(struct Tag tag) {
|
|
|
|
uiClose(viewTag(tag));
|
|
|
|
}
|
|
|
|
|
2018-08-11 23:30:30 +00:00
|
|
|
void uiViewNum(int num) {
|
|
|
|
if (num < 0) {
|
2018-08-13 17:49:03 +00:00
|
|
|
for (struct View *view = views.tail; view; view = view->prev) {
|
2018-08-11 23:30:30 +00:00
|
|
|
if (++num) continue;
|
|
|
|
uiView(view);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
} else {
|
2018-08-13 17:49:03 +00:00
|
|
|
for (struct View *view = views.head; view; view = view->next) {
|
2018-08-11 23:30:30 +00:00
|
|
|
if (num--) continue;
|
|
|
|
uiView(view);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static const short IRC_COLORS[] = {
|
|
|
|
[IRC_WHITE] = 8 + COLOR_WHITE,
|
|
|
|
[IRC_BLACK] = 0 + COLOR_BLACK,
|
|
|
|
[IRC_BLUE] = 0 + COLOR_BLUE,
|
|
|
|
[IRC_GREEN] = 0 + COLOR_GREEN,
|
|
|
|
[IRC_RED] = 8 + COLOR_RED,
|
|
|
|
[IRC_BROWN] = 0 + COLOR_RED,
|
|
|
|
[IRC_MAGENTA] = 0 + COLOR_MAGENTA,
|
|
|
|
[IRC_ORANGE] = 0 + COLOR_YELLOW,
|
|
|
|
[IRC_YELLOW] = 8 + COLOR_YELLOW,
|
|
|
|
[IRC_LIGHT_GREEN] = 8 + COLOR_GREEN,
|
|
|
|
[IRC_CYAN] = 0 + COLOR_CYAN,
|
|
|
|
[IRC_LIGHT_CYAN] = 8 + COLOR_CYAN,
|
|
|
|
[IRC_LIGHT_BLUE] = 8 + COLOR_BLUE,
|
|
|
|
[IRC_PINK] = 8 + COLOR_MAGENTA,
|
|
|
|
[IRC_GRAY] = 8 + COLOR_BLACK,
|
|
|
|
[IRC_LIGHT_GRAY] = 0 + COLOR_WHITE,
|
2018-08-04 17:35:29 +00:00
|
|
|
};
|
|
|
|
|
2018-08-06 18:19:52 +00:00
|
|
|
static const wchar_t *parseColor(short *pair, const wchar_t *str) {
|
2018-08-04 20:36:25 +00:00
|
|
|
short fg = 0;
|
2018-08-06 18:19:52 +00:00
|
|
|
size_t fgLen = MIN(wcsspn(str, L"0123456789"), 2);
|
2018-08-05 17:28:49 +00:00
|
|
|
if (!fgLen) { *pair = -1; return str; }
|
2018-08-04 20:36:25 +00:00
|
|
|
for (size_t i = 0; i < fgLen; ++i) {
|
2018-08-06 18:19:52 +00:00
|
|
|
fg = fg * 10 + (str[i] - L'0');
|
2018-08-04 20:36:25 +00:00
|
|
|
}
|
|
|
|
str = &str[fgLen];
|
|
|
|
|
|
|
|
short bg = 0;
|
2018-08-06 18:19:52 +00:00
|
|
|
size_t bgLen = 0;
|
|
|
|
if (str[0] == L',') bgLen = MIN(wcsspn(&str[1], L"0123456789"), 2);
|
2018-08-04 20:36:25 +00:00
|
|
|
for (size_t i = 0; i < bgLen; ++i) {
|
2018-08-06 18:19:52 +00:00
|
|
|
bg = bg * 10 + (str[1 + i] - L'0');
|
2018-08-04 20:36:25 +00:00
|
|
|
}
|
|
|
|
if (bgLen) str = &str[1 + bgLen];
|
|
|
|
|
2018-08-05 17:28:49 +00:00
|
|
|
if (*pair == -1) *pair = 0;
|
2018-08-05 18:59:51 +00:00
|
|
|
*pair = (*pair & 0xF0) | IRC_COLORS[fg & 0x0F];
|
|
|
|
if (bgLen) *pair = (*pair & 0x0F) | (IRC_COLORS[bg & 0x0F] << 4);
|
2018-08-04 20:36:25 +00:00
|
|
|
|
|
|
|
return str;
|
|
|
|
}
|
|
|
|
|
2018-08-06 19:12:13 +00:00
|
|
|
static void wordWrap(WINDOW *win, const wchar_t *str) {
|
|
|
|
size_t len = wcscspn(str, L" ");
|
|
|
|
size_t width = 1;
|
|
|
|
for (size_t i = 0; i < len; ++i) {
|
|
|
|
if (iswprint(str[i])) width += wcwidth(str[i]);
|
|
|
|
}
|
|
|
|
|
|
|
|
int _, x, xMax;
|
|
|
|
getyx(win, _, x);
|
|
|
|
getmaxyx(win, _, xMax);
|
|
|
|
|
|
|
|
if (width >= (size_t)(xMax - x)) {
|
|
|
|
waddch(win, '\n');
|
|
|
|
} else {
|
|
|
|
waddch(win, ' ');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-07 04:09:50 +00:00
|
|
|
static const wchar_t IRC_CODES[] = {
|
|
|
|
L' ',
|
|
|
|
IRC_BOLD,
|
|
|
|
IRC_COLOR,
|
|
|
|
IRC_REVERSE,
|
|
|
|
IRC_RESET,
|
|
|
|
IRC_ITALIC,
|
|
|
|
IRC_UNDERLINE,
|
|
|
|
L'\0',
|
|
|
|
};
|
|
|
|
|
2018-08-06 18:19:52 +00:00
|
|
|
static void addIRC(WINDOW *win, const wchar_t *str) {
|
2018-08-04 17:35:29 +00:00
|
|
|
attr_t attr = A_NORMAL;
|
2018-08-05 17:28:49 +00:00
|
|
|
short pair = -1;
|
2018-08-04 17:35:29 +00:00
|
|
|
for (;;) {
|
2018-08-07 04:09:50 +00:00
|
|
|
size_t cc = wcscspn(str, IRC_CODES);
|
2018-08-05 17:28:49 +00:00
|
|
|
wattr_set(win, attr | attr8(pair), 1 + pair8(pair), NULL);
|
2018-08-06 18:19:52 +00:00
|
|
|
waddnwstr(win, str, cc);
|
2018-08-04 17:35:29 +00:00
|
|
|
if (!str[cc]) break;
|
|
|
|
|
|
|
|
str = &str[cc];
|
|
|
|
switch (*str++) {
|
2018-08-07 04:09:50 +00:00
|
|
|
break; case L' ': wordWrap(win, str);
|
|
|
|
break; case IRC_BOLD: attr ^= A_BOLD;
|
|
|
|
break; case IRC_ITALIC: attr ^= A_ITALIC;
|
|
|
|
break; case IRC_UNDERLINE: attr ^= A_UNDERLINE;
|
|
|
|
break; case IRC_REVERSE: attr ^= A_REVERSE;
|
|
|
|
break; case IRC_COLOR: str = parseColor(&pair, str);
|
|
|
|
break; case IRC_RESET: attr = A_NORMAL; pair = -1;
|
2018-08-04 17:35:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-11 03:31:20 +00:00
|
|
|
void uiTopic(struct Tag tag, const char *topic) {
|
2018-08-11 23:30:30 +00:00
|
|
|
struct View *view = viewTag(tag);
|
|
|
|
if (!view->topic) {
|
|
|
|
view->topic = newpad(2, TOPIC_COLS);
|
|
|
|
mvwhline(view->topic, 1, 0, ACS_HLINE, TOPIC_COLS);
|
|
|
|
}
|
2018-08-07 20:24:14 +00:00
|
|
|
wchar_t *wcs = ambstowcs(topic);
|
|
|
|
if (!wcs) err(EX_DATAERR, "ambstowcs");
|
2018-08-11 03:31:20 +00:00
|
|
|
wmove(view->topic, 0, 0);
|
|
|
|
addIRC(view->topic, wcs);
|
|
|
|
wclrtoeol(view->topic);
|
2018-08-07 20:24:14 +00:00
|
|
|
free(wcs);
|
2018-08-06 18:19:52 +00:00
|
|
|
}
|
|
|
|
|
2018-08-17 18:00:08 +00:00
|
|
|
void uiLog(struct Tag tag, enum UIHeat heat, const wchar_t *line) {
|
2018-08-11 23:30:30 +00:00
|
|
|
struct View *view = viewTag(tag);
|
2018-08-11 03:31:20 +00:00
|
|
|
waddch(view->log, '\n');
|
2018-08-17 18:00:08 +00:00
|
|
|
if (view->mark && heat > UI_COLD) {
|
|
|
|
if (!view->unread++) waddch(view->log, '\n');
|
|
|
|
if (heat > UI_WARM) {
|
|
|
|
view->hot = true;
|
|
|
|
beep(); // TODO: Notification.
|
|
|
|
}
|
2018-08-08 22:50:57 +00:00
|
|
|
}
|
2018-08-11 03:31:20 +00:00
|
|
|
addIRC(view->log, line);
|
2018-08-04 17:35:29 +00:00
|
|
|
}
|
|
|
|
|
2018-08-17 18:00:08 +00:00
|
|
|
void uiFmt(struct Tag tag, enum UIHeat heat, const wchar_t *format, ...) {
|
2018-08-11 23:30:30 +00:00
|
|
|
wchar_t *wcs;
|
2018-08-04 17:35:29 +00:00
|
|
|
va_list ap;
|
|
|
|
va_start(ap, format);
|
2018-08-11 23:30:30 +00:00
|
|
|
vaswprintf(&wcs, format, ap);
|
2018-08-04 17:35:29 +00:00
|
|
|
va_end(ap);
|
2018-08-11 23:30:30 +00:00
|
|
|
if (!wcs) err(EX_OSERR, "vaswprintf");
|
2018-08-17 18:00:08 +00:00
|
|
|
uiLog(tag, heat, wcs);
|
2018-08-11 23:30:30 +00:00
|
|
|
free(wcs);
|
2018-08-04 17:35:29 +00:00
|
|
|
}
|
|
|
|
|
2018-08-12 02:46:28 +00:00
|
|
|
static void logScrollUp(int lines) {
|
2018-08-11 23:30:30 +00:00
|
|
|
int height = logHeight(ui.view);
|
|
|
|
if (ui.view->scroll == height) return;
|
2018-08-17 18:00:08 +00:00
|
|
|
if (ui.view->scroll == LOG_LINES) viewMark(ui.view);
|
2018-08-12 02:46:28 +00:00
|
|
|
ui.view->scroll = MAX(ui.view->scroll - lines, height);
|
2018-08-05 00:54:50 +00:00
|
|
|
}
|
2018-08-12 02:46:28 +00:00
|
|
|
static void logScrollDown(int lines) {
|
2018-08-11 23:30:30 +00:00
|
|
|
if (ui.view->scroll == LOG_LINES) return;
|
2018-08-12 02:46:28 +00:00
|
|
|
ui.view->scroll = MIN(ui.view->scroll + lines, LOG_LINES);
|
2018-08-17 18:00:08 +00:00
|
|
|
if (ui.view->scroll == LOG_LINES) viewUnmark(ui.view);
|
2018-08-05 00:54:50 +00:00
|
|
|
}
|
2018-08-12 02:46:28 +00:00
|
|
|
static void logPageUp(void) {
|
|
|
|
logScrollUp(logHeight(ui.view) / 2);
|
|
|
|
}
|
|
|
|
static void logPageDown(void) {
|
|
|
|
logScrollDown(logHeight(ui.view) / 2);
|
|
|
|
}
|
2018-08-05 00:54:50 +00:00
|
|
|
|
2018-08-11 03:31:20 +00:00
|
|
|
static bool keyChar(wchar_t ch) {
|
2018-08-20 21:20:44 +00:00
|
|
|
if (ch < 0200) {
|
2018-08-11 23:30:30 +00:00
|
|
|
enum TermEvent event = termEvent((char)ch);
|
|
|
|
switch (event) {
|
2018-08-17 18:00:08 +00:00
|
|
|
break; case TERM_FOCUS_IN: viewUnmark(ui.view);
|
|
|
|
break; case TERM_FOCUS_OUT: viewMark(ui.view);
|
2018-08-11 23:30:30 +00:00
|
|
|
break; default: {}
|
|
|
|
}
|
|
|
|
if (event) return false;
|
2018-08-11 03:31:20 +00:00
|
|
|
}
|
2018-08-11 23:30:30 +00:00
|
|
|
|
|
|
|
static bool meta;
|
|
|
|
if (ch == L'\33') {
|
|
|
|
meta = true;
|
2018-08-11 03:31:20 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-08-11 23:30:30 +00:00
|
|
|
if (meta) {
|
|
|
|
bool update = true;
|
2018-08-11 03:31:20 +00:00
|
|
|
switch (ch) {
|
2018-08-11 23:30:30 +00:00
|
|
|
break; case L'b': edit(ui.view->tag, EDIT_BACK_WORD, 0);
|
|
|
|
break; case L'f': edit(ui.view->tag, EDIT_FORE_WORD, 0);
|
|
|
|
break; case L'\b': edit(ui.view->tag, EDIT_KILL_BACK_WORD, 0);
|
|
|
|
break; case L'd': edit(ui.view->tag, EDIT_KILL_FORE_WORD, 0);
|
2018-08-11 03:31:20 +00:00
|
|
|
break; default: {
|
|
|
|
update = false;
|
2018-08-11 23:30:30 +00:00
|
|
|
if (ch < L'0' || ch > L'9') break;
|
|
|
|
uiViewNum(ch - L'0');
|
2018-08-11 03:31:20 +00:00
|
|
|
}
|
|
|
|
}
|
2018-08-11 23:30:30 +00:00
|
|
|
meta = false;
|
2018-08-11 03:31:20 +00:00
|
|
|
return update;
|
|
|
|
}
|
|
|
|
|
2018-08-11 23:30:30 +00:00
|
|
|
if (ch == L'\177') ch = L'\b';
|
2018-08-05 00:54:50 +00:00
|
|
|
switch (ch) {
|
2018-08-11 03:31:20 +00:00
|
|
|
break; case CTRL(L'L'): uiRedraw(); return false;
|
|
|
|
|
2018-08-11 23:30:30 +00:00
|
|
|
break; case CTRL(L'A'): edit(ui.view->tag, EDIT_HOME, 0);
|
|
|
|
break; case CTRL(L'B'): edit(ui.view->tag, EDIT_LEFT, 0);
|
|
|
|
break; case CTRL(L'D'): edit(ui.view->tag, EDIT_DELETE, 0);
|
|
|
|
break; case CTRL(L'E'): edit(ui.view->tag, EDIT_END, 0);
|
|
|
|
break; case CTRL(L'F'): edit(ui.view->tag, EDIT_RIGHT, 0);
|
|
|
|
break; case CTRL(L'K'): edit(ui.view->tag, EDIT_KILL_LINE, 0);
|
|
|
|
break; case CTRL(L'W'): edit(ui.view->tag, EDIT_KILL_BACK_WORD, 0);
|
|
|
|
|
|
|
|
break; case CTRL(L'C'): edit(ui.view->tag, EDIT_INSERT, IRC_COLOR);
|
|
|
|
break; case CTRL(L'N'): edit(ui.view->tag, EDIT_INSERT, IRC_RESET);
|
|
|
|
break; case CTRL(L'O'): edit(ui.view->tag, EDIT_INSERT, IRC_BOLD);
|
|
|
|
break; case CTRL(L'R'): edit(ui.view->tag, EDIT_INSERT, IRC_COLOR);
|
|
|
|
break; case CTRL(L'T'): edit(ui.view->tag, EDIT_INSERT, IRC_ITALIC);
|
|
|
|
break; case CTRL(L'U'): edit(ui.view->tag, EDIT_INSERT, IRC_UNDERLINE);
|
|
|
|
break; case CTRL(L'V'): edit(ui.view->tag, EDIT_INSERT, IRC_REVERSE);
|
|
|
|
|
|
|
|
break; case L'\b': edit(ui.view->tag, EDIT_BACKSPACE, 0);
|
|
|
|
break; case L'\t': edit(ui.view->tag, EDIT_COMPLETE, 0);
|
|
|
|
break; case L'\n': edit(ui.view->tag, EDIT_ENTER, 0);
|
2018-08-11 03:31:20 +00:00
|
|
|
|
2018-08-08 03:03:08 +00:00
|
|
|
break; default: {
|
2018-08-11 03:31:20 +00:00
|
|
|
if (!iswprint(ch)) return false;
|
2018-08-11 23:30:30 +00:00
|
|
|
edit(ui.view->tag, EDIT_INSERT, ch);
|
2018-08-08 03:03:08 +00:00
|
|
|
}
|
2018-08-05 00:54:50 +00:00
|
|
|
}
|
2018-08-11 03:31:20 +00:00
|
|
|
return true;
|
2018-08-05 00:54:50 +00:00
|
|
|
}
|
|
|
|
|
2018-08-11 03:31:20 +00:00
|
|
|
static bool keyCode(wchar_t ch) {
|
2018-08-05 00:54:50 +00:00
|
|
|
switch (ch) {
|
2018-08-11 03:31:20 +00:00
|
|
|
break; case KEY_RESIZE: uiResize(); return false;
|
2018-08-12 02:46:28 +00:00
|
|
|
break; case KEY_SLEFT: logScrollUp(1); return false;
|
|
|
|
break; case KEY_SRIGHT: logScrollDown(1); return false;
|
2018-08-11 23:30:30 +00:00
|
|
|
break; case KEY_PPAGE: logPageUp(); return false;
|
|
|
|
break; case KEY_NPAGE: logPageDown(); return false;
|
|
|
|
break; case KEY_LEFT: edit(ui.view->tag, EDIT_LEFT, 0);
|
|
|
|
break; case KEY_RIGHT: edit(ui.view->tag, EDIT_RIGHT, 0);
|
|
|
|
break; case KEY_HOME: edit(ui.view->tag, EDIT_HOME, 0);
|
|
|
|
break; case KEY_END: edit(ui.view->tag, EDIT_END, 0);
|
|
|
|
break; case KEY_DC: edit(ui.view->tag, EDIT_DELETE, 0);
|
|
|
|
break; case KEY_BACKSPACE: edit(ui.view->tag, EDIT_BACKSPACE, 0);
|
|
|
|
break; case KEY_ENTER: edit(ui.view->tag, EDIT_ENTER, 0);
|
2018-08-05 00:54:50 +00:00
|
|
|
}
|
2018-08-11 03:31:20 +00:00
|
|
|
return true;
|
2018-08-05 00:54:50 +00:00
|
|
|
}
|
2018-08-04 17:35:29 +00:00
|
|
|
|
2018-08-05 00:54:50 +00:00
|
|
|
void uiRead(void) {
|
2018-08-13 00:41:13 +00:00
|
|
|
uiShow();
|
2018-08-09 04:24:49 +00:00
|
|
|
|
2018-08-08 20:59:26 +00:00
|
|
|
bool update = false;
|
2018-08-05 00:54:50 +00:00
|
|
|
int ret;
|
2018-08-04 17:35:29 +00:00
|
|
|
wint_t ch;
|
2018-08-05 00:54:50 +00:00
|
|
|
while (ERR != (ret = wget_wch(ui.input, &ch))) {
|
|
|
|
if (ret == KEY_CODE_YES) {
|
2018-08-08 20:59:26 +00:00
|
|
|
update |= keyCode(ch);
|
2018-08-05 00:54:50 +00:00
|
|
|
} else {
|
2018-08-08 20:59:26 +00:00
|
|
|
update |= keyChar(ch);
|
2018-08-04 17:35:29 +00:00
|
|
|
}
|
|
|
|
}
|
2018-08-08 20:59:26 +00:00
|
|
|
if (!update) return;
|
2018-08-07 02:14:59 +00:00
|
|
|
|
2018-08-11 23:30:30 +00:00
|
|
|
int y, x;
|
2018-08-04 17:35:29 +00:00
|
|
|
wmove(ui.input, 1, 0);
|
2018-08-08 20:59:26 +00:00
|
|
|
addIRC(ui.input, editHead());
|
|
|
|
getyx(ui.input, y, x);
|
|
|
|
addIRC(ui.input, editTail());
|
2018-08-04 17:35:29 +00:00
|
|
|
wclrtoeol(ui.input);
|
2018-08-08 20:59:26 +00:00
|
|
|
wmove(ui.input, y, x);
|
2018-08-04 17:35:29 +00:00
|
|
|
}
|