about summary refs log tree commit diff homepage
path: root/client.c
diff options
context:
space:
mode:
authorJune McEnroe <june@causal.agency>2018-07-21 13:48:43 -0400
committerJune McEnroe <june@causal.agency>2018-07-21 13:48:43 -0400
commit4d8a54fd364982cb18241b18fe7739c0b6c0c143 (patch)
tree3bf2d2502d4d1777b960507145c2b21da05019fb /client.c
parentAdd ARRAY_LEN macro (diff)
downloadtorus-4d8a54fd364982cb18241b18fe7739c0b6c0c143.tar.gz
torus-4d8a54fd364982cb18241b18fe7739c0b6c0c143.zip
Refactor readInput for each mode
Diffstat (limited to 'client.c')
-rw-r--r--client.c119
1 files changed, 63 insertions, 56 deletions
diff --git a/client.c b/client.c
index ea1f6f5..fec7fb4 100644
--- a/client.c
+++ b/client.c
@@ -137,61 +137,7 @@ static void swapCell(int8_t dx, int8_t dy) {
 	clientPut(aColor, aCell);
 }
 
-static void readInput(void) {
-	int c = getch();
-
-	if (input.mode == MODE_INSERT) {
-		if (c == ESC) {
-			input.mode = MODE_NORMAL;
-			clientMove(-input.dx, -input.dy);
-		} else if (!input.dx && !input.dy) {
-			switch (c) {
-				break; case 'h': insertMode(-1,  0);
-				break; case 'j': insertMode( 0,  1);
-				break; case 'k': insertMode( 0, -1);
-				break; case 'l': insertMode( 1,  0);
-				break; case 'y': insertMode(-1, -1);
-				break; case 'u': insertMode( 1, -1);
-				break; case 'b': insertMode(-1,  1);
-				break; case 'n': insertMode( 1,  1);
-			}
-		} else if (c == '\b' || c == DEL) {
-			clientMove(-input.dx, -input.dy);
-			clientPut(input.color, ' ');
-			input.len--;
-		} else if (c == '\n') {
-			clientMove(input.dy, input.dx);
-			clientMove(-input.dx * input.len, -input.dy * input.len);
-			input.len = 0;
-		} else if (isprint(c)) {
-			clientPut(input.color, c);
-			clientMove(input.dx, input.dy);
-			input.len++;
-		}
-		return;
-	}
-
-	if (input.mode == MODE_REPLACE) {
-		if (isprint(c)) clientPut(attrsColor(inch()), c);
-		input.mode = MODE_NORMAL;
-		return;
-	}
-
-	if (input.mode == MODE_PUT) {
-		if (isprint(c)) clientPut(input.color, c);
-		input.mode = MODE_NORMAL;
-		return;
-	}
-
-	if (input.mode == MODE_DRAW && !input.draw) {
-		if (c == ESC) input.mode = MODE_NORMAL;
-		if (isprint(c)) {
-			input.draw = c;
-			clientPut(input.color, c);
-		}
-		return;
-	}
-
+static void inputNormal(int c) {
 	switch (c) {
 		break; case ESC: input.mode = MODE_NORMAL;
 
@@ -267,8 +213,69 @@ static void readInput(void) {
 		break; case KEY_UP:    clientMove( 0, -1);
 		break; case KEY_RIGHT: clientMove( 1,  0);
 	}
+}
 
-	if (input.mode == MODE_DRAW && input.draw) clientPut(input.color, input.draw);
+static void inputInsert(int c) {
+	if (c == ESC) {
+		input.mode = MODE_NORMAL;
+		clientMove(-input.dx, -input.dy);
+	} else if (!input.dx && !input.dy) {
+		switch (c) {
+			break; case 'h': insertMode(-1,  0);
+			break; case 'j': insertMode( 0,  1);
+			break; case 'k': insertMode( 0, -1);
+			break; case 'l': insertMode( 1,  0);
+			break; case 'y': insertMode(-1, -1);
+			break; case 'u': insertMode( 1, -1);
+			break; case 'b': insertMode(-1,  1);
+			break; case 'n': insertMode( 1,  1);
+		}
+	} else if (c == '\b' || c == DEL) {
+		clientMove(-input.dx, -input.dy);
+		clientPut(input.color, ' ');
+		input.len--;
+	} else if (c == '\n') {
+		clientMove(input.dy, input.dx);
+		clientMove(-input.dx * input.len, -input.dy * input.len);
+		input.len = 0;
+	} else if (isprint(c)) {
+		clientPut(input.color, c);
+		clientMove(input.dx, input.dy);
+		input.len++;
+	}
+}
+
+static void inputReplace(int c) {
+	if (isprint(c)) clientPut(attrsColor(inch()), c);
+	input.mode = MODE_NORMAL;
+}
+
+static void inputPut(int c) {
+	if (isprint(c)) clientPut(input.color, c);
+	input.mode = MODE_NORMAL;
+}
+
+static void inputDraw(int c) {
+	if (input.draw) {
+		inputNormal(c);
+		clientPut(input.color, input.draw);
+	} else if (isprint(c)) {
+		input.draw = c;
+		clientPut(input.color, c);
+	} else if (c == ESC) {
+		input.mode = MODE_NORMAL;
+	}
+}
+
+static void readInput(void) {
+	int c = getch();
+	switch (input.mode) {
+		break; case MODE_NORMAL:  inputNormal(c);
+		break; case MODE_INSERT:  inputInsert(c);
+		break; case MODE_REPLACE: inputReplace(c);
+		break; case MODE_PUT:     inputPut(c);
+		break; case MODE_DRAW:    inputDraw(c);
+	}
 }
 
 static void serverPut(uint8_t x, uint8_t y, uint8_t color, char cell) {
w=1'>Bump 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