From 2260209d3b09f728f0cbb891f5498a9f0f8e38eb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juan=20Manuel=20Tom=C3=A1s?= Date: Tue, 7 Jul 2020 17:43:50 -0300 Subject: Implement tui for client --- Makefile | 7 ++++ client.cpp | 8 ++++ jet.cpp | 8 +++- jet2 | Bin 0 -> 23768 bytes jet2.cpp | 139 ++++++++++++++++++++++--------------------------------------- point.cpp | 18 -------- 6 files changed, 72 insertions(+), 108 deletions(-) create mode 100755 jet2 diff --git a/Makefile b/Makefile index 45a82a3..179f7b1 100644 --- a/Makefile +++ b/Makefile @@ -1,4 +1,11 @@ FLAGS=-ggdb -O0 -lcurses $(shell ncursesw6-config --cflags --libs) +_PHONY:all + +all:jet jet2 + jet: Makefile *.cpp g++ $(FLAGS) jet.cpp -o jet + +jet2: Makefile *.cpp + g++ $(FLAGS) jet2.cpp -o jet2 diff --git a/client.cpp b/client.cpp index f99dc5d..85289b6 100644 --- a/client.cpp +++ b/client.cpp @@ -76,4 +76,12 @@ struct Client { target++; } } + + void push() { + cursor.push(args.pop()); + } + + void pop() { + cursor.pop(); + } }; diff --git a/jet.cpp b/jet.cpp index 50572c2..c4e997c 100644 --- a/jet.cpp +++ b/jet.cpp @@ -35,6 +35,10 @@ void parse_command(char *command, Client *client) { client->show(); } else if (strcmp(token, "move") == 0) { client->move(); + } else if (strcmp(token, "push") == 0) { + client->push(); + } else if (strcmp(token, "pop") == 0) { + client->pop(); } else { client->args.push(atoi(token)); } @@ -47,13 +51,15 @@ int main() { scratch.read("LICENSE"); int listener = create_listener(); + int epollfd = epoll_create1(0); - epoll_event events[MAX_EVENTS]; + epoll_event ev; ev.events = EPOLLIN; ev.data.fd = listener; epoll_ctl(epollfd, EPOLL_CTL_ADD, listener, &ev); + epoll_event events[MAX_EVENTS]; Client *clients[1024] = {}; for (;;) { diff --git a/jet2 b/jet2 new file mode 100755 index 0000000..aeb3f78 Binary files /dev/null and b/jet2 differ diff --git a/jet2.cpp b/jet2.cpp index c00042a..791bd14 100644 --- a/jet2.cpp +++ b/jet2.cpp @@ -1,14 +1,17 @@ -#include #include -#include -#include +#include +#include #include -#include "page.cpp" -#include "point.cpp" -#include "buffer.cpp" + +#include +#include +#include +#include +#include #define NORMAL_MODE 0 #define INSERT_MODE 1 +#define PORT 6969 #define pos(x, y) (x) + (y) * window_width int main(int argc, char *argv[]) { @@ -18,110 +21,68 @@ int main(int argc, char *argv[]) { intrflush(stdscr, FALSE); keypad(stdscr, TRUE); - if (argc > 1) { - buffer.read(argv[1]); - } - int window_height, window_width; getmaxyx(stdscr, window_height, window_width); - int *view = new int[window_width * window_height]; + char *view = new char[window_width * window_height]; for (int i = 0; i < window_width * window_height; view[i++] = 0); + + int s = socket(AF_INET, SOCK_STREAM, 0); + sockaddr_in addr = { AF_INET, htons(PORT), htonl(INADDR_LOOPBACK)}; + connect(s, (sockaddr *) &addr, sizeof(sockaddr_in)); + int mode = NORMAL_MODE; int quit = 0; while (!quit) { clear(); - Point window_end(window_start); - int x = -1, y = -1; - for (int i = 0; i < window_height; i++) { - for (int j = 0; j < window_width; j++) { - if (window_end == cursor) { - x = j; - y = i; - } - view[pos(j, i)] = window_end.element(); - if (window_end.element() == '\n') { - for (int k = j + 1; k < window_width; k++) { - view[pos(k, i)] = 0; - } - j = window_width; - } else if (window_end.element() == '\t') { - for (int k = j + 1; k < j + 8; k++) { - view[pos(k, i)] = 0; - } - j = j + 7; - } - window_end++; - } - } + char msg[32] = {}; + int len = sprintf(msg, " %d %d show ", window_width, window_height); + write(s, msg, len); + read(s, view, window_width * window_height); for (int i = 0; i < window_width * window_height; i++) { - printw("%lc", view[i]); - } - if (x != -1 && y != -1) { - move(y, x); + if (view[i]) { + printw("%c", view[i]); + } } + memset(msg, 0, 32); int input = getch(); - if (byte_type(input) == 1) { - if (mode == NORMAL_MODE) { - switch (input) { - case '': - quit = 1; - break; - case 'i': - mode = INSERT_MODE; - break; - case 'j': - for (int i = pos(x, y); i < pos(x + 1, y + 1); i++) { - if (view[i]) cursor++; - } - cursor--; - if (y + 1 >= window_height) { - window_start.seek('\n', window_width - 1); - window_start++; - } - break; - case 'k': - for (int i = pos(x, y); i > pos(x, y - 1); i--) { - if (view[i]) cursor--; - } - if (y - 1 <= 0) { - window_start--; - window_start.rseek('\n', window_width - 1); - } - break; - case 'h': - cursor--; - break; - case 'l': - cursor++; - break; - } - } else { - switch (input) { - case '': - mode = NORMAL_MODE; - break; - case KEY_BACKSPACE: - cursor.pop(); - break; - default: - cursor.push(input); - } + if (mode == NORMAL_MODE) { + switch (input) { + case '': + quit = 1; + break; + case 'i': + mode = INSERT_MODE; + break; + case 'h': + len = sprintf(msg, " -1 move "); + write(s, msg, len); + break; + case 'l': + len = sprintf(msg, " 1 move "); + write(s, msg, len); + break; } } else { - cursor.push(input); - for (int i = 0; i < byte_type(input) - 1; i++) { - cursor.push(getch()); + switch (input) { + case '': + mode = NORMAL_MODE; + break; + case KEY_BACKSPACE: + len = sprintf(msg, " pop "); + write(s, msg, len); + break; + default: + len = sprintf(msg, " %d push ", input); + write(s, msg, len); } } } - buffer.write(buffer.name); - endwin(); return 0; } diff --git a/point.cpp b/point.cpp index b0be8e9..0e80e7f 100644 --- a/point.cpp +++ b/point.cpp @@ -93,24 +93,6 @@ struct Point { return next_byte(); } - uint64_t seek(uint8_t c, uint64_t limit) { - uint64_t travel_distance = 0; - while (!at_end() && next_byte() != c && travel_distance < limit) { - (*this)++; - travel_distance++; - } - return travel_distance; - } - - uint64_t rseek(uint8_t c, uint64_t limit) { - uint64_t travel_distance = 0; - while (!at_start() && prev_byte() != c && travel_distance < limit) { - (*this)--; - travel_distance++; - } - return travel_distance; - } - void align_gap() { while (page->gap_end < index_to_offset()) { page->move_gap_forward(); -- cgit v1.2.3