diff --git a/BUILD b/BUILD index 8776965..556bbcf 100644 --- a/BUILD +++ b/BUILD @@ -1,7 +1,7 @@ cc_binary( name = "ncac", srcs = [ - "ncac.c", + "ncac.cc", "ncac.h", ], deps = [ diff --git a/ncac.c b/ncac.cc similarity index 63% rename from ncac.c rename to ncac.cc index f26edeb..c7dd8ee 100644 --- a/ncac.c +++ b/ncac.cc @@ -8,47 +8,45 @@ int main(int argc, char **argv) { - (void)argc; - (void)argv; setup(); - char input; - int curs_x = 0; int curs_y = 0; + char input; + while(1) { switch (input = getch()) { case 'a': - draw_text(&curs_x, &curs_y, "Hello, world!"); + ui::draw_text("Hello, world!", &curs_x, &curs_y); + break; + case 'b': + ui::draw_text("Goodbye, world!", &curs_x, &curs_y); break; default: - finish(0); + finish(SIGTERM); break; } } return 0; } -void finish(int sig) { - (void)sig; +static void finish(int sig) { endwin(); exit(0); } -void setup() { +static void setup() { // initialize ncurses initscr(); - cbreak(); - - // don't echo input noecho(); - nonl(); // install handlers signal(SIGINT, &finish); + signal(SIGKILL, &finish); + signal(SIGTERM, &finish); } diff --git a/ncac.h b/ncac.h index ca405d5..433e822 100644 --- a/ncac.h +++ b/ncac.h @@ -4,12 +4,10 @@ /** * Cleans up before exit. Installed on SIGINT */ -void finish(); +static void finish(int sig); /** * Sets up ncurses and internal data structures */ -void setup(); - - +static void setup(); #endif // _NCAC_H_ diff --git a/ui/BUILD b/ui/BUILD index 022bfa4..4986231 100644 --- a/ui/BUILD +++ b/ui/BUILD @@ -1,6 +1,6 @@ cc_library( name="ui", - srcs = ["base.c"], + srcs = ["base.cc"], hdrs = ["base.h"], deps = ["@system_include//:curses"], visibility = ["//visibility:public"], diff --git a/ui/base.c b/ui/base.c deleted file mode 100644 index cb343fa..0000000 --- a/ui/base.c +++ /dev/null @@ -1,17 +0,0 @@ -#include "base.h" - -#include -#include - -void draw_text(int *x, int *y, char *text) { - size_t len = strlen(text); - if (!len) return; - - mvaddch(*x, *y, text[0]); - - for (size_t i=1; i < strlen(text); i++) { - addch(text[i]); - } - - getyx(stdscr, *y, *x); -} diff --git a/ui/base.cc b/ui/base.cc new file mode 100644 index 0000000..ecbb95d --- /dev/null +++ b/ui/base.cc @@ -0,0 +1,23 @@ +#include "base.h" + +#include +#include + +#include + +namespace ui { + void draw_text(const std::string text, int *x, int *y) { + if (!text.length()) return; + + auto iter = text.begin(); + + mvaddch(*y, *x, *iter); + ++iter; + + for (; iter != text.end(); ++iter) { + addch(*iter); + } + + getyx(stdscr, *y, *x); + } +} diff --git a/ui/base.h b/ui/base.h index b941981..ea7f085 100644 --- a/ui/base.h +++ b/ui/base.h @@ -1,10 +1,14 @@ #ifndef UI_BASE_H_ #define UI_BASE_H_ -/** - * Prints the string text starting at position *x, *y. x and y are updated to - * the position after the last character - */ -void draw_text(int *x, int *y, char *text); +#include +namespace ui { + /** + * Prints the string text starting at position *x, *y. x and y are updated to + * the position after the last character + */ + void draw_text(const std::string text, int *x, int *y); + +} // namespace ui #endif // UI_BASE_H_