From 98280238383dc390207827d09dc92e0459229134 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juan=20Manuel=20Tom=C3=A1s?= Date: Sun, 24 May 2020 05:35:00 -0300 Subject: Rewrite in c++ --- test/page.c | 48 ------------------------------------- test/page.cpp | 48 +++++++++++++++++++++++++++++++++++++ test/point.c | 76 ---------------------------------------------------------- test/point.cpp | 76 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 124 insertions(+), 124 deletions(-) delete mode 100644 test/page.c create mode 100644 test/page.cpp delete mode 100644 test/point.c create mode 100644 test/point.cpp (limited to 'test') diff --git a/test/page.c b/test/page.c deleted file mode 100644 index f558fc6..0000000 --- a/test/page.c +++ /dev/null @@ -1,48 +0,0 @@ -#include - -#define PAGE_SIZE 32 -#include "../page.c" - -int main() { - int exit = 0; - struct page *page = new_page(); - - initscr(); - cbreak(); - noecho(); - nonl(); - intrflush(stdscr, FALSE); - keypad(stdscr, TRUE); - - while (!exit) { - clear(); - - for (int i = 0; i < page->gap_start; i++) { - addch(page->elements[i]); - } - for (int i = page->gap_start; i < page->gap_end; i++) { - addch('.'); - } - for (int i = page->gap_end; i < PAGE_SIZE; i++) { - addch(page->elements[i]); - } - - int input = getch(); - switch (input) { - case KEY_LEFT: - move_gap_backward(page); - break; - case KEY_RIGHT: - move_gap_forward(page); - break; - case KEY_BACKSPACE: - delete_at_gap(page); - break; - default: - insert_at_gap(page, input); - } - } - - endwin(); - return 0; -} diff --git a/test/page.cpp b/test/page.cpp new file mode 100644 index 0000000..d0a4d54 --- /dev/null +++ b/test/page.cpp @@ -0,0 +1,48 @@ +#include + +#define PAGE_SIZE 32 +#include "../page.cpp" + +int main() { + int exit = 0; + Page page = Page(); + + initscr(); + cbreak(); + noecho(); + nonl(); + intrflush(stdscr, FALSE); + keypad(stdscr, TRUE); + + while (!exit) { + clear(); + + for (int i = 0; i < page.gap_start; i++) { + addch(page.elements[i]); + } + for (int i = page.gap_start; i < page.gap_end; i++) { + addch('.'); + } + for (int i = page.gap_end; i < PAGE_SIZE; i++) { + addch(page.elements[i]); + } + + int input = getch(); + switch (input) { + case KEY_LEFT: + page--; + break; + case KEY_RIGHT: + page++; + break; + case KEY_BACKSPACE: + page.pop(); + break; + default: + page.push(input); + } + } + + endwin(); + return 0; +} diff --git a/test/point.c b/test/point.c deleted file mode 100644 index 0902f47..0000000 --- a/test/point.c +++ /dev/null @@ -1,76 +0,0 @@ -#include - -#define PAGE_SIZE 16 -#include "../page.c" -#include "../point.c" - -int main() { - int exit = 0; - struct page *page = new_page(); - struct point point = {page, 0}; - - initscr(); - start_color(); - cbreak(); - noecho(); - nonl(); - intrflush(stdscr, FALSE); - keypad(stdscr, TRUE); - -#define black 20 -#define red 21 -#define green 22 - init_color(black, 0, 0, 0); - init_color(red, 1000, 0, 0); - init_color(green, 0, 1000, 0); - init_pair(1, red, black); - init_pair(2, green, black); - - while (!exit) { - clear(); - - struct page *iter = page; - while (iter) { - if (iter == point.page) { - attron(COLOR_PAIR(1)); - } else { - attron(COLOR_PAIR(2)); - } - for (int i = 0; i < iter->gap_start; i++) { - addch(iter->elements[i]); - } - for (int i = iter->gap_start; i < iter->gap_end; i++) { - addch('_'); - } - for (int i = iter->gap_end; i < PAGE_SIZE; i++) { - addch(iter->elements[i]); - } - iter = iter->next; - attroff(COLOR_PAIR(1)); - attroff(COLOR_PAIR(2)); - } - - int input = getch(); - switch (input) { - case '': - exit = 1; - break; - case KEY_LEFT: - move_point_backward(&point); - align_gap(&point); - break; - case KEY_RIGHT: - move_point_forward(&point); - align_gap(&point); - break; - case KEY_BACKSPACE: - delete_at_point(&point); - break; - default: - insert_at_point(&point, input); - } - } - - endwin(); - return 0; -} diff --git a/test/point.cpp b/test/point.cpp new file mode 100644 index 0000000..edf4492 --- /dev/null +++ b/test/point.cpp @@ -0,0 +1,76 @@ +#include + +#define PAGE_SIZE 16 +#include "../page.cpp" +#include "../point.cpp" + +int main() { + int exit = 0; + Page *page = new Page(); + Point point = Point(page); + + initscr(); + start_color(); + cbreak(); + noecho(); + nonl(); + intrflush(stdscr, FALSE); + keypad(stdscr, TRUE); + +#define black 20 +#define red 21 +#define green 22 + init_color(black, 0, 0, 0); + init_color(red, 1000, 0, 0); + init_color(green, 0, 1000, 0); + init_pair(1, red, black); + init_pair(2, green, black); + + while (!exit) { + clear(); + + Page *iter = page; + while (iter) { + if (iter == point.page && iter->gap_end == point.index_to_offset()) { + attron(COLOR_PAIR(1)); + } else { + attron(COLOR_PAIR(2)); + } + for (int i = 0; i < iter->gap_start; i++) { + addch(iter->elements[i]); + } + for (int i = iter->gap_start; i < iter->gap_end; i++) { + addch('_'); + } + for (int i = iter->gap_end; i < PAGE_SIZE; i++) { + addch(iter->elements[i]); + } + iter = iter->next; + attroff(COLOR_PAIR(1)); + attroff(COLOR_PAIR(2)); + } + + int input = getch(); + switch (input) { + case '': + exit = 1; + break; + case KEY_LEFT: + point--; + point.align_gap(); + break; + case KEY_RIGHT: + point++; + point.align_gap(); + break; + case KEY_BACKSPACE: + point.pop(); + break; + default: + point.push(input); + } + } + + endwin(); + return 0; +} -- cgit v1.2.3