172 lines
3.1 KiB
C
172 lines
3.1 KiB
C
#include "log.h"
|
|
#include "env.h"
|
|
#include "intl.h"
|
|
#include "util.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 details(const char *msg, ...) {
|
|
va_list args;
|
|
va_start(args, msg);
|
|
|
|
printf(" " COLOR_RESET);
|
|
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);
|
|
}
|
|
|
|
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) {
|
|
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);
|
|
|
|
while (true) {
|
|
input(question);
|
|
|
|
int c = getchar();
|
|
if (c == '\n')
|
|
return false;
|
|
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;
|
|
}
|
|
|
|
error(_("Please answer with y/n"));
|
|
}
|
|
}
|