2019-02-22 19:11:50 +00:00
|
|
|
/* Copyright (C) 2018, 2019 C. McEnroe <june@causal.agency>
|
2018-08-04 17:35:29 +00:00
|
|
|
*
|
|
|
|
* 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 <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
|
2019-02-22 19:11:50 +00:00
|
|
|
#define A_ITALIC A_UNDERLINE
|
2018-08-11 23:30:30 +00:00
|
|
|
#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
|
|
|
|
2019-02-22 19:11:50 +00:00
|
|
|
#define CTRL(ch) ((ch) & 037)
|
|
|
|
enum { Esc = L'\33', Del = L'\177' };
|
|
|
|
|
|
|
|
static const int LogLines = 512;
|
|
|
|
|
|
|
|
static int lastLine(void) {
|
|
|
|
return LINES - 1;
|
|
|
|
}
|
|
|
|
static int lastCol(void) {
|
|
|
|
return COLS - 1;
|
|
|
|
}
|
|
|
|
static int logHeight(void) {
|
|
|
|
return LINES - 2;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct Window {
|
|
|
|
struct Tag tag;
|
|
|
|
WINDOW *log;
|
|
|
|
bool hot;
|
|
|
|
bool mark;
|
|
|
|
int scroll;
|
|
|
|
uint unread;
|
|
|
|
struct Window *prev;
|
|
|
|
struct Window *next;
|
|
|
|
};
|
|
|
|
|
|
|
|
static struct {
|
|
|
|
struct Window *active;
|
|
|
|
struct Window *head;
|
|
|
|
struct Window *tail;
|
|
|
|
struct Window *tag[TagsLen];
|
|
|
|
} windows;
|
|
|
|
|
|
|
|
static void windowAppend(struct Window *win) {
|
|
|
|
if (windows.tail) windows.tail->next = win;
|
|
|
|
win->prev = windows.tail;
|
|
|
|
win->next = NULL;
|
|
|
|
windows.tail = win;
|
|
|
|
if (!windows.head) windows.head = win;
|
|
|
|
windows.tag[win->tag.id] = win;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void windowRemove(struct Window *win) {
|
|
|
|
windows.tag[win->tag.id] = NULL;
|
|
|
|
if (win->prev) win->prev->next = win->next;
|
|
|
|
if (win->next) win->next->prev = win->prev;
|
|
|
|
if (windows.head == win) windows.head = win->next;
|
|
|
|
if (windows.tail == win) windows.tail = win->prev;
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct Window *windowFor(struct Tag tag) {
|
|
|
|
struct Window *win = windows.tag[tag.id];
|
|
|
|
if (win) return win;
|
|
|
|
|
|
|
|
win = calloc(1, sizeof(*win));
|
|
|
|
if (!win) err(EX_OSERR, "calloc");
|
|
|
|
|
|
|
|
win->tag = tag;
|
|
|
|
win->mark = true;
|
|
|
|
win->scroll = LogLines;
|
|
|
|
|
|
|
|
win->log = newpad(LogLines, COLS);
|
|
|
|
wsetscrreg(win->log, 0, LogLines - 1);
|
|
|
|
scrollok(win->log, true);
|
|
|
|
wmove(win->log, LogLines - 1, 0);
|
|
|
|
|
|
|
|
windowAppend(win);
|
|
|
|
return win;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void windowResize(struct Window *win) {
|
|
|
|
wresize(win->log, LogLines, COLS);
|
|
|
|
wmove(win->log, LogLines - 1, lastCol());
|
|
|
|
}
|
|
|
|
|
|
|
|
static void windowMark(struct Window *win) {
|
|
|
|
win->mark = true;
|
|
|
|
}
|
|
|
|
static void windowUnmark(struct Window *win) {
|
|
|
|
win->mark = false;
|
|
|
|
win->unread = 0;
|
|
|
|
win->hot = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void windowShow(struct Window *win) {
|
|
|
|
if (windows.active) windowMark(windows.active);
|
|
|
|
if (win) {
|
|
|
|
touchwin(win->log);
|
|
|
|
windowUnmark(win);
|
|
|
|
}
|
|
|
|
windows.active = win;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void windowClose(struct Window *win) {
|
|
|
|
if (windows.active == win) windowShow(win->next ? win->next : win->prev);
|
|
|
|
windowRemove(win);
|
|
|
|
delwin(win->log);
|
|
|
|
free(win);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void windowScroll(struct Window *win, int lines) {
|
|
|
|
if (lines < 0) {
|
|
|
|
if (win->scroll == logHeight()) return;
|
|
|
|
if (win->scroll == LogLines) windowMark(win);
|
|
|
|
win->scroll = MAX(win->scroll + lines, logHeight());
|
|
|
|
} else {
|
|
|
|
if (win->scroll == LogLines) return;
|
|
|
|
win->scroll = MIN(win->scroll + lines, LogLines);
|
|
|
|
if (win->scroll == LogLines) windowUnmark(win);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-05 18:59:51 +00:00
|
|
|
static void colorInit(void) {
|
|
|
|
start_color();
|
|
|
|
use_default_colors();
|
2019-02-22 19:11:50 +00:00
|
|
|
if (COLORS < 16) {
|
|
|
|
for (short pair = 0; pair < 0100; ++pair) {
|
|
|
|
if (pair < 010) {
|
2018-08-05 18:59:51 +00:00
|
|
|
init_pair(1 + pair, pair, -1);
|
|
|
|
} else {
|
2019-02-22 19:11:50 +00:00
|
|
|
init_pair(1 + pair, pair & 007, (pair & 070) >> 3);
|
2018-08-05 18:59:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
2019-02-22 19:11:50 +00:00
|
|
|
for (short pair = 0; pair < 0x100; ++pair) {
|
|
|
|
if (pair < 0x10) {
|
2018-08-05 18:59:51 +00:00
|
|
|
init_pair(1 + pair, pair, -1);
|
|
|
|
} else {
|
2019-02-22 19:11:50 +00:00
|
|
|
init_pair(1 + pair, pair & 0x0F, (pair & 0xF0) >> 4);
|
2018-08-05 18:59:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-08-05 00:54:50 +00:00
|
|
|
|
2019-02-22 19:11:50 +00:00
|
|
|
static attr_t colorAttr(short color) {
|
|
|
|
if (color < 0) return A_NORMAL;
|
|
|
|
if (COLORS < 16 && (color & 0x08)) return A_BOLD;
|
|
|
|
return A_NORMAL;
|
2018-08-05 18:59:51 +00:00
|
|
|
}
|
2019-02-22 19:11:50 +00:00
|
|
|
static short colorPair(short color) {
|
|
|
|
if (color < 0) return 0;
|
|
|
|
if (COLORS < 16) return 1 + ((color & 0x70) >> 1 | (color & 0x07));
|
|
|
|
return 1 + color;
|
2018-08-05 18:59:51 +00:00
|
|
|
}
|
2018-08-04 20:02:43 +00:00
|
|
|
|
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;
|
|
|
|
} ui;
|
|
|
|
|
2018-09-13 05:03:47 +00:00
|
|
|
void uiInit(void) {
|
|
|
|
initscr();
|
|
|
|
cbreak();
|
|
|
|
noecho();
|
|
|
|
termInit();
|
2019-02-22 19:11:50 +00:00
|
|
|
colorInit();
|
2018-10-22 20:09:40 +00:00
|
|
|
ui.status = newwin(1, COLS, 0, 0);
|
|
|
|
ui.input = newpad(1, 512);
|
2018-09-13 05:03:47 +00:00
|
|
|
keypad(ui.input, true);
|
|
|
|
nodelay(ui.input, true);
|
2018-12-03 20:52:04 +00:00
|
|
|
uiShow();
|
2018-09-13 05:03:47 +00:00
|
|
|
}
|
|
|
|
|
2019-02-22 19:11:50 +00:00
|
|
|
static void uiResize(void) {
|
|
|
|
wresize(ui.status, 1, COLS);
|
|
|
|
for (struct Window *win = windows.head; win; win = win->next) {
|
|
|
|
windowResize(win);
|
|
|
|
}
|
2018-09-13 05:03:47 +00:00
|
|
|
}
|
|
|
|
|
2019-02-22 19:11:50 +00:00
|
|
|
void uiShow(void) {
|
|
|
|
ui.hide = false;
|
|
|
|
termMode(TermFocus, true);
|
|
|
|
uiDraw();
|
2018-09-13 05:03:47 +00:00
|
|
|
}
|
2019-02-22 19:11:50 +00:00
|
|
|
void uiHide(void) {
|
|
|
|
ui.hide = true;
|
|
|
|
termMode(TermFocus, false);
|
|
|
|
endwin();
|
2018-09-13 05:03:47 +00:00
|
|
|
}
|
2019-02-22 19:11:50 +00:00
|
|
|
|
|
|
|
void uiExit(int status) {
|
|
|
|
uiHide();
|
|
|
|
printf(
|
|
|
|
"This program is AGPLv3 Free Software!\n"
|
|
|
|
"Code is available from <" SOURCE_URL ">.\n"
|
|
|
|
);
|
|
|
|
exit(status);
|
2018-09-13 05:03:47 +00:00
|
|
|
}
|
|
|
|
|
2019-01-25 09:18:54 +00:00
|
|
|
static int _;
|
2018-09-13 05:03:47 +00:00
|
|
|
void uiDraw(void) {
|
|
|
|
if (ui.hide) return;
|
2018-10-22 20:09:40 +00:00
|
|
|
wnoutrefresh(ui.status);
|
2019-02-22 19:11:50 +00:00
|
|
|
if (windows.active) {
|
|
|
|
pnoutrefresh(
|
|
|
|
windows.active->log,
|
|
|
|
windows.active->scroll - logHeight(), 0,
|
|
|
|
1, 0,
|
|
|
|
lastLine() - 1, lastCol()
|
|
|
|
);
|
|
|
|
}
|
2019-01-25 09:18:54 +00:00
|
|
|
int x;
|
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
|
|
|
);
|
|
|
|
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;
|
|
|
|
|
2019-02-22 19:11:50 +00:00
|
|
|
short color = -1;
|
|
|
|
if (format->fg != IRCDefault) color = Colors[format->fg];
|
|
|
|
if (format->bg != IRCDefault) color |= Colors[format->bg] << 4;
|
2018-09-13 02:55:02 +00:00
|
|
|
|
2019-02-22 19:11:50 +00:00
|
|
|
wattr_set(win, attr | colorAttr(color), colorPair(color), NULL);
|
2018-09-13 02:55:02 +00:00
|
|
|
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);
|
2019-02-22 19:11:50 +00:00
|
|
|
|
2018-09-13 00:23:45 +00:00
|
|
|
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;
|
|
|
|
|
2019-01-25 09:18:54 +00:00
|
|
|
int x, xMax;
|
2018-09-13 00:23:45 +00:00
|
|
|
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-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
|
|
|
}
|
|
|
|
|
2019-02-22 19:11:50 +00:00
|
|
|
static void title(const struct Window *win) {
|
2018-11-29 18:29:20 +00:00
|
|
|
int unread;
|
|
|
|
char *str;
|
2019-02-22 19:11:50 +00:00
|
|
|
int len = asprintf(&str, "%s%n (%u)", win->tag.name, &unread, win->unread);
|
2018-11-29 18:29:20 +00:00
|
|
|
if (len < 0) err(EX_OSERR, "asprintf");
|
2019-02-22 04:16:20 +00:00
|
|
|
if (!win->unread) str[unread] = '\0';
|
2018-11-29 18:29:20 +00:00
|
|
|
termTitle(str);
|
|
|
|
free(str);
|
|
|
|
}
|
|
|
|
|
2018-09-02 18:04:05 +00:00
|
|
|
static void uiStatus(void) {
|
2018-10-22 20:09:40 +00:00
|
|
|
wmove(ui.status, 0, 0);
|
2018-09-02 18:04:05 +00:00
|
|
|
int num = 0;
|
2019-02-22 04:16:20 +00:00
|
|
|
for (const struct Window *win = windows.head; win; win = win->next, ++num) {
|
2019-02-22 19:11:50 +00:00
|
|
|
if (!win->unread && windows.active != win) continue;
|
|
|
|
if (windows.active == win) title(win);
|
2018-09-02 18:04:05 +00:00
|
|
|
int unread;
|
|
|
|
wchar_t *str;
|
|
|
|
int len = aswprintf(
|
2018-12-08 17:58:22 +00:00
|
|
|
&str, L"%c %d %s %n(\3%02d%u\3) ",
|
2019-02-22 19:11:50 +00:00
|
|
|
(windows.active == win ? IRCReverse : IRCReset),
|
2019-02-22 04:16:20 +00:00
|
|
|
num, win->tag.name,
|
|
|
|
&unread, (win->hot ? IRCYellow : IRCDefault), win->unread
|
2018-09-02 18:04:05 +00:00
|
|
|
);
|
|
|
|
if (len < 0) err(EX_OSERR, "aswprintf");
|
2019-02-22 04:16:20 +00:00
|
|
|
if (!win->unread) str[unread] = L'\0';
|
2018-10-22 20:09:40 +00:00
|
|
|
addWrap(ui.status, str);
|
2018-09-02 18:04:05 +00:00
|
|
|
free(str);
|
|
|
|
}
|
2019-02-22 19:11:50 +00:00
|
|
|
// TODO: Put active window's topic in the rest of the status area.
|
2018-10-22 20:09:40 +00:00
|
|
|
wclrtoeol(ui.status);
|
2018-09-02 18:04:05 +00:00
|
|
|
}
|
|
|
|
|
2019-02-22 19:11:50 +00:00
|
|
|
void uiShowTag(struct Tag tag) {
|
|
|
|
windowShow(windowFor(tag));
|
2018-09-15 03:37:09 +00:00
|
|
|
uiStatus();
|
2019-02-22 19:11:50 +00:00
|
|
|
uiPrompt(false);
|
2018-09-02 18:04:05 +00:00
|
|
|
}
|
|
|
|
|
2019-02-22 19:11:50 +00:00
|
|
|
void uiShowNum(int num) {
|
|
|
|
struct Window *win = NULL;
|
2018-09-02 18:04:05 +00:00
|
|
|
if (num < 0) {
|
2019-02-22 19:11:50 +00:00
|
|
|
for (win = windows.tail; win; win = win->prev) {
|
|
|
|
if (!++num) break;
|
2018-09-02 18:04:05 +00:00
|
|
|
}
|
|
|
|
} else {
|
2019-02-22 19:11:50 +00:00
|
|
|
for (win = windows.head; win; win = win->next) {
|
|
|
|
if (!num--) break;
|
2018-09-02 18:04:05 +00:00
|
|
|
}
|
|
|
|
}
|
2019-02-22 19:11:50 +00:00
|
|
|
if (win) windowShow(win);
|
|
|
|
uiStatus();
|
|
|
|
uiPrompt(false);
|
2018-09-02 18:04:05 +00:00
|
|
|
}
|
|
|
|
|
2018-09-13 05:03:47 +00:00
|
|
|
void uiCloseTag(struct Tag tag) {
|
2019-02-22 19:11:50 +00:00
|
|
|
windowClose(windowFor(tag));
|
|
|
|
uiStatus();
|
|
|
|
uiPrompt(false);
|
2018-08-06 18:19:52 +00:00
|
|
|
}
|
|
|
|
|
2019-02-22 19:11:50 +00:00
|
|
|
static void notify(struct Tag tag, const wchar_t *str) {
|
2018-10-28 06:44:09 +00:00
|
|
|
beep();
|
|
|
|
if (!self.notify) return;
|
|
|
|
|
2019-02-22 19:11:50 +00:00
|
|
|
size_t len = 0;
|
2018-10-28 06:44:09 +00:00
|
|
|
char buf[256];
|
2019-02-22 19:11:50 +00:00
|
|
|
struct Format format = { .str = str };
|
2018-10-28 06:44:09 +00:00
|
|
|
formatReset(&format);
|
|
|
|
while (formatParse(&format, NULL)) {
|
2019-02-22 19:11:50 +00:00
|
|
|
int n = snprintf(
|
|
|
|
&buf[len], sizeof(buf) - len,
|
2018-10-28 06:44:09 +00:00
|
|
|
"%.*ls", (int)format.len, format.str
|
|
|
|
);
|
2019-02-22 19:11:50 +00:00
|
|
|
if (n < 0) err(EX_OSERR, "snprintf");
|
|
|
|
len += n;
|
|
|
|
if (len >= sizeof(buf)) break;
|
2018-10-28 06:44:09 +00:00
|
|
|
}
|
|
|
|
eventPipe((const char *[]) { "notify-send", tag.name, buf, NULL });
|
|
|
|
}
|
|
|
|
|
2019-02-22 19:11:50 +00:00
|
|
|
void uiLog(struct Tag tag, enum UIHeat heat, const wchar_t *str) {
|
|
|
|
struct Window *win = windowFor(tag);
|
2018-09-05 21:10:26 +00:00
|
|
|
int lines = 1;
|
2019-02-22 04:16:20 +00:00
|
|
|
waddch(win->log, '\n');
|
|
|
|
if (win->mark && heat > UICold) {
|
|
|
|
if (!win->unread++) {
|
2018-09-05 21:10:26 +00:00
|
|
|
lines++;
|
2019-02-22 04:16:20 +00:00
|
|
|
waddch(win->log, '\n');
|
2018-09-05 21:10:26 +00:00
|
|
|
}
|
2018-09-02 20:13:00 +00:00
|
|
|
if (heat > UIWarm) {
|
2019-02-22 04:16:20 +00:00
|
|
|
win->hot = true;
|
2019-02-22 19:11:50 +00:00
|
|
|
notify(tag, str);
|
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
|
|
|
}
|
2019-02-22 19:11:50 +00:00
|
|
|
lines += addWrap(win->log, str);
|
2019-02-22 04:16:20 +00:00
|
|
|
if (win->scroll != LogLines) win->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
|
|
|
}
|
|
|
|
|
2019-02-22 19:11:50 +00:00
|
|
|
static void keyCode(wchar_t code) {
|
|
|
|
if (code == KEY_RESIZE) uiResize();
|
|
|
|
struct Window *win = windows.active;
|
|
|
|
if (!win) return;
|
|
|
|
switch (code) {
|
|
|
|
break; case KEY_SLEFT: windowScroll(win, -1);
|
|
|
|
break; case KEY_SRIGHT: windowScroll(win, +1);
|
|
|
|
break; case KEY_PPAGE: windowScroll(win, -logHeight() / 2);
|
|
|
|
break; case KEY_NPAGE: windowScroll(win, +logHeight() / 2);
|
|
|
|
break; case KEY_LEFT: edit(win->tag, EditLeft, 0);
|
|
|
|
break; case KEY_RIGHT: edit(win->tag, EditRight, 0);
|
|
|
|
break; case KEY_HOME: edit(win->tag, EditHome, 0);
|
|
|
|
break; case KEY_END: edit(win->tag, EditEnd, 0);
|
|
|
|
break; case KEY_DC: edit(win->tag, EditDelete, 0);
|
|
|
|
break; case KEY_BACKSPACE: edit(win->tag, EditBackspace, 0);
|
|
|
|
break; case KEY_ENTER: edit(win->tag, EditEnter, 0);
|
2018-09-13 05:03:47 +00:00
|
|
|
}
|
2018-08-12 02:46:28 +00:00
|
|
|
}
|
2018-08-05 00:54:50 +00:00
|
|
|
|
2018-09-14 03:44:55 +00:00
|
|
|
static void keyChar(wchar_t ch) {
|
2019-02-22 19:11:50 +00:00
|
|
|
struct Window *win = windows.active;
|
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) {
|
2019-02-22 19:11:50 +00:00
|
|
|
break; case TermFocusIn: if (win) windowUnmark(win);
|
|
|
|
break; case TermFocusOut: if (win) windowMark(win);
|
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
|
|
|
}
|
2019-02-22 19:11:50 +00:00
|
|
|
if (ch == Del) ch = L'\b';
|
2018-08-11 23:30:30 +00:00
|
|
|
|
|
|
|
static bool meta;
|
2019-02-22 19:11:50 +00:00
|
|
|
if (ch == Esc) {
|
2018-08-11 23:30:30 +00:00
|
|
|
meta = true;
|
2018-09-14 03:44:55 +00:00
|
|
|
return;
|
2018-08-11 03:31:20 +00:00
|
|
|
}
|
2018-08-11 23:30:30 +00:00
|
|
|
if (meta) {
|
2018-09-13 05:03:47 +00:00
|
|
|
meta = false;
|
2019-02-22 19:11:50 +00:00
|
|
|
if (ch >= L'0' && ch <= L'9') uiShowNum(ch - L'0');
|
|
|
|
if (!win) return;
|
2018-08-11 03:31:20 +00:00
|
|
|
switch (ch) {
|
2019-02-22 19:11:50 +00:00
|
|
|
break; case L'b': edit(win->tag, EditBackWord, 0);
|
|
|
|
break; case L'f': edit(win->tag, EditForeWord, 0);
|
|
|
|
break; case L'\b': edit(win->tag, EditKillBackWord, 0);
|
|
|
|
break; case L'd': edit(win->tag, EditKillForeWord, 0);
|
|
|
|
break; case L'm': uiLog(win->tag, UICold, L"");
|
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
|
|
|
}
|
|
|
|
|
2019-02-22 19:11:50 +00:00
|
|
|
if (ch == CTRL(L'L')) clearok(curscr, true);
|
|
|
|
if (!win) return;
|
2018-08-05 00:54:50 +00:00
|
|
|
switch (ch) {
|
2019-02-22 19:11:50 +00:00
|
|
|
break; case CTRL(L'A'): edit(win->tag, EditHome, 0);
|
|
|
|
break; case CTRL(L'B'): edit(win->tag, EditLeft, 0);
|
|
|
|
break; case CTRL(L'D'): edit(win->tag, EditDelete, 0);
|
|
|
|
break; case CTRL(L'E'): edit(win->tag, EditEnd, 0);
|
|
|
|
break; case CTRL(L'F'): edit(win->tag, EditRight, 0);
|
|
|
|
break; case CTRL(L'K'): edit(win->tag, EditKillLine, 0);
|
|
|
|
break; case CTRL(L'W'): edit(win->tag, EditKillBackWord, 0);
|
|
|
|
|
|
|
|
break; case CTRL(L'C'): edit(win->tag, EditInsert, IRCColor);
|
|
|
|
break; case CTRL(L'N'): edit(win->tag, EditInsert, IRCReset);
|
|
|
|
break; case CTRL(L'O'): edit(win->tag, EditInsert, IRCBold);
|
|
|
|
break; case CTRL(L'R'): edit(win->tag, EditInsert, IRCColor);
|
|
|
|
break; case CTRL(L'T'): edit(win->tag, EditInsert, IRCItalic);
|
|
|
|
break; case CTRL(L'U'): edit(win->tag, EditInsert, IRCUnderline);
|
|
|
|
break; case CTRL(L'V'): edit(win->tag, EditInsert, IRCReverse);
|
|
|
|
|
|
|
|
break; case L'\b': edit(win->tag, EditBackspace, 0);
|
|
|
|
break; case L'\t': edit(win->tag, EditComplete, 0);
|
|
|
|
break; case L'\n': edit(win->tag, EditEnter, 0);
|
|
|
|
|
|
|
|
break; default: if (iswprint(ch)) edit(win->tag, EditInsert, ch);
|
2018-08-05 00:54:50 +00:00
|
|
|
}
|
2018-09-14 03:44:55 +00:00
|
|
|
}
|
|
|
|
|
2019-02-22 19:11:50 +00:00
|
|
|
void uiRead(void) {
|
|
|
|
if (ui.hide) uiShow();
|
|
|
|
int ret;
|
|
|
|
wint_t ch;
|
|
|
|
while (ERR != (ret = wget_wch(ui.input, &ch))) {
|
|
|
|
(ret == KEY_CODE_YES ? keyCode(ch) : keyChar(ch));
|
|
|
|
}
|
|
|
|
uiPrompt(false);
|
|
|
|
}
|
|
|
|
|
2018-09-14 03:44:55 +00:00
|
|
|
static bool isAction(struct Tag tag, const wchar_t *input) {
|
2018-12-05 19:46:34 +00:00
|
|
|
if (tag.id == TagStatus.id || tag.id == TagRaw.id) return false;
|
2018-09-14 03:44:55 +00:00
|
|
|
return !wcsncasecmp(input, L"/me ", 4);
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool isCommand(struct Tag tag, const wchar_t *input) {
|
2018-12-05 19:46:34 +00:00
|
|
|
if (tag.id == TagStatus.id || tag.id == TagRaw.id) return true;
|
2018-09-14 03:44:55 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2019-02-22 19:11:50 +00:00
|
|
|
void uiPrompt(bool nickChanged) {
|
|
|
|
static wchar_t *promptMesg;
|
|
|
|
static wchar_t *promptAction;
|
|
|
|
if (nickChanged || !promptMesg || !promptAction) {
|
|
|
|
free(promptMesg);
|
|
|
|
free(promptAction);
|
|
|
|
enum IRCColor color = formatColor(self.user);
|
|
|
|
int len = aswprintf(&promptMesg, L"\3%d<%s>\3 ", color, self.nick);
|
|
|
|
if (len < 0) err(EX_OSERR, "aswprintf");
|
|
|
|
len = aswprintf(&promptAction, L"\3%d* %s\3 ", color, self.nick);
|
|
|
|
if (len < 0) err(EX_OSERR, "aswprintf");
|
2018-09-14 03:44:55 +00:00
|
|
|
}
|
2019-02-22 19:11:50 +00:00
|
|
|
|
|
|
|
const wchar_t *input = editHead();
|
2018-09-14 03:44:55 +00:00
|
|
|
|
2018-09-13 05:03:47 +00:00
|
|
|
wmove(ui.input, 0, 0);
|
2019-02-22 19:11:50 +00:00
|
|
|
if (windows.active) {
|
|
|
|
if (isAction(windows.active->tag, input) && editTail() >= &input[4]) {
|
|
|
|
input = &input[4];
|
|
|
|
addWrap(ui.input, promptAction);
|
|
|
|
} else if (!isCommand(windows.active->tag, input)) {
|
|
|
|
addWrap(ui.input, promptMesg);
|
|
|
|
}
|
|
|
|
}
|
2018-09-13 05:03:47 +00:00
|
|
|
|
2019-01-25 09:18:54 +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
|
|
|
}
|