summary refs log tree commit diff homepage
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
parentAdd ARRAY_LEN macro (diff)
downloadtorus-4d8a54fd364982cb18241b18fe7739c0b6c0c143.tar.gz
torus-4d8a54fd364982cb18241b18fe7739c0b6c0c143.zip
Refactor readInput for each mode
Diffstat (limited to '')
-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) {
r class='logheader'>2021-09-21Rewrite pngo, add explicit optionsJune McEnroe Interesting to see how my code habits have changed. 2021-09-16Fix /* **/ comment matchingJune McEnroe 2021-09-15Remove typer, add downgrade to READMEJune McEnroe 2021-09-15Set bot mode on downgradeJune McEnroe 2021-09-15Enter capsicum in downgradeJune McEnroe 2021-09-15Factor out common parts of downgrade messagesJune McEnroe Also bump the message cap to 1024 because that is ostensibly useful for replying to older messages. 2021-09-14Add downgrade IRC botJune McEnroe 2021-09-14Sort by title if authors matchJune McEnroe There are probably better things to sort by but title definitely always exists. 2021-09-13Swap-remove tags as they're foundJune McEnroe This makes it even faster. From ~1s on a sqlite3.c amalgamation to ~0.85s. 2021-09-12Replace htagml regex with strncmpJune McEnroe Since ctags only ever produces regular expressions of the form /^re$/ or /^re/ with no other special characters, instead unescape the pattern and simply use strncmp. Running on a sqlite3.c amalgamation, the regex version takes ~37s while the strncmp version takes ~1s, producing identical output. Big win! 2021-09-11Also defer printing comment for lone close-parensJune McEnroe 2021-09-10Publish "git-comment"June McEnroe 2021-09-10Add git comment --pretty optionJune McEnroe 2021-09-08Defer printing comment if line is blank or closing braceJune McEnroe This fixes badly indented comments. 2021-09-08Up default min-repeat to 30 linesJune McEnroe 2021-09-08Handle dirty lines in git-commentJune McEnroe 2021-09-08Document and install git-commentJune McEnroe 2021-09-08Add repeat and all options to git-commentJune McEnroe 2021-09-08Add group threshold to git-commentJune McEnroe