summary refs log tree commit diff
diff options
context:
space:
mode:
authorJune McEnroe <june@causal.agency>2019-07-11 17:37:31 -0400
committerJune McEnroe <june@causal.agency>2019-07-11 17:37:31 -0400
commit9bc11116890bf5f33d3a32d5efe97167fa8056ce (patch)
tree5caf068d7a88951c78643a86c783d20715140a39
parentAdd bright option to shotty (diff)
downloadsrc-9bc11116890bf5f33d3a32d5efe97167fa8056ce.tar.gz
src-9bc11116890bf5f33d3a32d5efe97167fa8056ce.zip
Factor out clear
Diffstat (limited to '')
-rw-r--r--bin/shotty.c38
1 files changed, 18 insertions, 20 deletions
diff --git a/bin/shotty.c b/bin/shotty.c
index a70f40b8..e5f05bbf 100644
--- a/bin/shotty.c
+++ b/bin/shotty.c
@@ -58,6 +58,13 @@ static struct Cell *cell(uint y, uint x) {
 	return &cells[y * cols + x];
 }
 
+static void clear(struct Cell *a, struct Cell *b) {
+	for (; a <= b; ++a) {
+		a->style = style;
+		a->ch = ' ';
+	}
+}
+
 static char updateNUL(wchar_t ch) {
 	switch (ch) {
 		case BS: if (x) x--; return NUL;
@@ -156,24 +163,18 @@ static char updateCSI(wchar_t ch) {
 		}
 
 		break; case ED: {
-			struct Cell *from = cell(0, 0);
-			struct Cell *to = cell(rows - 1, cols - 1);
-			if (ps[0] == 0) from = cell(y, x);
-			if (ps[0] == 1) to = cell(y, x);
-			for (struct Cell *clear = from; clear <= to; ++clear) {
-				clear->style = style;
-				clear->ch = ' ';
-			}
+			struct Cell *a = cell(0, 0);
+			struct Cell *b = cell(rows - 1, cols - 1);
+			if (ps[0] == 0) a = cell(y, x);
+			if (ps[0] == 1) b = cell(y, x);
+			clear(a, b);
 		}
 		break; case EL: {
-			struct Cell *from = cell(y, 0);
-			struct Cell *to = cell(y, cols - 1);
-			if (ps[0] == 0) from = cell(y, x);
-			if (ps[0] == 1) to = cell(y, x);
-			for (struct Cell *clear = from; clear <= to; ++clear) {
-				clear->style = style;
-				clear->ch = ' ';
-			}
+			struct Cell *a = cell(y, 0);
+			struct Cell *b = cell(y, cols - 1);
+			if (ps[0] == 0) a = cell(y, x);
+			if (ps[0] == 1) b = cell(y, x);
+			clear(a, b);
 		}
 
 		break; case SM: // ignore
@@ -287,10 +288,7 @@ int main(int argc, char *argv[]) {
 	if (!cells) err(EX_OSERR, "calloc");
 
 	style = def;
-	for (uint i = 0; i < rows * cols; ++i) {
-		cells[i].style = style;
-		cells[i].ch = ' ';
-	}
+	clear(cell(0, 0), cell(rows - 1, cols - 1));
 
 	wint_t ch;
 	while (WEOF != (ch = getwc(file))) {