summary refs log tree commit diff
diff options
context:
space:
mode:
authorJune McEnroe <june@causal.agency>2018-08-06 22:14:59 -0400
committerJune McEnroe <june@causal.agency>2018-08-06 22:14:59 -0400
commitd71d0409951203d333a354c991073d18f98b8fd1 (patch)
tree5bf405547877cc26edf8301cd722944014453bdc
parentFix allocation size in vaswprintf (diff)
downloadcatgirl-d71d0409951203d333a354c991073d18f98b8fd1.tar.gz
catgirl-d71d0409951203d333a354c991073d18f98b8fd1.zip
Rewrite line editing again, add formatting
-rw-r--r--ui.c86
1 files changed, 47 insertions, 39 deletions
diff --git a/ui.c b/ui.c
index 9c77668..94d9a9b 100644
--- a/ui.c
+++ b/ui.c
@@ -110,7 +110,7 @@ void uiInit(void) {
 
 	ui.input = newpad(2, INPUT_COLS);
 	mvwhline(ui.input, 0, 0, ACS_HLINE, INPUT_COLS);
-	wmove(ui.input, 1, ui.cursor);
+	wmove(ui.input, 1, 0);
 
 	keypad(ui.input, true);
 	nodelay(ui.input, true);
@@ -142,9 +142,11 @@ void uiDraw(void) {
 		2, 0,
 		lastLine() - 2, lastCol()
 	);
+	int _, x;
+	getyx(ui.input, _, x);
 	pnoutrefresh(
 		ui.input,
-		0, MAX(0, ui.cursor - lastCol() + 1),
+		0, MAX(0, x - lastCol() + 5),
 		lastLine() - 1, 0,
 		lastLine(), lastCol()
 	);
@@ -277,69 +279,58 @@ static void logDown(void) {
 
 static struct {
 	wchar_t buf[512];
-	size_t len;
-} line;
+	wchar_t *ptr;
+	wchar_t *end;
+} line = { .ptr = line.buf, .end = line.buf };
 static const size_t BUF_LEN = sizeof(line.buf) / sizeof(line.buf[0]);
 
 static void left(void) {
-	if (ui.cursor) ui.cursor--;
+	if (line.ptr > line.buf) line.ptr--;
 }
 static void right(void) {
-	if (ui.cursor < line.len) ui.cursor++;
+	if (line.ptr < line.end) line.ptr++;
 }
 static void home(void) {
-	ui.cursor = 0;
+	line.ptr = line.buf;
 }
 static void end(void) {
-	ui.cursor = line.len;
+	line.ptr = line.end;
 }
 
 static void kill(void) {
-	line.len = ui.cursor;
+	line.end = line.ptr;
 }
 
 static void insert(wchar_t ch) {
-	if (!iswprint(ch)) return;
-	if (line.len == BUF_LEN - 1) return;
-	if (ui.cursor == line.len) {
-		line.buf[line.len] = ch;
-	} else {
-		wmemmove(
-			&line.buf[ui.cursor + 1],
-			&line.buf[ui.cursor],
-			line.len - ui.cursor
-		);
-		line.buf[ui.cursor] = ch;
+	if (line.end == &line.buf[BUF_LEN - 1]) return;
+	if (line.ptr != line.end) {
+		wmemmove(line.ptr + 1, line.ptr, line.end - line.ptr);
 	}
-	line.len++;
-	ui.cursor++;
+	*line.ptr++ = ch;
+	line.end++;
 }
 
 static void backspace(void) {
-	if (!ui.cursor) return;
-	if (ui.cursor != line.len) {
-		wmemmove(
-			&line.buf[ui.cursor - 1],
-			&line.buf[ui.cursor],
-			line.len - ui.cursor
-		);
+	if (line.ptr == line.buf) return;
+	if (line.ptr != line.end) {
+		wmemmove(line.ptr - 1, line.ptr, line.end - line.ptr);
 	}
-	line.len--;
-	ui.cursor--;
+	line.ptr--;
+	line.end--;
 }
 
 static void delete(void) {
-	if (ui.cursor == line.len) return;
+	if (line.ptr == line.end) return;
 	right();
 	backspace();
 }
 
 static void enter(void) {
-	if (!line.len) return;
-	line.buf[line.len] = '\0';
+	if (line.end == line.buf) return;
+	*line.end = L'\0';
 	input(line.buf);
-	line.len = 0;
-	ui.cursor = 0;
+	line.ptr = line.buf;
+	line.end = line.buf;
 }
 
 static void keyChar(wint_t ch) {
@@ -354,7 +345,12 @@ static void keyChar(wint_t ch) {
 		break; case '\b':      backspace();
 		break; case '\177':    backspace();
 		break; case '\n':      enter();
-		break; default:        insert(ch);
+		break; case CTRL('O'): insert(L'\2');
+		break; case CTRL('C'): insert(L'\3');
+		break; case CTRL('R'): insert(L'\3');
+		break; case CTRL('I'): insert(L'\35');
+		break; case CTRL('U'): insert(L'\37');
+		break; default: if (iswprint(ch)) insert(ch);
 	}
 }
 
@@ -383,8 +379,20 @@ void uiRead(void) {
 			keyChar(ch);
 		}
 	}
+
 	wmove(ui.input, 1, 0);
-	waddnwstr(ui.input, line.buf, line.len);
+
+	ch = *line.ptr;
+	*line.ptr = L'\0';
+	addIRC(ui.input, line.buf);
+	*line.ptr = ch;
+
+	int _, x;
+	getyx(ui.input, _, x);
+
+	*line.end = L'\0';
+	addIRC(ui.input, line.ptr);
+
 	wclrtoeol(ui.input);
-	wmove(ui.input, 1, ui.cursor);
+	wmove(ui.input, 1, x);
 }
rowth or change. I suppose this book had a bit of one, but only in the last two chapters. 2022-07-30Add Normal PeopleJune McEnroe Unbearably straight. Eyerolls and sighs per page off the charts. Shout out to Joanna, I guess. I kinda like the lack of quotation marks though to be honest. After half of the Ruth Ozeki novel and now this, I need to get back to some genre fiction. 2022-07-26Rewrite glitch from new pngoJune McEnroe 2022-07-26Update Care with time-to-ID and piercingsJune McEnroe 2022-07-26Add -w to upJune McEnroe 2022-07-13Set push.autoSetupRemoteJune McEnroe 2022-07-08Remove TOURJune McEnroe There is not that much distinct stuff here anymore. 2022-07-03Add The Bone Shard EmperorJune McEnroe Suffers a little bit from middle book but I really enjoyed it. Read it faster than the first one too, despite its length. 2022-06-25Bump xterm font size to 12June McEnroe 2022-06-10Handle subshells (and functions) inside substitutionsJune McEnroe 2022-06-10Switch to jorts Install scriptJune McEnroe 2022-06-08Indicate if still reading or no resultsJune McEnroe 2022-06-08Add Maiden, Mother, CroneJune McEnroe Mixed bag like most collections of short stories. Some of them are pretty good. The author of the worst written story also has the worst written bio. 2022-06-05FIRST SHOW IN 2.5 YEARS BABEY!!!June McEnroe 2022-06-03Set line number on File linesJune McEnroe 2022-06-03Stop polling stdin after EOFJune McEnroe 2022-06-02Set TABSIZE=4June McEnroe Absolutely indiscriminately. 2022-06-02Do basic match highlightingJune McEnroe 2022-06-02Clean up parsing a littleJune McEnroe 2022-06-02Don't duplicate path stringJune McEnroe 2022-06-02Use stderr instead of /dev/tty, realloc buffer if lines too longJune McEnroe For some reason I haven't been able to figure out, trying to poll /dev/tty returns POLLNVAL (and this was using 100% CPU looping), but using stderr instead works fine. 2022-06-02Add initial working version of qfJune McEnroe 2022-05-29Set prompt for okshJune McEnroe