first commit
This commit is contained in:
114
src/log.c
Normal file
114
src/log.c
Normal file
@ -0,0 +1,114 @@
|
||||
#include "log.h"
|
||||
#include "env.h"
|
||||
#include <stdarg.h>
|
||||
#include <stdbool.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <sys/ioctl.h>
|
||||
#include <unistd.h>
|
||||
|
||||
int lastprog = -1;
|
||||
struct winsize barwin = {0};
|
||||
|
||||
void bar_init() {
|
||||
lastprog = -1;
|
||||
ioctl(STDOUT_FILENO, TIOCGWINSZ, &barwin);
|
||||
printf("\e[?25l");
|
||||
bar(0, 1);
|
||||
}
|
||||
|
||||
void bar_free() {
|
||||
int size = barwin.ws_col;
|
||||
|
||||
printf("\r");
|
||||
for (int i = 0; i < size; i++)
|
||||
printf(" ");
|
||||
printf("\r");
|
||||
|
||||
printf("\e[?25h");
|
||||
}
|
||||
|
||||
bool bar(float cur, float max) {
|
||||
if (cur <= 0 || max <= 0 || cur > max)
|
||||
return false;
|
||||
|
||||
float per = 100 * (cur / max);
|
||||
if (per > 100) {
|
||||
return false;
|
||||
}
|
||||
|
||||
char perc[20];
|
||||
sprintf(perc, " %%%.f", per);
|
||||
|
||||
int size = barwin.ws_col - (3 + strlen(perc));
|
||||
if (size <= 0) {
|
||||
return false;
|
||||
}
|
||||
|
||||
int prog = size * (cur / max);
|
||||
if (prog == lastprog || prog > size)
|
||||
return true;
|
||||
lastprog = prog;
|
||||
|
||||
printf("\r" COLOR_BOLD "[");
|
||||
for (int i = 0; i < prog; i++) {
|
||||
printf("#");
|
||||
}
|
||||
printf(COLOR_BLUE "#" COLOR_RESET COLOR_BOLD);
|
||||
|
||||
for (int i = prog; i < size; i++) {
|
||||
printf(" ");
|
||||
}
|
||||
printf("]" COLOR_RESET);
|
||||
|
||||
printf(COLOR_BOLD COLOR_BLUE "%s" COLOR_RESET "\r", perc);
|
||||
return true;
|
||||
}
|
||||
|
||||
void info(const char *msg, ...) {
|
||||
va_list args;
|
||||
va_start(args, msg);
|
||||
|
||||
printf(COLOR_BOLD COLOR_BLUE ">>> " COLOR_RESET COLOR_BOLD);
|
||||
vprintf(msg, args);
|
||||
printf(COLOR_RESET "\n");
|
||||
|
||||
va_end(args);
|
||||
}
|
||||
|
||||
void error(const char *msg, ...) {
|
||||
va_list args;
|
||||
va_start(args, msg);
|
||||
|
||||
printf(COLOR_BOLD COLOR_RED ">>> " COLOR_RESET COLOR_BOLD);
|
||||
vprintf(msg, args);
|
||||
printf(COLOR_RESET "\n");
|
||||
|
||||
va_end(args);
|
||||
}
|
||||
|
||||
void success(const char *msg, ...) {
|
||||
va_list args;
|
||||
va_start(args, msg);
|
||||
|
||||
printf(COLOR_BOLD COLOR_GREEN ">>> " COLOR_RESET COLOR_BOLD);
|
||||
vprintf(msg, args);
|
||||
printf(COLOR_RESET "\n");
|
||||
|
||||
va_end(args);
|
||||
}
|
||||
|
||||
void debug(const char *msg, ...) {
|
||||
if (!env_mc_debug())
|
||||
return;
|
||||
|
||||
va_list args;
|
||||
va_start(args, msg);
|
||||
|
||||
printf(COLOR_BOLD COLOR_MAGENTA "DEBUG: " COLOR_RESET COLOR_BOLD);
|
||||
vprintf(msg, args);
|
||||
printf(COLOR_RESET "\n");
|
||||
|
||||
va_end(args);
|
||||
}
|
Reference in New Issue
Block a user