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
|
|
|
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 *log;
|
2018-09-13 05:03:47 +00:00
|
|
|
int scroll, unread;
|
|
|
|
bool hot, mark;
|
2018-08-11 23:30:30 +00:00
|
|
|
struct View *prev;
|
|
|
|
struct View *next;
|
2018-08-13 17:49:03 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
static struct {
|
|
|
|
bool hide;
|
2018-09-02 18:04:05 +00:00
|
|
|
WINDOW *status;
|
2018-08-13 17:49:03 +00:00
|
|
|
WINDOW *input;
|
2018-09-13 05:03:47 +00:00
|
|
|
struct View *view;
|
2018-08-13 17:49:03 +00:00
|
|
|
} ui;
|
|
|
|
|
2018-08-13 00:41:13 +00:00
|
|
|
static void uiShow(void) {
|
|
|
|
ui.hide = false;
|
2018-09-02 20:13:00 +00:00
|
|
|
termMode(TermFocus, true);
|
2018-08-13 00:41:13 +00:00
|
|
|
}
|
|
|
|
|
2018-09-02 18:04:05 +00:00
|
|
|
void uiHide(void) {
|
|
|
|
ui.hide = true;
|
2018-09-02 20:13:00 +00:00
|
|
|
termMode(TermFocus, false);
|
2018-09-02 18:04:05 +00:00
|
|
|
endwin();
|
2018-08-11 03:31:20 +00:00
|
|
|
}
|
|
|
|
|
2018-09-13 05:03:47 +00:00
|
|
|
static const int ColsMax = 512;
|
2018-09-02 18:04:05 +00:00
|
|
|
|
2018-09-13 05:03:47 +00:00
|
|
|
void uiInit(void) {
|
|
|
|
setlocale(LC_CTYPE, "");
|
|
|
|
initscr();
|
|
|
|
cbreak();
|
|
|
|
noecho();
|
2018-09-02 18:04:05 +00:00
|
|
|
|
2018-09-13 05:03:47 +00:00
|
|
|
colorInit();
|
|
|
|
termInit();
|
|
|
|
|
|
|
|
ui.status = newpad(1, ColsMax);
|
|
|
|
ui.input = newpad(1, ColsMax);
|
|
|
|
keypad(ui.input, true);
|
|
|
|
nodelay(ui.input, true);
|
|
|
|
|
|
|
|
uiViewTag(TagStatus);
|
|
|
|
}
|
|
|
|
|
|
|
|
void uiExit(void) {
|
|
|
|
uiHide();
|
|
|
|
printf(
|
|
|
|
"This program is AGPLv3 Free Software!\n"
|
|
|
|
"The source is available at <" SOURCE_URL ">.\n"
|
2018-08-04 17:35:29 +00:00
|
|
|
);
|
2018-09-13 05:03:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int lastLine(void) {
|
|
|
|
return LINES - 1;
|
|
|
|
}
|
|
|
|
static int lastCol(void) {
|
|
|
|
return COLS - 1;
|
|
|
|
}
|
|
|
|
static int logHeight(void) {
|
|
|
|
return LINES - 2;
|
|
|
|
}
|
|
|
|
|
|
|
|
void uiDraw(void) {
|
|
|
|
if (ui.hide) return;
|
2018-09-02 18:04:05 +00:00
|
|
|
|
2018-08-07 02:14:59 +00:00
|
|
|
int _, x;
|
2018-09-02 18:04:05 +00:00
|
|
|
getyx(ui.status, _, x);
|
|
|
|
pnoutrefresh(
|
|
|
|
ui.status,
|
|
|
|
0, MAX(0, x - lastCol() - 1),
|
2018-09-13 05:03:47 +00:00
|
|
|
0, 0,
|
|
|
|
0, lastCol()
|
|
|
|
);
|
|
|
|
|
|
|
|
pnoutrefresh(
|
|
|
|
ui.view->log,
|
|
|
|
ui.view->scroll - logHeight(), 0,
|
|
|
|
1, 0,
|
2018-09-02 18:04:05 +00:00
|
|
|
lastLine() - 1, lastCol()
|
|
|
|
);
|
|
|
|
|
2018-08-07 02:14:59 +00:00
|
|
|
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-09-02 18:04:05 +00:00
|
|
|
lastLine(), 0,
|
2018-08-05 00:54:50 +00:00
|
|
|
lastLine(), lastCol()
|
2018-08-04 17:35:29 +00:00
|
|
|
);
|
2018-09-02 18:04:05 +00:00
|
|
|
|
2018-08-04 17:35:29 +00:00
|
|
|
doupdate();
|
|
|
|
}
|
|
|
|
|
2018-09-13 19:17:41 +00:00
|
|
|
static const short Colors[] = {
|
2018-09-02 20:13:00 +00:00
|
|
|
[IRCWhite] = 8 + COLOR_WHITE,
|
|
|
|
[IRCBlack] = 0 + COLOR_BLACK,
|
|
|
|
[IRCBlue] = 0 + COLOR_BLUE,
|
|
|
|
[IRCGreen] = 0 + COLOR_GREEN,
|
|
|
|
[IRCRed] = 8 + COLOR_RED,
|
|
|
|
[IRCBrown] = 0 + COLOR_RED,
|
|
|
|
[IRCMagenta] = 0 + COLOR_MAGENTA,
|
|
|
|
[IRCOrange] = 0 + COLOR_YELLOW,
|
|
|
|
[IRCYellow] = 8 + COLOR_YELLOW,
|
|
|
|
[IRCLightGreen] = 8 + COLOR_GREEN,
|
|
|
|
[IRCCyan] = 0 + COLOR_CYAN,
|
|
|
|
[IRCLightCyan] = 8 + COLOR_CYAN,
|
|
|
|
[IRCLightBlue] = 8 + COLOR_BLUE,
|
|
|
|
[IRCPink] = 8 + COLOR_MAGENTA,
|
|
|
|
[IRCGray] = 8 + COLOR_BLACK,
|
|
|
|
[IRCLightGray] = 0 + COLOR_WHITE,
|
2018-08-04 17:35:29 +00:00
|
|
|
};
|
|
|
|
|
2018-09-13 02:55:02 +00:00
|
|
|
static void addFormat(WINDOW *win, const struct Format *format) {
|
|
|
|
attr_t attr = A_NORMAL;
|
|
|
|
if (format->bold) attr |= A_BOLD;
|
|
|
|
if (format->italic) attr |= A_ITALIC;
|
|
|
|
if (format->underline) attr |= A_UNDERLINE;
|
|
|
|
if (format->reverse) attr |= A_REVERSE;
|
|
|
|
|
|
|
|
short pair = -1;
|
2018-09-13 19:17:41 +00:00
|
|
|
if (format->fg != IRCDefault) pair = Colors[format->fg];
|
|
|
|
if (format->bg != IRCDefault) pair |= Colors[format->bg] << 4;
|
2018-09-13 02:55:02 +00:00
|
|
|
|
|
|
|
wattr_set(win, attr | attr8(pair), 1 + pair8(pair), NULL);
|
|
|
|
waddnwstr(win, format->str, format->len);
|
|
|
|
}
|
|
|
|
|
2018-09-14 22:54:18 +00:00
|
|
|
static int printWidth(const wchar_t *str, size_t len) {
|
|
|
|
int width = 0;
|
|
|
|
for (size_t i = 0; i < len; ++i) {
|
|
|
|
if (iswprint(str[i])) width += wcwidth(str[i]);
|
|
|
|
}
|
|
|
|
return width;
|
|
|
|
}
|
|
|
|
|
2018-09-13 02:55:02 +00:00
|
|
|
static int addWrap(WINDOW *win, const wchar_t *str) {
|
2018-09-14 22:48:03 +00:00
|
|
|
int lines = 0;
|
|
|
|
|
2018-09-13 00:23:45 +00:00
|
|
|
struct Format format = { .str = str };
|
|
|
|
formatReset(&format);
|
|
|
|
while (formatParse(&format, NULL)) {
|
2018-09-14 22:48:03 +00:00
|
|
|
size_t word = 1 + wcscspn(&format.str[1], L" ");
|
|
|
|
if (word < format.len) format.len = word;
|
|
|
|
|
2018-09-13 00:23:45 +00:00
|
|
|
int _, x, xMax;
|
|
|
|
getyx(win, _, x);
|
|
|
|
getmaxyx(win, _, xMax);
|
2018-09-14 22:54:18 +00:00
|
|
|
if (xMax - x - 1 < printWidth(format.str, word)) {
|
2018-09-13 00:23:45 +00:00
|
|
|
if (format.str[0] == L' ') {
|
|
|
|
format.str++;
|
|
|
|
format.len--;
|
|
|
|
}
|
|
|
|
waddch(win, '\n');
|
|
|
|
lines++;
|
|
|
|
}
|
2018-09-14 22:48:03 +00:00
|
|
|
|
2018-09-13 02:55:02 +00:00
|
|
|
addFormat(win, &format);
|
2018-08-04 17:35:29 +00:00
|
|
|
}
|
2018-09-05 21:10:26 +00:00
|
|
|
return lines;
|
2018-08-04 17:35:29 +00:00
|
|
|
}
|
|
|
|
|
2018-09-13 05:03:47 +00:00
|
|
|
static struct {
|
|
|
|
struct View *head;
|
|
|
|
struct View *tail;
|
|
|
|
struct View *tags[TagsLen];
|
|
|
|
} views;
|
|
|
|
|
2018-09-02 18:04:05 +00:00
|
|
|
static void uiStatus(void) {
|
|
|
|
mvwhline(ui.status, 0, 0, ACS_HLINE, COLS);
|
|
|
|
mvwaddch(ui.status, 0, COLS, ACS_RTEE);
|
|
|
|
|
|
|
|
int num = 0;
|
|
|
|
int count = 0;
|
|
|
|
for (const struct View *view = views.head; view; view = view->next, ++num) {
|
|
|
|
if (!view->unread) continue;
|
2018-09-02 20:13:00 +00:00
|
|
|
bool status = (view->tag.id == TagStatus.id);
|
2018-09-02 18:04:05 +00:00
|
|
|
|
|
|
|
int unread;
|
|
|
|
wchar_t *str;
|
|
|
|
int len = aswprintf(
|
|
|
|
&str, L",\3%02d%d\3%s%s%n(%d)",
|
2018-09-02 20:13:00 +00:00
|
|
|
(view->hot ? IRCYellow : IRCWhite), num,
|
2018-09-02 18:04:05 +00:00
|
|
|
&status[":"], (status ? "" : view->tag.name),
|
|
|
|
&unread, view->unread
|
|
|
|
);
|
|
|
|
if (len < 0) err(EX_OSERR, "aswprintf");
|
|
|
|
if (view->unread == 1) str[unread] = L'\0';
|
|
|
|
|
2018-09-13 02:55:02 +00:00
|
|
|
addWrap(ui.status, count ? str : &str[1]);
|
2018-09-02 18:04:05 +00:00
|
|
|
free(str);
|
|
|
|
count++;
|
|
|
|
}
|
|
|
|
|
|
|
|
waddch(ui.status, count ? ACS_LTEE : '\b');
|
|
|
|
waddch(ui.status, ACS_HLINE);
|
|
|
|
}
|
|
|
|
|
2018-09-13 05:03:47 +00:00
|
|
|
static void viewAppend(struct View *view) {
|
|
|
|
if (views.tail) views.tail->next = view;
|
|
|
|
view->prev = views.tail;
|
|
|
|
view->next = NULL;
|
|
|
|
views.tail = view;
|
|
|
|
if (!views.head) views.head = 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;
|
|
|
|
}
|
|
|
|
|
|
|
|
static const int LogLines = 256;
|
|
|
|
|
|
|
|
static struct View *viewTag(struct Tag tag) {
|
|
|
|
struct View *view = views.tags[tag.id];
|
|
|
|
if (view) return view;
|
|
|
|
|
|
|
|
view = calloc(1, sizeof(*view));
|
|
|
|
if (!view) err(EX_OSERR, "calloc");
|
|
|
|
|
|
|
|
view->tag = tag;
|
|
|
|
|
|
|
|
view->log = newpad(LogLines, COLS);
|
|
|
|
wsetscrreg(view->log, 0, LogLines - 1);
|
|
|
|
scrollok(view->log, true);
|
|
|
|
wmove(view->log, LogLines - 1, 0);
|
|
|
|
|
|
|
|
view->scroll = LogLines;
|
|
|
|
view->mark = true;
|
|
|
|
|
|
|
|
viewAppend(view);
|
|
|
|
return view;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void viewClose(struct View *view) {
|
|
|
|
viewRemove(view);
|
|
|
|
delwin(view->log);
|
|
|
|
free(view);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void uiResize(void) {
|
|
|
|
for (struct View *view = views.head; view; view = view->next) {
|
|
|
|
wresize(view->log, LogLines, COLS);
|
|
|
|
wmove(view->log, LogLines - 1, lastCol());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void viewUnmark(struct View *view) {
|
|
|
|
view->mark = false;
|
|
|
|
view->unread = 0;
|
|
|
|
view->hot = false;
|
|
|
|
uiStatus();
|
|
|
|
}
|
|
|
|
|
2018-09-02 18:04:05 +00:00
|
|
|
static void uiView(struct View *view) {
|
|
|
|
termTitle(view->tag.name);
|
|
|
|
touchwin(view->log);
|
2018-09-13 05:03:47 +00:00
|
|
|
if (ui.view) ui.view->mark = true;
|
2018-09-02 18:04:05 +00:00
|
|
|
viewUnmark(view);
|
|
|
|
ui.view = view;
|
2018-09-15 03:37:09 +00:00
|
|
|
uiStatus();
|
|
|
|
uiPrompt();
|
2018-09-02 18:04:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void uiViewTag(struct Tag tag) {
|
|
|
|
uiView(viewTag(tag));
|
|
|
|
}
|
|
|
|
|
|
|
|
void uiViewNum(int num) {
|
|
|
|
if (num < 0) {
|
|
|
|
for (struct View *view = views.tail; view; view = view->prev) {
|
|
|
|
if (++num) continue;
|
|
|
|
uiView(view);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
for (struct View *view = views.head; view; view = view->next) {
|
|
|
|
if (num--) continue;
|
|
|
|
uiView(view);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-13 05:03:47 +00:00
|
|
|
void uiCloseTag(struct Tag tag) {
|
2018-08-11 23:30:30 +00:00
|
|
|
struct View *view = viewTag(tag);
|
2018-09-13 05:03:47 +00:00
|
|
|
if (ui.view == view) {
|
|
|
|
if (view->next) {
|
|
|
|
uiView(view->next);
|
|
|
|
} else if (view->prev) {
|
|
|
|
uiView(view->prev);
|
|
|
|
} else {
|
|
|
|
return;
|
|
|
|
}
|
2018-08-11 23:30:30 +00:00
|
|
|
}
|
2018-09-13 05:03:47 +00:00
|
|
|
viewClose(view);
|
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-09-05 21:10:26 +00:00
|
|
|
int lines = 1;
|
2018-08-11 03:31:20 +00:00
|
|
|
waddch(view->log, '\n');
|
2018-09-13 05:03:47 +00:00
|
|
|
|
2018-09-02 20:13:00 +00:00
|
|
|
if (view->mark && heat > UICold) {
|
2018-09-05 21:10:26 +00:00
|
|
|
if (!view->unread++) {
|
|
|
|
lines++;
|
|
|
|
waddch(view->log, '\n');
|
|
|
|
}
|
2018-09-02 20:13:00 +00:00
|
|
|
if (heat > UIWarm) {
|
2018-08-17 18:00:08 +00:00
|
|
|
view->hot = true;
|
2018-09-13 05:03:47 +00:00
|
|
|
beep(); // TODO: Notify.
|
2018-08-17 18:00:08 +00:00
|
|
|
}
|
2018-09-02 18:04:05 +00:00
|
|
|
uiStatus();
|
2018-08-08 22:50:57 +00:00
|
|
|
}
|
2018-09-13 05:03:47 +00:00
|
|
|
|
2018-09-13 02:55:02 +00:00
|
|
|
lines += addWrap(view->log, line);
|
2018-09-05 21:10:26 +00:00
|
|
|
if (view->scroll != LogLines) view->scroll -= lines;
|
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-09-13 05:03:47 +00:00
|
|
|
wchar_t *str;
|
2018-08-04 17:35:29 +00:00
|
|
|
va_list ap;
|
|
|
|
va_start(ap, format);
|
2018-09-13 05:03:47 +00:00
|
|
|
vaswprintf(&str, format, ap);
|
2018-08-04 17:35:29 +00:00
|
|
|
va_end(ap);
|
2018-09-13 05:03:47 +00:00
|
|
|
if (!str) err(EX_OSERR, "vaswprintf");
|
|
|
|
uiLog(tag, heat, str);
|
|
|
|
free(str);
|
2018-08-04 17:35:29 +00:00
|
|
|
}
|
|
|
|
|
2018-09-13 05:03:47 +00:00
|
|
|
static void scrollUp(int lines) {
|
|
|
|
if (ui.view->scroll == logHeight()) return;
|
|
|
|
if (ui.view->scroll == LogLines) ui.view->mark = true;
|
|
|
|
ui.view->scroll = MAX(ui.view->scroll - lines, logHeight());
|
2018-09-02 18:04:05 +00:00
|
|
|
}
|
2018-09-13 05:03:47 +00:00
|
|
|
static void scrollDown(int lines) {
|
2018-09-02 20:13:00 +00:00
|
|
|
if (ui.view->scroll == LogLines) return;
|
|
|
|
ui.view->scroll = MIN(ui.view->scroll + lines, LogLines);
|
|
|
|
if (ui.view->scroll == LogLines) viewUnmark(ui.view);
|
2018-08-05 00:54:50 +00:00
|
|
|
}
|
2018-09-13 05:03:47 +00:00
|
|
|
|
2018-09-14 03:44:55 +00:00
|
|
|
static void keyCode(wchar_t ch) {
|
2018-09-13 05:03:47 +00:00
|
|
|
switch (ch) {
|
2018-09-14 03:44:55 +00:00
|
|
|
break; case KEY_RESIZE: uiResize();
|
|
|
|
break; case KEY_SLEFT: scrollUp(1);
|
|
|
|
break; case KEY_SRIGHT: scrollDown(1);
|
|
|
|
break; case KEY_PPAGE: scrollUp(logHeight() / 2);
|
|
|
|
break; case KEY_NPAGE: scrollDown(logHeight() / 2);
|
2018-09-13 05:03:47 +00:00
|
|
|
break; case KEY_LEFT: edit(ui.view->tag, EditLeft, 0);
|
|
|
|
break; case KEY_RIGHT: edit(ui.view->tag, EditRight, 0);
|
|
|
|
break; case KEY_HOME: edit(ui.view->tag, EditHome, 0);
|
|
|
|
break; case KEY_END: edit(ui.view->tag, EditEnd, 0);
|
|
|
|
break; case KEY_DC: edit(ui.view->tag, EditDelete, 0);
|
|
|
|
break; case KEY_BACKSPACE: edit(ui.view->tag, EditBackspace, 0);
|
|
|
|
break; case KEY_ENTER: edit(ui.view->tag, EditEnter, 0);
|
|
|
|
}
|
2018-08-12 02:46:28 +00:00
|
|
|
}
|
2018-08-05 00:54:50 +00:00
|
|
|
|
2018-09-13 05:03:47 +00:00
|
|
|
#define CTRL(ch) ((ch) ^ 0100)
|
|
|
|
|
2018-09-14 03:44:55 +00:00
|
|
|
static void 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-09-02 20:13:00 +00:00
|
|
|
break; case TermFocusIn: viewUnmark(ui.view);
|
2018-09-13 05:03:47 +00:00
|
|
|
break; case TermFocusOut: ui.view->mark = true;
|
2018-08-11 23:30:30 +00:00
|
|
|
break; default: {}
|
|
|
|
}
|
2018-09-14 03:44:55 +00:00
|
|
|
if (event) return;
|
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-09-14 03:44:55 +00:00
|
|
|
return;
|
2018-08-11 03:31:20 +00:00
|
|
|
}
|
|
|
|
|
2018-09-13 05:03:47 +00:00
|
|
|
if (ch == L'\177') ch = L'\b';
|
|
|
|
|
2018-08-11 23:30:30 +00:00
|
|
|
if (meta) {
|
2018-09-13 05:03:47 +00:00
|
|
|
meta = false;
|
2018-08-11 03:31:20 +00:00
|
|
|
switch (ch) {
|
2018-09-02 20:13:00 +00:00
|
|
|
break; case L'b': edit(ui.view->tag, EditBackWord, 0);
|
|
|
|
break; case L'f': edit(ui.view->tag, EditForeWord, 0);
|
|
|
|
break; case L'\b': edit(ui.view->tag, EditKillBackWord, 0);
|
|
|
|
break; case L'd': edit(ui.view->tag, EditKillForeWord, 0);
|
2018-08-11 03:31:20 +00:00
|
|
|
break; default: {
|
2018-09-13 05:03:47 +00:00
|
|
|
if (ch >= L'0' && ch <= L'9') uiViewNum(ch - L'0');
|
2018-08-11 03:31:20 +00:00
|
|
|
}
|
|
|
|
}
|
2018-09-14 03:44:55 +00:00
|
|
|
return;
|
2018-08-11 03:31:20 +00:00
|
|
|
}
|
|
|
|
|
2018-08-05 00:54:50 +00:00
|
|
|
switch (ch) {
|
2018-09-14 03:44:55 +00:00
|
|
|
break; case CTRL(L'L'): clearok(curscr, true);
|
2018-08-11 03:31:20 +00:00
|
|
|
|
2018-09-02 20:13:00 +00:00
|
|
|
break; case CTRL(L'A'): edit(ui.view->tag, EditHome, 0);
|
|
|
|
break; case CTRL(L'B'): edit(ui.view->tag, EditLeft, 0);
|
|
|
|
break; case CTRL(L'D'): edit(ui.view->tag, EditDelete, 0);
|
|
|
|
break; case CTRL(L'E'): edit(ui.view->tag, EditEnd, 0);
|
|
|
|
break; case CTRL(L'F'): edit(ui.view->tag, EditRight, 0);
|
|
|
|
break; case CTRL(L'K'): edit(ui.view->tag, EditKillLine, 0);
|
|
|
|
break; case CTRL(L'W'): edit(ui.view->tag, EditKillBackWord, 0);
|
|
|
|
|
|
|
|
break; case CTRL(L'C'): edit(ui.view->tag, EditInsert, IRCColor);
|
|
|
|
break; case CTRL(L'N'): edit(ui.view->tag, EditInsert, IRCReset);
|
|
|
|
break; case CTRL(L'O'): edit(ui.view->tag, EditInsert, IRCBold);
|
|
|
|
break; case CTRL(L'R'): edit(ui.view->tag, EditInsert, IRCColor);
|
|
|
|
break; case CTRL(L'T'): edit(ui.view->tag, EditInsert, IRCItalic);
|
|
|
|
break; case CTRL(L'U'): edit(ui.view->tag, EditInsert, IRCUnderline);
|
|
|
|
break; case CTRL(L'V'): edit(ui.view->tag, EditInsert, IRCReverse);
|
|
|
|
|
|
|
|
break; case L'\b': edit(ui.view->tag, EditBackspace, 0);
|
|
|
|
break; case L'\t': edit(ui.view->tag, EditComplete, 0);
|
|
|
|
break; case L'\n': edit(ui.view->tag, EditEnter, 0);
|
2018-08-11 03:31:20 +00:00
|
|
|
|
2018-08-08 03:03:08 +00:00
|
|
|
break; default: {
|
2018-09-14 03:44:55 +00:00
|
|
|
if (iswprint(ch)) edit(ui.view->tag, EditInsert, ch);
|
2018-08-08 03:03:08 +00:00
|
|
|
}
|
2018-08-05 00:54:50 +00:00
|
|
|
}
|
2018-09-14 03:44:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static bool isAction(struct Tag tag, const wchar_t *input) {
|
|
|
|
if (tag.id == TagStatus.id || tag.id == TagVerbose.id) return false;
|
|
|
|
return !wcsncasecmp(input, L"/me ", 4);
|
|
|
|
}
|
|
|
|
|
|
|
|
// FIXME: This duplicates logic from input.c for wcs.
|
|
|
|
static bool isCommand(struct Tag tag, const wchar_t *input) {
|
|
|
|
if (tag.id == TagStatus.id || tag.id == TagVerbose.id) return true;
|
|
|
|
if (input[0] != L'/') return false;
|
|
|
|
const wchar_t *space = wcschr(&input[1], L' ');
|
|
|
|
const wchar_t *extra = wcschr(&input[1], L'/');
|
|
|
|
return !extra || (space && extra > space);
|
2018-08-05 00:54:50 +00:00
|
|
|
}
|
|
|
|
|
2018-09-15 03:37:09 +00:00
|
|
|
void uiPrompt(void) {
|
2018-09-14 03:44:55 +00:00
|
|
|
const wchar_t *input = editHead();
|
|
|
|
|
|
|
|
// TODO: Avoid reformatting these on every read.
|
|
|
|
wchar_t *prompt = NULL;
|
|
|
|
int len = 0;
|
|
|
|
if (isAction(ui.view->tag, input) && editTail() >= &input[4]) {
|
|
|
|
input = &input[4];
|
|
|
|
len = aswprintf(
|
|
|
|
&prompt, L"\3%d* %s\3 ",
|
|
|
|
formatColor(self.user), self.nick
|
|
|
|
);
|
|
|
|
} else if (!isCommand(ui.view->tag, input)) {
|
|
|
|
len = aswprintf(
|
|
|
|
&prompt, L"\3%d(%s)\3 ",
|
|
|
|
formatColor(self.user), self.nick
|
|
|
|
);
|
|
|
|
}
|
|
|
|
if (len < 0) err(EX_OSERR, "aswprintf");
|
|
|
|
|
2018-09-13 05:03:47 +00:00
|
|
|
wmove(ui.input, 0, 0);
|
2018-09-14 03:44:55 +00:00
|
|
|
if (prompt) addWrap(ui.input, prompt);
|
|
|
|
free(prompt);
|
2018-09-13 05:03:47 +00:00
|
|
|
|
2018-09-13 19:39:40 +00:00
|
|
|
int _, x = 0;
|
2018-09-14 03:44:55 +00:00
|
|
|
struct Format format = { .str = input };
|
|
|
|
formatReset(&format);
|
2018-09-13 02:55:02 +00:00
|
|
|
while (formatParse(&format, editTail())) {
|
2018-09-13 05:03:47 +00:00
|
|
|
if (format.split) getyx(ui.input, _, x);
|
2018-09-13 02:55:02 +00:00
|
|
|
addFormat(ui.input, &format);
|
|
|
|
}
|
|
|
|
|
2018-08-04 17:35:29 +00:00
|
|
|
wclrtoeol(ui.input);
|
2018-09-13 05:03:47 +00:00
|
|
|
wmove(ui.input, 0, x);
|
2018-08-04 17:35:29 +00:00
|
|
|
}
|
2018-09-15 03:37:09 +00:00
|
|
|
|
|
|
|
void uiRead(void) {
|
|
|
|
uiShow();
|
|
|
|
int ret;
|
|
|
|
wint_t ch;
|
|
|
|
while (ERR != (ret = wget_wch(ui.input, &ch))) {
|
|
|
|
if (ret == KEY_CODE_YES) {
|
|
|
|
keyCode(ch);
|
|
|
|
} else {
|
|
|
|
keyChar(ch);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
uiPrompt();
|
|
|
|
}
|