147 lines
3.2 KiB
C
147 lines
3.2 KiB
C
#include "print.h"
|
|
|
|
#include <blue/core/hash.h>
|
|
#include <blue/term/print.h>
|
|
#include <ctype.h>
|
|
#include <stdarg.h>
|
|
#include <stdbool.h>
|
|
#include <stdio.h>
|
|
#include <string.h>
|
|
|
|
#define F_BLACK "[black]"
|
|
#define F_RED "[red]"
|
|
#define F_YEL "[yellow]"
|
|
#define F_CYN "[cyan]"
|
|
|
|
#define F_BLACK_BOLD "[bold,black]"
|
|
#define F_RED_BOLD "[bold,red]"
|
|
#define F_YEL_BOLD "[bold,yellow]"
|
|
#define F_CYN_BOLD "[bold,cyan]"
|
|
|
|
#define F_BLACK_BG "[bg_black]"
|
|
#define F_RED_BG "[bg_reg]"
|
|
#define F_YEL_BG "[bg_yellow]"
|
|
#define F_CYN_BG "[bg_cyan]"
|
|
|
|
#define F_RESET "[reset]"
|
|
|
|
typedef b_status (*print_function)(struct b_tty *tty, const char *s);
|
|
|
|
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(struct b_tty *tty, const char *s)
|
|
{
|
|
return B_SUCCESS;
|
|
}
|
|
|
|
static b_status print_i(struct b_tty *tty, const char *s)
|
|
{
|
|
struct b_paragraph_format format = {0};
|
|
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_tty_puts(tty, 0, F_CYN_BOLD " i:" F_RESET " ");
|
|
b_print_paragraph(s, tty, &format);
|
|
|
|
return B_SUCCESS;
|
|
}
|
|
|
|
static b_status print_warn(struct b_tty *tty, const char *s)
|
|
{
|
|
struct b_paragraph_format format = {0};
|
|
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_tty_puts(tty, 0, F_YEL_BOLD " !!" F_RESET " ");
|
|
b_print_paragraph(s, tty, &format);
|
|
|
|
return B_SUCCESS;
|
|
}
|
|
|
|
static b_status print_err(struct b_tty *tty, const char *s)
|
|
{
|
|
struct b_paragraph_format format = {0};
|
|
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_tty_puts(tty, 0, F_RED_BOLD "Err:" F_RESET " ");
|
|
b_print_paragraph(s, tty, &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(b_stdtty, buf);
|
|
}
|
|
|
|
int b_putc(char c)
|
|
{
|
|
return b_tty_putc(b_stdtty, 0, c);
|
|
}
|
|
|
|
int b_puts(const char* s)
|
|
{
|
|
return b_tty_puts(b_stdtty, 0, s);
|
|
}
|
|
|
|
int b_printf(const char* format, ...)
|
|
{
|
|
va_list arg;
|
|
va_start(arg, format);
|
|
int x = b_tty_vprintf(
|
|
b_stdtty, B_TTY_DISABLE_INTERPOLATED_FORMATTING, format, arg);
|
|
va_end(arg);
|
|
return x;
|
|
} |