diff options
author | Juan Manuel Tomás <jtomas1815@gmail.com> | 2020-07-27 22:46:56 -0300 |
---|---|---|
committer | Juan Manuel Tomás <jtomas1815@gmail.com> | 2020-07-27 22:46:56 -0300 |
commit | 83ec165fddc5f9ee99a8e2aa75d188c45253a0fa (patch) | |
tree | c22bbfb091c2461eca1aca0756cad2fc175643ad /src/client | |
parent | dacf6e0625d10ff47d2aad0ca8d705f90f030119 (diff) | |
download | jet-83ec165fddc5f9ee99a8e2aa75d188c45253a0fa.tar.gz jet-83ec165fddc5f9ee99a8e2aa75d188c45253a0fa.zip |
Remove interface class
It felt like a forced abstraction.
Diffstat (limited to 'src/client')
-rw-r--r-- | src/client/cursor.cpp | 4 | ||||
-rw-r--r-- | src/client/interface.cpp | 95 | ||||
-rw-r--r-- | src/client/jetc.cpp | 91 |
3 files changed, 89 insertions, 101 deletions
diff --git a/src/client/cursor.cpp b/src/client/cursor.cpp index 226b8a5..65e32bf 100644 --- a/src/client/cursor.cpp +++ b/src/client/cursor.cpp @@ -1,10 +1,8 @@ -class Cursor { +struct Cursor { int x; int y; Window &w; - public: - Cursor(Window &w) : x(0), y(0), w(w) {} char element() { diff --git a/src/client/interface.cpp b/src/client/interface.cpp deleted file mode 100644 index 0da5653..0000000 --- a/src/client/interface.cpp +++ /dev/null @@ -1,95 +0,0 @@ -#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 8a649b0..cdb570e 100644 --- a/src/client/jetc.cpp +++ b/src/client/jetc.cpp @@ -5,10 +5,95 @@ #include <common/socket.cpp> #include <client/window.cpp> #include <client/cursor.cpp> -#include <client/interface.cpp> + +#define NORMAL_MODE 0 +#define INSERT_MODE 1 + +Socket io; +Window window; +Cursor cursor(window); + +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(); +} int main(int argc, char *argv[]) { - Interface interface; - interface.run(); + io.connect(); + + 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); + } + } + } + return 0; } |