2024-05-01 18:02:15 +00:00
|
|
|
#include "log.h"
|
|
|
|
#include "env.h"
|
2024-05-01 20:07:02 +00:00
|
|
|
#include "intl.h"
|
|
|
|
#include "util.h"
|
|
|
|
|
2024-05-01 18:02:15 +00:00
|
|
|
#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);
|
|
|
|
}
|
|
|
|
|
2024-05-01 20:07:02 +00:00
|
|
|
void details(const char *msg, ...) {
|
|
|
|
va_list args;
|
|
|
|
va_start(args, msg);
|
|
|
|
|
2024-05-01 21:50:53 +00:00
|
|
|
printf(" " COLOR_RESET);
|
2024-05-01 20:07:02 +00:00
|
|
|
vprintf(msg, args);
|
|
|
|
printf(COLOR_RESET "\n");
|
|
|
|
|
|
|
|
va_end(args);
|
|
|
|
}
|
|
|
|
|
2024-05-01 18:02:15 +00:00
|
|
|
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);
|
|
|
|
}
|
2024-05-01 20:07:02 +00:00
|
|
|
|
|
|
|
void input(const char *msg, ...) {
|
|
|
|
va_list args;
|
|
|
|
va_start(args, msg);
|
|
|
|
|
|
|
|
printf(COLOR_BOLD COLOR_CYAN ">>> " COLOR_RESET COLOR_BOLD);
|
|
|
|
vprintf(msg, args);
|
|
|
|
printf(COLOR_RESET);
|
|
|
|
|
|
|
|
va_end(args);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool yesno(const char *msg){
|
2024-05-01 21:50:53 +00:00
|
|
|
if(env_mc_yes())
|
|
|
|
return true;
|
|
|
|
|
|
|
|
char *yes[] = {_("y"), _("Y")};
|
|
|
|
char *no[] = {_("n"), _("N")};
|
|
|
|
|
|
|
|
char question[strlen(msg)+12], c;
|
|
|
|
sprintf(question, _("%s [y/N] "), msg);
|
2024-05-01 20:07:02 +00:00
|
|
|
|
|
|
|
while(true){
|
|
|
|
input(question);
|
2024-05-01 21:50:53 +00:00
|
|
|
|
|
|
|
int c = getchar();
|
|
|
|
if(c == '\n')
|
2024-05-01 20:07:02 +00:00
|
|
|
return false;
|
2024-05-01 21:50:53 +00:00
|
|
|
getchar();
|
|
|
|
|
|
|
|
for(int i = 0; i < sizeof(yes)/sizeof(char*); i++){
|
|
|
|
if(yes[i][0] == c)
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
for(int i = 0; i < sizeof(no)/sizeof(char*); i++){
|
|
|
|
if(no[i][0] == c)
|
|
|
|
return false;
|
|
|
|
}
|
2024-05-01 20:07:02 +00:00
|
|
|
|
|
|
|
error(_("Please answer with y/n"));
|
|
|
|
}
|
|
|
|
}
|