From dacf6e0625d10ff47d2aad0ca8d705f90f030119 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juan=20Manuel=20Tom=C3=A1s?= Date: Wed, 22 Jul 2020 22:52:51 -0300 Subject: Update architecture --- src/client/cursor.cpp | 46 +++++++++++++++++++++++ src/client/interface.cpp | 95 ++++++++++++++++++++++++++++++++++++++++++++++++ src/client/jetc.cpp | 45 ++--------------------- src/client/window.cpp | 74 +++++++++---------------------------- 4 files changed, 162 insertions(+), 98 deletions(-) create mode 100644 src/client/cursor.cpp create mode 100644 src/client/interface.cpp (limited to 'src/client') diff --git a/src/client/cursor.cpp b/src/client/cursor.cpp new file mode 100644 index 0000000..226b8a5 --- /dev/null +++ b/src/client/cursor.cpp @@ -0,0 +1,46 @@ +class Cursor { + int x; + int y; + Window &w; + + public: + + Cursor(Window &w) : x(0), y(0), w(w) {} + + char element() { + return w.view[x + y * w.width]; + } + + void move_left() { + if (x == 0) { + if (y != 0) { + y--; + x = w.line_ends[y]; + } + } else { + x--; + } + } + + void move_right() { + if (element()) { + if (element() == '\n') { + x = 0; + y++; + } else { + x++; + } + } + } + + void move_up() { + } + + void move_down() { + } + + void update() { + w.set_cursor(x, y); + } +}; + diff --git a/src/client/interface.cpp b/src/client/interface.cpp new file mode 100644 index 0000000..0da5653 --- /dev/null +++ b/src/client/interface.cpp @@ -0,0 +1,95 @@ +#define NORMAL_MODE 0 +#define INSERT_MODE 1 + +class Interface { + Socket io; + Window window; + Cursor cursor; + + public: + + Interface() : cursor(window) { + io.connect(); + } + + void run() { + int mode = NORMAL_MODE; + int quit = 0; + while (!quit) { + update(); + + int input = window.get_input(); + if (mode == NORMAL_MODE) { + switch (input) { + case '': + quit = 1; + break; + case 'i': + mode = INSERT_MODE; + break; + case 'h': + move_left(); + break; + case 'l': + move_right(); + break; + } + } else { + switch (input) { + case '': + mode = NORMAL_MODE; + break; + case KEY_BACKSPACE: + delete_element(); + break; + default: + insert_element(input); + } + } + } + } + + void update() { + int8_t msg[5]; + msg[0] = OP_SHOW; + encode2(window.width, msg, 1); + encode2(window.height, msg, 3); + io.send(msg, 5); + io.recv(window.view, window.width * window.height); + io.recv(window.line_ends, window.height); + + window.update(); + cursor.update(); + } + + void move_left() { + int8_t mov[2]; + mov[0] = OP_MOVE1; + mov[1] = -1; + io.send(mov, 2); + cursor.move_left(); + } + + void move_right() { + int8_t mov[2]; + mov[0] = OP_MOVE1; + mov[1] = 1; + io.send(mov, 2); + cursor.move_right(); + } + + void delete_element() { + int8_t del; + del = OP_DELETE; + io.send(&del, 1); + cursor.move_left(); + } + + void insert_element(int input) { + int8_t ins[2]; + ins[0] = OP_INSERT; + ins[1] = input; + io.send(ins, 2); + cursor.move_right(); + } +}; diff --git a/src/client/jetc.cpp b/src/client/jetc.cpp index 2dd6074..8a649b0 100644 --- a/src/client/jetc.cpp +++ b/src/client/jetc.cpp @@ -4,48 +4,11 @@ #include #include #include - -#define NORMAL_MODE 0 -#define INSERT_MODE 1 +#include +#include int main(int argc, char *argv[]) { - Window window; - - int mode = NORMAL_MODE; - - int quit = 0; - while (!quit) { - window.redraw(); - - int input = getch(); - if (mode == NORMAL_MODE) { - switch (input) { - case '': - quit = 1; - break; - case 'i': - mode = INSERT_MODE; - break; - case 'h': - window.move_left(); - break; - case 'l': - window.move_right(); - break; - } - } else { - switch (input) { - case '': - mode = NORMAL_MODE; - break; - case KEY_BACKSPACE: - window.delete_element(); - break; - default: - window.insert_element(input); - } - } - } - + Interface interface; + interface.run(); return 0; } diff --git a/src/client/window.cpp b/src/client/window.cpp index 9e389fe..14128bf 100644 --- a/src/client/window.cpp +++ b/src/client/window.cpp @@ -1,16 +1,12 @@ #include -class Window { - Socket io; - char *view; +struct Window { int width; int height; - int cursor_x; - int cursor_y; - - public: + char *view; + int *line_ends; - Window() : cursor_x(0), cursor_y(0) { + Window() { initscr(); cbreak(); noecho(); @@ -20,66 +16,30 @@ class Window { getmaxyx(stdscr, height, width); view = new char[width * height]; - for (int i = 0; i < width * height; i++) { - view[i] = 0; - } + for (int i = 0; i < width * height; view[i++] = 0); - io.connect(); + line_ends = new int[height]; + for (int i = 0; i < height; line_ends[i++] = 0); } ~Window() { + delete[] view; + delete[] line_ends; endwin(); } - void redraw() { - clear(); - - int8_t msg[5]; - msg[0] = OP_SHOW; - encode2(width, msg, 1); - encode2(height, msg, 3); - io.send(msg, 5); - io.recv(view, width * height); - for (int i = 0; i < width * height; i++) { - printw("%c", view[i]); - } - move(cursor_y, cursor_x); - } - - char pos(size_t x, size_t y) { - return view[x + y * width]; + int get_input() { + return getch(); } - void move_left() { - int8_t mov[2]; - if (cursor_x > 0) { - mov[0] = OP_MOVE1; - mov[1] = -1; - io.send(mov, 2); - cursor_x--; - } + void set_cursor(int x, int y) { + move(y, x); } - void move_right() { - int8_t mov[2]; - if (cursor_x < width - 1 && pos(cursor_x + 1, cursor_y) != 0) { - mov[0] = OP_MOVE1; - mov[1] = 1; - io.send(mov, 2); - cursor_x++; + void update() { + clear(); + for (int i = 0; i < width * height; i++) { + printw("%c", view[i]); } } - - void delete_element() { - int8_t del[1]; - del[0] = OP_DELETE; - io.send(del, 1); - } - - void insert_element(int input) { - int8_t ins[2]; - ins[0] = OP_INSERT; - ins[1] = input; - io.send(ins, 2); - } }; -- cgit v1.2.3