From 15b2358ba963e1d793d63bee6a40f4a759fc7ae9 Mon Sep 17 00:00:00 2001 From: Aritra Sarkar Date: Fri, 19 Nov 2021 12:02:53 +0530 Subject: [PATCH] Move one of the rects with hjkl --- ansilib/inc/graphics/box.h | 2 ++ ansilib/inc/graphics/shapes.h | 2 +- ansilib/src/graphics/box.c | 35 ++++++++++++++++++++++++++++++++++- ansilib/src/graphics/shapes.c | 10 +++++----- src/main.c | 32 +++++++++++++++++++++----------- 5 files changed, 63 insertions(+), 18 deletions(-) diff --git a/ansilib/inc/graphics/box.h b/ansilib/inc/graphics/box.h index b1f149c..0bb431d 100644 --- a/ansilib/inc/graphics/box.h +++ b/ansilib/inc/graphics/box.h @@ -14,5 +14,7 @@ typedef struct { } box_t; void draw(const box_t *box); +void erase(const box_t *box); +void nudge(box_t *box, dir_t dir, int32_t steps); #endif /* __GRAPHICS_BOX_H_ */ diff --git a/ansilib/inc/graphics/shapes.h b/ansilib/inc/graphics/shapes.h index 54a5d3a..3a2e2ed 100644 --- a/ansilib/inc/graphics/shapes.h +++ b/ansilib/inc/graphics/shapes.h @@ -5,6 +5,6 @@ # include -void draw_rectangle(uint32_t width, uint32_t height); +void draw_rectangle(uint32_t width, uint32_t height, char ch); #endif /* __GRAPHICS_SHAPES_H_ */ diff --git a/ansilib/src/graphics/box.c b/ansilib/src/graphics/box.c index 21bc952..ab1b20e 100644 --- a/ansilib/src/graphics/box.c +++ b/ansilib/src/graphics/box.c @@ -1,3 +1,5 @@ +#include + #include #include #include @@ -7,7 +9,38 @@ void draw(const box_t *box) switch ( box->type ) { case RECTANGLE: move_cursor(box->origin); - draw_rectangle(box->width, box->height); + draw_rectangle(box->width, box->height, '#'); + break; + } +} + +void erase(const box_t *box) +{ + switch ( box->type ) { + case RECTANGLE: + move_cursor(box->origin); + draw_rectangle(box->width, box->height, ' '); + break; + } +} + +void nudge(box_t *box, dir_t dir, int32_t steps) +{ + switch ( dir ) { + case UP: + box->origin.y -= steps; + break; + + case DOWN: + box->origin.y += steps; + break; + + case LEFT: + box->origin.x -= steps; + break; + + case RIGHT: + box->origin.x += steps; break; } } diff --git a/ansilib/src/graphics/shapes.c b/ansilib/src/graphics/shapes.c index 12fae5f..3eef5df 100644 --- a/ansilib/src/graphics/shapes.c +++ b/ansilib/src/graphics/shapes.c @@ -3,14 +3,14 @@ #include -void draw_rectangle(uint32_t width, uint32_t height) +void draw_rectangle(uint32_t width, uint32_t height, char ch) { /* Top edge */ for (uint32_t i = 0; i < width; i++) { /* TODO: This character should be an argument such as * `border_char`. Also, let's not forget about * `border_width` */ - printmsg("#"); + printmsg("%c", ch); } for (uint32_t i = 0; i < height - 2; i++) { @@ -24,11 +24,11 @@ void draw_rectangle(uint32_t width, uint32_t height) nudge_cursor(DOWN, 1); /* Left edges */ - printmsg("#"); + printmsg("%c", ch); /* Right edges */ nudge_cursor(RIGHT, width - 2); - printmsg("#"); + printmsg("%c", ch); } /* Get to the next line */ @@ -37,6 +37,6 @@ void draw_rectangle(uint32_t width, uint32_t height) /* Bottom edge */ for (uint32_t i = 0; i < width; i++) { - printmsg("#"); + printmsg("%c", ch); } } diff --git a/src/main.c b/src/main.c index 5ba1e4c..56f60a0 100644 --- a/src/main.c +++ b/src/main.c @@ -83,21 +83,31 @@ int main(void) move_cursor(orig_cursor_pos); while ( 1 ) { - /* Read a byte from the standard input */ while ( read(STDIN_FILENO, &c, 1) < 1 ); - if ( iscntrl(c) ) { - /* Control characters are non-printable, - * hence don't even try to print them */ - printf("%d\r\n", c); - - } else { - /* print character ascii values and - * show the characters as well */ - printf("%d ('%c')\r\n", c, c); - + if ( !iscntrl(c) ) { if ( c == 'q' ) break; + else if ( c == 'h' || c == 'j' || c == 'k' || c == 'l' ) { + orig_cursor_pos = get_cursor_pos(); + erase(&rects[0]); + switch ( c ) { + case 'h': + nudge(&rects[0], LEFT, 1); + break; + case 'j': + nudge(&rects[0], DOWN, 1); + break; + case 'k': + nudge(&rects[0], UP, 1); + break; + case 'l': + nudge(&rects[0], RIGHT, 1); + break; + } + draw(&rects[0]); + move_cursor(orig_cursor_pos); + } } } -- 2.45.2