Simplify edit buffer conversion and input rendering

weechat-hashes
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, EditEnter,
}; };
void edit(size_t id, enum Edit op, wchar_t ch); void edit(size_t id, enum Edit op, wchar_t ch);
char *editHead(void); char *editBuffer(size_t *pos);
char *editTail(void);
const char *complete(size_t id, const char *prefix); const char *complete(size_t id, const char *prefix);
void completeAccept(void); void completeAccept(void);

27
edit.c
View File

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

76
ui.c
View File

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