alsa-visual/spark_ncurses.c
2024-03-30 17:11:03 +03:00

108 lines
1.6 KiB
C

#include <fcntl.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <signal.h>
#include <ncurses.h>
#include <sys/ioctl.h>
#include <ctype.h>
#include <math.h>
#define NARGS 1024
#define ARG_SIZE 50
int args;
int values[NARGS + 1];
struct winsize ws;
int add_value(const char *str) {
if (args >= sizeof(values) / sizeof(int) || args > ws.ws_col)
return 1;
values[args] = atoi(str);
args++;
return 0;
}
void stdin_read(void) {
char arg[ARG_SIZE + 1];
char *p = arg;
while (1) {
int c = getchar();
if (c == EOF)
return;
else if (c == ' ' && p != arg) {
*p = '\0';
p = arg;
if (add_value(arg))
break;
}
else {
*p = c;
if (p + 1 == arg + sizeof(arg))
break;
p++;
}
}
}
void sig_handler(int sig) {
(void)sig;
endwin();
exit(0);
}
int main(void) {
signal(SIGINT, sig_handler);
signal(SIGTERM, sig_handler);
if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &ws) < 0)
return 1;
initscr();
noecho();
curs_set(0);
timeout(0);
int min = 0;
int max = 0;
int middle = ws.ws_row / 2;
while (1) {
args = 0;
stdin_read();
clear();
for (int i = 0; i < args; i++) {
if (values[i] < min)
min = values[i];
else if (values[i] > max)
max = values[i];
int val = (values[i] - min) * 10 / ((max - min) + 1);
for (int j = 0; j < val; j++) {
mvprintw(middle - 2 - j, i, "#");
mvprintw(middle + 2 + j, i, "#");
}
}
/* Vol */
int val = (values[args - 1] - min) * ws.ws_col / ((max - min) + 1);
for (int j = 0; j < val; j++) {
mvprintw(middle - 1, ws.ws_col - j, "<=");
mvprintw(middle + 1, ws.ws_col - j, "<=");
}
refresh();
}
}