178 lines
3.6 KiB
C
178 lines
3.6 KiB
C
#include "print.h"
|
|
|
|
#include <blue/term.h>
|
|
#include <ctype.h>
|
|
#include <stdarg.h>
|
|
#include <stdio.h>
|
|
|
|
#define F_BLACK "\033[30m"
|
|
#define F_RED "\033[91m"
|
|
#define F_YEL "\033[93m"
|
|
#define F_CYN "\033[96m"
|
|
|
|
#define F_BLACK_BOLD "\033[1;30m"
|
|
#define F_RED_BOLD "\033[1;91m"
|
|
#define F_YEL_BOLD "\033[1;93m"
|
|
#define F_CYN_BOLD "\033[1;96m"
|
|
|
|
#define F_BLACK_BG "\033[40m"
|
|
#define F_RED_BG "\033[101m"
|
|
#define F_YEL_BG "\033[103m"
|
|
#define F_CYN_BG "\033[106m"
|
|
|
|
#define F_RESET "\033[0m"
|
|
|
|
typedef b_status (*print_function)(FILE *fp, const char *s);
|
|
|
|
b_status b_term_get_dimensions(FILE *fp, unsigned int *w, unsigned int *h)
|
|
{
|
|
return z__b_stream_dimensions(fp, w, h) == 0 ? B_SUCCESS
|
|
: B_ERR_NOT_SUPPORTED;
|
|
}
|
|
|
|
static int print(FILE *fp, const char *str)
|
|
{
|
|
return fputs(str, fp);
|
|
}
|
|
|
|
static int print_no_ansi(FILE *fp, const char *str)
|
|
{
|
|
int out = 0;
|
|
|
|
for (size_t i = 0; str[i]; i++) {
|
|
if (str[i] != '\033') {
|
|
fputc(str[i], fp);
|
|
out++;
|
|
continue;
|
|
}
|
|
|
|
while (str[i] && !isalpha(str[i])) {
|
|
i++;
|
|
}
|
|
}
|
|
|
|
return out;
|
|
}
|
|
|
|
static b_status print_normal(FILE *fp, const char *s)
|
|
{
|
|
return B_SUCCESS;
|
|
}
|
|
|
|
static b_status print_i(FILE *fp, const char *s)
|
|
{
|
|
b_paragraph_format format = {};
|
|
format.p_left_margin = 5;
|
|
format.p_right_margin = 5;
|
|
format.p_flags = B_PARAGRAPH_DONT_INDENT_FIRST_LINE
|
|
| B_PARAGRAPH_MULTI_LINE_BREAK;
|
|
|
|
// printf(F_CYN_BG F_BLACK " i " F_RESET " ");
|
|
b_fprintf(fp, F_CYN_BOLD " i:" F_RESET " ");
|
|
b_print_paragraph(s, fp, &format);
|
|
|
|
return B_SUCCESS;
|
|
}
|
|
|
|
static b_status print_warn(FILE *fp, const char *s)
|
|
{
|
|
b_paragraph_format format = {};
|
|
format.p_left_margin = 5;
|
|
format.p_right_margin = 5;
|
|
format.p_flags = B_PARAGRAPH_DONT_INDENT_FIRST_LINE
|
|
| B_PARAGRAPH_MULTI_LINE_BREAK;
|
|
|
|
// printf(F_YEL_BG F_BLACK " Warn " F_RESET " ");
|
|
b_fprintf(fp, F_YEL_BOLD " !!" F_RESET " ");
|
|
b_print_paragraph(s, fp, &format);
|
|
|
|
return B_SUCCESS;
|
|
}
|
|
|
|
static b_status print_err(FILE *fp, const char *s)
|
|
{
|
|
b_paragraph_format format = {};
|
|
format.p_left_margin = 5;
|
|
format.p_right_margin = 5;
|
|
format.p_flags = B_PARAGRAPH_DONT_INDENT_FIRST_LINE
|
|
| B_PARAGRAPH_MULTI_LINE_BREAK;
|
|
|
|
// printf(F_RED_BG F_BLACK " Err " F_RESET " ");
|
|
b_fprintf(fp, F_RED_BOLD "Err:" F_RESET " ");
|
|
b_print_paragraph(s, fp, &format);
|
|
|
|
return B_SUCCESS;
|
|
}
|
|
|
|
static print_function format_printers[] = {
|
|
[B_PRINT_NORMAL] = print_normal,
|
|
[B_PRINT_I] = print_i,
|
|
[B_PRINT_WARN] = print_warn,
|
|
[B_PRINT_ERR] = print_err,
|
|
};
|
|
static size_t nr_format_printers
|
|
= sizeof format_printers / sizeof format_printers[B_PRINT_NORMAL];
|
|
|
|
b_status b_print(b_print_format format, const char *str, ...)
|
|
{
|
|
char buf[1024];
|
|
|
|
va_list arg;
|
|
va_start(arg, str);
|
|
vsnprintf(buf, sizeof buf, str, arg);
|
|
va_end(arg);
|
|
|
|
if (format >= nr_format_printers) {
|
|
return B_ERR_INVALID_ARGUMENT;
|
|
}
|
|
|
|
print_function printer = format_printers[format];
|
|
if (!printer) {
|
|
return B_ERR_INVALID_ARGUMENT;
|
|
}
|
|
|
|
return printer(stdout, buf);
|
|
}
|
|
|
|
static int b_vfprintf(FILE *fp, const char *format, va_list arg)
|
|
{
|
|
char str[1024];
|
|
|
|
vsnprintf(str, sizeof str, format, arg);
|
|
|
|
if (z__b_stream_is_tty(fp)) {
|
|
return print(fp, str);
|
|
}
|
|
|
|
return print_no_ansi(fp, str);
|
|
}
|
|
|
|
int b_fputs(const char *str, FILE *fp)
|
|
{
|
|
if (z__b_stream_is_tty(fp)) {
|
|
return print(fp, str);
|
|
}
|
|
|
|
return print_no_ansi(fp, str);
|
|
}
|
|
|
|
int b_printf(const char *format, ...)
|
|
{
|
|
va_list arg;
|
|
va_start(arg, format);
|
|
int len = b_vfprintf(stdout, format, arg);
|
|
va_end(arg);
|
|
|
|
return len;
|
|
}
|
|
|
|
int b_fprintf(FILE *fp, const char *format, ...)
|
|
{
|
|
va_list arg;
|
|
va_start(arg, format);
|
|
int len = b_vfprintf(fp, format, arg);
|
|
va_end(arg);
|
|
|
|
return len;
|
|
}
|