diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/client/cursor.cpp | 46 | ||||
-rw-r--r-- | src/client/interface.cpp | 95 | ||||
-rw-r--r-- | src/client/jetc.cpp | 45 | ||||
-rw-r--r-- | src/client/window.cpp | 74 | ||||
-rw-r--r-- | src/server/client.cpp | 7 |
5 files changed, 169 insertions, 98 deletions
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 <common/ipc.cpp> #include <common/socket.cpp> #include <client/window.cpp> - -#define NORMAL_MODE 0 -#define INSERT_MODE 1 +#include <client/cursor.cpp> +#include <client/interface.cpp> 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 <cursesw.h> -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); - } }; diff --git a/src/server/client.cpp b/src/server/client.cpp index d862b13..75c1c38 100644 --- a/src/server/client.cpp +++ b/src/server/client.cpp @@ -57,12 +57,17 @@ struct Client { void show(size_t window_w, size_t window_h) { char *view = new char[window_w * window_h]; + int *line_ends = new int[window_h]; + for (int i = 0; i < window_h; i++) { + line_ends[i] = window_w; + } Point window_end(window_start); for (int i = 0; i < window_h; i++) { for (int j = 0; j < window_w; j++) { view[pos(j, i)] = window_end.element(); if (window_end.element() == '\n') { + line_ends[i] = j; for (int k = j + 1; k < window_w; k++) { view[pos(k, i)] = 0; } @@ -78,7 +83,9 @@ struct Client { } io.send(view, window_w * window_h); + io.send(line_ends, window_h); delete[] view; + delete[] line_ends; } void move(int64_t target) { |