Files

147 lines
3.2 KiB
C
Raw Permalink Normal View History

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