Add key bindings for IRC formatting

master
C. McEnroe 2020-02-07 02:46:40 -05:00
parent 86ee56ec45
commit 4343f35f9c
2 changed files with 77 additions and 17 deletions

View File

@ -161,6 +161,47 @@ Insert a blank line in the window.
Switch to window by number 0\(en9. Switch to window by number 0\(en9.
.El .El
. .
.Ss IRC Formatting
.Bl -tag -width Ds -compact
.It Ic C-z b
Toggle bold.
.It Ic C-z c
Set or reset color.
.It Ic C-z i
Toggle italics.
.It Ic C-z o
Reset formatting.
.It Ic C-z r
Toggle reverse color.
.It Ic C-z u
Toggle underline.
.El
.
.Pp
To set colors, follow
.Ic C-z c
by one or two digits for the foreground color,
optionally followed by a comma
and one or two digits for the background color.
To reset color, follow
.Ic C-z c
by a non-digit.
.
.Pp
The color numbers are as follows:
.Pp
.Bl -column "99" "orange (dark yellow)" "15" "pink (light magenta)"
.It \ 0 Ta white Ta \ 8 Ta yellow
.It \ 1 Ta black Ta \ 9 Ta light green
.It \ 2 Ta blue Ta 10 Ta cyan
.It \ 3 Ta green Ta 11 Ta light cyan
.It \ 4 Ta red Ta 12 Ta light blue
.It \ 5 Ta brown (dark red) Ta 13 Ta pink (light magenta)
.It \ 6 Ta magenta Ta 14 Ta gray
.It \ 7 Ta orange (dark yellow) Ta 15 Ta light gray
.It 99 Ta default
.El
.
.Sh FILES .Sh FILES
.Bl -tag -width Ds .Bl -tag -width Ds
.It Pa $XDG_CONFIG_DIRS/catgirl .It Pa $XDG_CONFIG_DIRS/catgirl

53
ui.c
View File

@ -261,14 +261,16 @@ static short mapColor(enum Color color) {
} }
} }
enum { B = '\2', C = '\3', O = '\17', R = '\26', I = '\35', U = '\37' };
static void styleParse(struct Style *style, const char **str, size_t *len) { static void styleParse(struct Style *style, const char **str, size_t *len) {
switch (**str) { switch (**str) {
break; case '\2': (*str)++; style->attr ^= A_BOLD; break; case B: (*str)++; style->attr ^= A_BOLD;
break; case '\17': (*str)++; *style = Reset; break; case O: (*str)++; *style = Reset;
break; case '\26': (*str)++; style->attr ^= A_REVERSE; break; case R: (*str)++; style->attr ^= A_REVERSE;
break; case '\35': (*str)++; style->attr ^= A_ITALIC; break; case I: (*str)++; style->attr ^= A_ITALIC;
break; case '\37': (*str)++; style->attr ^= A_UNDERLINE; break; case U: (*str)++; style->attr ^= A_UNDERLINE;
break; case '\3': { break; case C: {
(*str)++; (*str)++;
if (!isdigit(**str)) { if (!isdigit(**str)) {
style->fg = Default; style->fg = Default;
@ -283,7 +285,7 @@ static void styleParse(struct Style *style, const char **str, size_t *len) {
if (isdigit(**str)) style->bg = style->bg * 10 + *(*str)++ - '0'; if (isdigit(**str)) style->bg = style->bg * 10 + *(*str)++ - '0';
} }
} }
*len = strcspn(*str, "\2\3\17\26\35\37"); *len = strcspn(*str, (const char[]) { B, C, O, R, I, U, '\0' });
} }
static void statusAdd(const char *str) { static void statusAdd(const char *str) {
@ -456,12 +458,12 @@ static void inputAdd(struct Style *style, const char *str) {
styleParse(style, &str, &len); styleParse(style, &str, &len);
wattr_set(input, A_BOLD | A_REVERSE, 0, NULL); wattr_set(input, A_BOLD | A_REVERSE, 0, NULL);
switch (*code) { switch (*code) {
break; case '\2': waddch(input, 'B'); break; case B: waddch(input, 'B');
break; case '\3': waddch(input, 'C'); break; case C: waddch(input, 'C');
break; case '\17': waddch(input, 'O'); break; case O: waddch(input, 'O');
break; case '\26': waddch(input, 'R'); break; case R: waddch(input, 'R');
break; case '\35': waddch(input, 'I'); break; case I: waddch(input, 'I');
break; case '\37': waddch(input, 'U'); break; case U: waddch(input, 'U');
} }
if (str - code > 1) waddnstr(input, &code[1], str - &code[1]); if (str - code > 1) waddnstr(input, &code[1], str - &code[1]);
wattr_set( wattr_set(
@ -574,7 +576,7 @@ static void keyMeta(wchar_t ch) {
static void keyCtrl(wchar_t ch) { static void keyCtrl(wchar_t ch) {
size_t id = windows.active->id; size_t id = windows.active->id;
switch (ch) { switch (ch ^ L'@') {
break; case L'?': edit(id, EditErase, 0); break; case L'?': edit(id, EditErase, 0);
break; case L'A': edit(id, EditHome, 0); break; case L'A': edit(id, EditHome, 0);
break; case L'E': edit(id, EditEnd, 0); break; case L'E': edit(id, EditEnd, 0);
@ -585,10 +587,22 @@ static void keyCtrl(wchar_t ch) {
} }
} }
static void keyStyle(wchar_t ch) {
size_t id = windows.active->id;
switch (iswcntrl(ch) ? ch ^ L'@' : towupper(ch)) {
break; case L'B': edit(id, EditInsert, B);
break; case L'C': edit(id, EditInsert, C);
break; case L'I': edit(id, EditInsert, I);
break; case L'O': edit(id, EditInsert, O);
break; case L'R': edit(id, EditInsert, R);
break; case L'U': edit(id, EditInsert, U);
}
}
void uiRead(void) { void uiRead(void) {
int ret; int ret;
wint_t ch; wint_t ch;
static bool meta; static bool meta, style;
while (ERR != (ret = wget_wch(input, &ch))) { while (ERR != (ret = wget_wch(input, &ch))) {
if (ret == KEY_CODE_YES) { if (ret == KEY_CODE_YES) {
keyCode(ch); keyCode(ch);
@ -597,12 +611,17 @@ void uiRead(void) {
continue; continue;
} else if (meta) { } else if (meta) {
keyMeta(ch); keyMeta(ch);
} else if (ch == (L'Z' ^ L'@')) {
style = true;
continue;
} else if (style) {
keyStyle(ch);
} else if (iswcntrl(ch)) { } else if (iswcntrl(ch)) {
keyCtrl(ch ^ L'@'); keyCtrl(ch);
} else { } else {
edit(windows.active->id, EditInsert, ch); edit(windows.active->id, EditInsert, ch);
} }
meta = false; meta = style = false;
} }
inputUpdate(); inputUpdate();
} }