Simplify edit buffer conversion and input rendering

master
C. McEnroe 2020-02-09 01:28:24 -05:00
parent a212a7ae2c
commit 2d62ea9e30
3 changed files with 56 additions and 50 deletions

3
chat.h
View File

@ -156,8 +156,7 @@ enum Edit {
EditEnter,
};
void edit(size_t id, enum Edit op, wchar_t ch);
char *editHead(void);
char *editTail(void);
char *editBuffer(size_t *pos);
const char *complete(size_t id, const char *prefix);
void completeAccept(void);

27
edit.c
View File

@ -27,21 +27,22 @@ static wchar_t buf[Cap];
static size_t len;
static size_t pos;
char *editHead(void) {
char *editBuffer(size_t *mbsPos) {
static char mbs[MB_LEN_MAX * Cap];
const wchar_t *ptr = buf;
size_t n = wcsnrtombs(mbs, &ptr, pos, sizeof(mbs) - 1, NULL);
assert(n != (size_t)-1);
mbs[n] = '\0';
return mbs;
}
char *editTail(void) {
static char mbs[MB_LEN_MAX * Cap];
const wchar_t *ptr = &buf[pos];
size_t n = wcsnrtombs(mbs, &ptr, len - pos, sizeof(mbs) - 1, NULL);
const wchar_t *ptr = buf;
size_t mbsLen = wcsnrtombs(mbs, &ptr, pos, sizeof(mbs) - 1, NULL);
assert(mbsLen != (size_t)-1);
if (mbsPos) *mbsPos = mbsLen;
ptr = &buf[pos];
size_t n = wcsnrtombs(
&mbs[mbsLen], &ptr, len - pos, sizeof(mbs) - mbsLen - 1, NULL
);
assert(n != (size_t)-1);
mbs[n] = '\0';
mbsLen += n;
mbs[mbsLen] = '\0';
return mbs;
}
@ -78,7 +79,7 @@ void edit(size_t id, enum Edit op, wchar_t ch) {
}
break; case EditEnter: {
pos = 0;
command(id, editTail());
command(id, editBuffer(NULL));
len = 0;
}
}

76
ui.c
View File

@ -507,48 +507,54 @@ static void inputAdd(struct Style *style, const char *str) {
static void inputUpdate(void) {
size_t id = windows.active->id;
const char *nick = self.nick;
const char *head = editHead();
size_t pos;
char *buf = editBuffer(&pos);
const char *skip = NULL;
const char *pre = "";
const char *suf = " ";
struct Style style = { .fg = self.color, .bg = Default };
struct Style reset = Reset;
if (NULL != (skip = commandIsPrivmsg(id, head))) {
pre = "<";
suf = "> ";
} else if (NULL != (skip = commandIsNotice(id, head))) {
pre = "-";
suf = "- ";
reset.fg = LightGray;
} else if (NULL != (skip = commandIsAction(id, head))) {
style.attr |= A_ITALIC;
pre = "* ";
reset.attr |= A_ITALIC;
struct Style init = { .fg = self.color, .bg = Default };
struct Style rest = Reset;
const char *prefix = "";
const char *prompt = (self.nick ? self.nick : "");
const char *suffix = "";
if (NULL != (skip = commandIsPrivmsg(id, buf))) {
prefix = "<"; suffix = "> ";
} else if (NULL != (skip = commandIsNotice(id, buf))) {
prefix = "-"; suffix = "- ";
rest.fg = LightGray;
} else if (NULL != (skip = commandIsAction(id, buf))) {
init.attr |= A_ITALIC;
prefix = "* "; suffix = " ";
rest.attr |= A_ITALIC;
} else if (id == Debug) {
skip = head;
style.fg = Gray;
pre = "<<";
nick = NULL;
skip = buf;
init.fg = Gray;
prompt = "<< ";
} else {
prompt = "";
}
if (skip && skip > &buf[pos]) {
skip = NULL;
prefix = prompt = suffix = "";
}
int y, x;
wmove(input, 0, 0);
if (skip) {
wattr_set(
input,
style.attr | colorAttr(mapColor(style.fg)),
colorPair(mapColor(style.fg), mapColor(style.bg)),
NULL
);
waddstr(input, pre);
if (nick) waddstr(input, nick);
waddstr(input, suf);
}
style = reset;
inputAdd(&style, (skip ? skip : head));
wattr_set(
input,
init.attr | colorAttr(mapColor(init.fg)),
colorPair(mapColor(init.fg), mapColor(init.bg)),
NULL
);
waddstr(input, prefix);
waddstr(input, prompt);
waddstr(input, suffix);
struct Style style = rest;
char p = buf[pos];
buf[pos] = '\0';
inputAdd(&style, (skip ? skip : buf));
getyx(input, y, x);
inputAdd(&style, editTail());
buf[pos] = p;
inputAdd(&style, &buf[pos]);
wclrtoeol(input);
wmove(input, y, x);
}