Browse code

Refactor into tty_newline and tty_setcol

John Hawthorn authored on 17/08/2014 03:31:11
Showing 3 changed files

  • fzy.c index 69a62da..96b810e 100644
  • tty.c index e15ce31..a96aafc 100644
  • tty.h index 5c69073..fa32203 100644
... ...
@@ -89,13 +89,13 @@ int search_size;
89 89
 char search[SEARCH_SIZE_MAX + 1] = {0};
90 90
 
91 91
 void clear(tty_t *tty){
92
-	fprintf(tty->fout, "%c%c0G", 0x1b, '[');
92
+	tty_setcol(tty, 0);
93 93
 	int line = 0;
94 94
 	while(line++ < NUMLINES + 1){
95
-		fprintf(tty->fout, "%c%cK\n", 0x1b, '[');
95
+		tty_newline(tty);
96 96
 	}
97 97
 	fprintf(tty->fout, "%c%c%iA", 0x1b, '[', line-1);
98
-	fprintf(tty->fout, "%c%c0G", 0x1b, '[');
98
+	tty_setcol(tty, 0);
99 99
 }
100 100
 
101 101
 #define TTY_COLOR_HIGHLIGHT TTY_COLOR_YELLOW
... ...
@@ -138,7 +138,7 @@ void draw(tty_t *tty){
138 138
 		line++;
139 139
 	}
140 140
 	fprintf(tty->fout, "%c%c%iA", 0x1b, '[', line + 1);
141
-	fprintf(tty->fout, "%c%c%ziG", 0x1b, '[', strlen(prompt) + strlen(search) + 1);
141
+	tty_setcol(tty, strlen(prompt) + strlen(search) + 1);
142 142
 	fflush(tty->fout);
143 143
 }
144 144
 
... ...
@@ -59,6 +59,14 @@ void tty_setnormal(tty_t *tty){
59 59
 	tty->fgcolor = 9;
60 60
 }
61 61
 
62
+void tty_newline(tty_t *tty){
63
+	tty_printf(tty, "%c%cK\n", 0x1b, '[');
64
+}
65
+
66
+void tty_setcol(tty_t *tty, int col){
67
+	tty_printf(tty, "%c%c%iG", 0x1b, '[', col);
68
+}
69
+
62 70
 void tty_printf(tty_t *tty, const char *fmt, ...){
63 71
 	va_list args;
64 72
 	va_start(args, fmt);
... ...
@@ -28,6 +28,14 @@ void tty_setnormal(tty_t *tty);
28 28
 #define TTY_COLOR_WHITE   7
29 29
 #define TTY_COLOR_NORMAL  9
30 30
 
31
+/* tty_newline
32
+ * Move cursor to the beginning of the next line, clearing to the end of the
33
+ * current line
34
+ */
35
+void tty_newline(tty_t *tty);
36
+
37
+void tty_setcol(tty_t *tty, int col);
38
+
31 39
 void tty_printf(tty_t *tty, const char *fmt, ...);
32 40
 
33 41
 #endif