diff --git a/main.c b/main.c index e8e5410..1fe460d 100644 --- a/main.c +++ b/main.c @@ -1,5 +1,7 @@ #include #include +#include +#include #include typedef struct { @@ -12,12 +14,12 @@ size_t cursor; void die(char *msg) { endwin(); - puts(msg); + printf("%s: %s\n", msg, strerror(errno)); exit(1); } void add_value(clock_t sec, clock_t msec) { - timer_size += 1; + timer_size++; timer = realloc(timer, (1 + timer_size) * sizeof(TIME)); if (timer == NULL) @@ -27,6 +29,59 @@ void add_value(clock_t sec, clock_t msec) { timer[timer_size].msec = msec; } +void keyboard(clock_t sec, clock_t mcsec) { + int key = getch(); + if (key < 0) + return; + + clear(); + switch (key) { + case ' ': + add_value(sec, mcsec / 10000); + cursor = timer_size; + break; + + case 'w': + if (cursor + 1 <= timer_size) + cursor++; + break; + + case 's': + if (cursor - 1 > 0) + cursor--; + break; + + case 'p': + timeout(-1); + getch(); + timeout(0); + clear(); + break; + + case 'q': + endwin(); + exit(0); + + default: + break; + } +} + +void print_timer(void) { + if (timer_size > 0) { + /* Start y */ + unsigned int y = 3; + mvprintw(y, 0, ">"); + + for (size_t i = cursor; i < cursor + 10; i++) { + if (i > timer_size) + break; + + mvprintw(y++, 2, "(%ld) %ld.%ld", i, timer[i].sec, timer[i].msec); + } + } +} + int main(void) { /* Init array */ @@ -34,6 +89,7 @@ int main(void) { if (timer == NULL) die("malloc returned NULL"); + /* Init terminal */ initscr(); noecho(); timeout(0); @@ -44,62 +100,16 @@ int main(void) { /* Microsecond */ clock_t mcsec = clock(); if (mcsec == -1) - die("Yes"); + die(""); clock_t sec = mcsec / 1000000; clock_t msec = (mcsec / 100000) % 10; mvprintw(1, 2, "%ld.%ld", sec, msec); - /* Print timer array */ - if (timer_size > 0) { - - /* Start y */ - unsigned int j = 3; - mvprintw(j, 0, ">"); - - for (size_t i = cursor; i < cursor + 10; i++) { - if (i > timer_size) - break; - - mvprintw(j++, 2, "(%ld) %ld.%ld", i, timer[i].sec, timer[i].msec); - } - } + print_timer(); + keyboard(sec, mcsec); refresh(); - - /* Functions */ - switch (getch()) { - case ' ': - clear(); - add_value(sec, mcsec / 10000); - cursor = timer_size; - break; - - case 'w': - clear(); - if (cursor + 1 <= timer_size) - cursor++; - break; - - case 's': - clear(); - if (cursor - 1 > 0) - cursor--; - break; - - case 'p': - timeout(-1); - getch(); - timeout(0); - clear(); - break; - - case 'q': - die("Succesfull exit"); - - default: - break; - } } }