Загрузить файлы в «/»

This commit is contained in:
8nlight 2023-10-07 17:04:55 +03:00
parent ba47ab6656
commit 2d1ffc23d5

106
main.c
View File

@ -1,5 +1,7 @@
#include <ncurses.h> #include <ncurses.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <time.h> #include <time.h>
typedef struct { typedef struct {
@ -12,12 +14,12 @@ size_t cursor;
void die(char *msg) { void die(char *msg) {
endwin(); endwin();
puts(msg); printf("%s: %s\n", msg, strerror(errno));
exit(1); exit(1);
} }
void add_value(clock_t sec, clock_t msec) { void add_value(clock_t sec, clock_t msec) {
timer_size += 1; timer_size++;
timer = realloc(timer, (1 + timer_size) * sizeof(TIME)); timer = realloc(timer, (1 + timer_size) * sizeof(TIME));
if (timer == NULL) if (timer == NULL)
@ -27,63 +29,24 @@ void add_value(clock_t sec, clock_t msec) {
timer[timer_size].msec = msec; timer[timer_size].msec = msec;
} }
int main(void) { void keyboard(clock_t sec, clock_t mcsec) {
int key = getch();
if (key < 0)
return;
/* Init array */
timer = malloc(sizeof(TIME));
if (timer == NULL)
die("malloc returned NULL");
initscr();
noecho();
timeout(0);
curs_set(0);
while (1) {
/* Microsecond */
clock_t mcsec = clock();
if (mcsec == -1)
die("Yes");
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);
}
}
refresh();
/* Functions */
switch (getch()) {
case ' ':
clear(); clear();
switch (key) {
case ' ':
add_value(sec, mcsec / 10000); add_value(sec, mcsec / 10000);
cursor = timer_size; cursor = timer_size;
break; break;
case 'w': case 'w':
clear();
if (cursor + 1 <= timer_size) if (cursor + 1 <= timer_size)
cursor++; cursor++;
break; break;
case 's': case 's':
clear();
if (cursor - 1 > 0) if (cursor - 1 > 0)
cursor--; cursor--;
break; break;
@ -96,10 +59,57 @@ int main(void) {
break; break;
case 'q': case 'q':
die("Succesfull exit"); endwin();
exit(0);
default: default:
break; 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 */
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, "%ld.%ld", sec, msec);
print_timer();
keyboard(sec, mcsec);
refresh();
} }
} }