M ansilib/inc/ansi/common.h => ansilib/inc/ansi/common.h +1 -1
@@ 1,6 1,6 @@
#ifndef __ANSI_COMMON_H_
# define __ANSI_COMMON_H_
-# define CSI "\x1b["
+# define CSI "\x1b[" /* Control Sequence Introducer */
#endif /* __ANSI_COMMON_H_ */
M ansilib/inc/ansi/cursor.h => ansilib/inc/ansi/cursor.h +1 -0
@@ 5,6 5,7 @@
typedef enum { LEFT, DOWN, UP, RIGHT } dir_t;
+point_t get_cursor_pos(void);
void move_cursor(point_t);
void nudge_cursor(dir_t, uint32_t step);
M ansilib/src/ansi/cursor.c => ansilib/src/ansi/cursor.c +22 -0
@@ 1,10 1,32 @@
#include <unistd.h>
#include <stdio.h>
+#include <stdint.h>
+#include <inttypes.h>
#include <string.h>
#include <ansi/common.h>
#include <ansi/cursor.h>
+point_t get_cursor_pos(void)
+{
+ char buf[16];
+ point_t ret = { 0 };
+
+ /* ESC[6n request cursor position (reports as ESC[#;#R) */
+
+ write(STDOUT_FILENO, CSI"6n", 4);
+ if ( read(STDIN_FILENO, buf, 16) ) {
+
+ uint32_t line, column;
+ sscanf(buf, CSI"%"PRIu32"i;%"PRIu32"iR", &line, &column);
+
+ ret.x = column;
+ ret.y = line;
+ }
+
+ return ret;
+}
+
void move_cursor(point_t pos)
{
char buf[16];
M src/main.c => src/main.c +8 -1
@@ 4,6 4,7 @@
#include <termios.h>
#include <unistd.h>
+#include <ansi/cursor.h>
#include <graphics/common.h>
#include <graphics/shapes.h>
@@ 68,7 69,13 @@ int main(void)
* }
*/
- draw_rectangle((point_t) { 2, 2 }, 32, 16);
+ /* save current cursor position */
+ point_t orig_cursor_pos = get_cursor_pos();
+
+ draw_rectangle((point_t) { 20, 40 }, 32, 16);
+
+ /* Return to the original cursor position */
+ move_cursor(orig_cursor_pos);
while ( 1 ) {