134 lines
2.0 KiB
C
134 lines
2.0 KiB
C
#include <ncurses.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
#include <errno.h>
|
|
#include <time.h>
|
|
|
|
typedef struct {
|
|
clock_t sec;
|
|
clock_t msec;
|
|
} TIME;
|
|
TIME *timer;
|
|
size_t timer_size;
|
|
size_t cursor;
|
|
|
|
void die(char *msg) {
|
|
endwin();
|
|
printf("%s: %s\n", strerror(errno), msg);
|
|
exit(1);
|
|
}
|
|
|
|
void export(void) {
|
|
FILE *fp = fopen("export", "a");
|
|
if (fp == NULL)
|
|
die("");
|
|
|
|
for (size_t i = 0; i < timer_size; i++)
|
|
fprintf(fp, "%ld%ld,", timer[i].sec, timer[i].msec / 10);
|
|
|
|
fclose(fp);
|
|
}
|
|
|
|
void add_value(clock_t sec, clock_t msec) {
|
|
timer_size++;
|
|
|
|
timer = realloc(timer, (1 + timer_size) * sizeof(TIME));
|
|
if (timer == NULL)
|
|
die("realloc returned NULL");
|
|
|
|
timer[timer_size].sec = sec;
|
|
timer[timer_size].msec = msec;
|
|
}
|
|
|
|
void keyboard(clock_t sec, clock_t mcsec) {
|
|
int key = getch();
|
|
if (key < 0)
|
|
return;
|
|
|
|
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 'e':
|
|
endwin();
|
|
export();
|
|
exit(0);
|
|
|
|
case 'q':
|
|
endwin();
|
|
exit(0);
|
|
|
|
default:
|
|
break;
|
|
}
|
|
|
|
clear();
|
|
}
|
|
|
|
void print_timer(void) {
|
|
if (timer_size > 0) {
|
|
/* Start y */
|
|
unsigned int y = 4;
|
|
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 */
|
|
timer = malloc(sizeof(TIME));
|
|
if (timer == NULL)
|
|
die("malloc returned NULL");
|
|
|
|
/* Init terminal */
|
|
initscr();
|
|
noecho();
|
|
timeout(0);
|
|
curs_set(0);
|
|
|
|
while (1) {
|
|
|
|
/* Microsecond */
|
|
clock_t mcsec = clock();
|
|
if (mcsec == -1)
|
|
die("");
|
|
|
|
clock_t sec = mcsec / 1000000;
|
|
clock_t msec = (mcsec / 100000) % 10;
|
|
|
|
mvprintw(1, 2, "[q quit] [e export] [' ' add mark] [w/s control] [p pause]");
|
|
mvprintw(3, 2, "%ld.%ld", sec, msec);
|
|
|
|
print_timer();
|
|
keyboard(sec, mcsec);
|
|
|
|
refresh();
|
|
}
|
|
}
|