Files
fx/term/print.c

147 lines
3.2 KiB
C
Raw Normal View History

2024-10-24 21:31:22 +01:00
#include "print.h"
2026-03-16 10:35:43 +00:00
#include <fx/core/hash.h>
#include <fx/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
2026-03-16 10:35:43 +00:00
typedef fx_status (*print_function)(struct fx_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;
}
2026-03-16 10:35:43 +00:00
static fx_status print_normal(struct fx_tty *tty, const char *s)
2024-10-24 21:31:22 +01:00
{
2026-03-16 10:35:43 +00:00
return FX_SUCCESS;
2024-10-24 21:31:22 +01:00
}
2026-03-16 10:35:43 +00:00
static fx_status print_i(struct fx_tty *tty, const char *s)
2024-10-24 21:31:22 +01:00
{
2026-03-16 10:35:43 +00:00
struct fx_paragraph_format format = {0};
2024-10-24 21:31:22 +01:00
format.p_left_margin = 5;
format.p_right_margin = 5;
2026-03-16 10:35:43 +00:00
format.p_flags = FX_PARAGRAPH_DONT_INDENT_FIRST_LINE
| FX_PARAGRAPH_MULTI_LINE_BREAK;
2024-10-24 21:31:22 +01:00
// printf(F_CYN_BG F_BLACK " i " F_RESET " ");
2026-03-16 10:35:43 +00:00
fx_tty_puts(tty, 0, F_CYN_BOLD " i:" F_RESET " ");
fx_print_paragraph(s, tty, &format);
2024-10-24 21:31:22 +01:00
2026-03-16 10:35:43 +00:00
return FX_SUCCESS;
2024-10-24 21:31:22 +01:00
}
2026-03-16 10:35:43 +00:00
static fx_status print_warn(struct fx_tty *tty, const char *s)
2024-10-24 21:31:22 +01:00
{
2026-03-16 10:35:43 +00:00
struct fx_paragraph_format format = {0};
2024-10-24 21:31:22 +01:00
format.p_left_margin = 5;
format.p_right_margin = 5;
2026-03-16 10:35:43 +00:00
format.p_flags = FX_PARAGRAPH_DONT_INDENT_FIRST_LINE
| FX_PARAGRAPH_MULTI_LINE_BREAK;
2024-10-24 21:31:22 +01:00
// printf(F_YEL_BG F_BLACK " Warn " F_RESET " ");
2026-03-16 10:35:43 +00:00
fx_tty_puts(tty, 0, F_YEL_BOLD " !!" F_RESET " ");
fx_print_paragraph(s, tty, &format);
2024-10-24 21:31:22 +01:00
2026-03-16 10:35:43 +00:00
return FX_SUCCESS;
2024-10-24 21:31:22 +01:00
}
2026-03-16 10:35:43 +00:00
static fx_status print_err(struct fx_tty *tty, const char *s)
2024-10-24 21:31:22 +01:00
{
2026-03-16 10:35:43 +00:00
struct fx_paragraph_format format = {0};
2024-10-24 21:31:22 +01:00
format.p_left_margin = 5;
format.p_right_margin = 5;
2026-03-16 10:35:43 +00:00
format.p_flags = FX_PARAGRAPH_DONT_INDENT_FIRST_LINE
| FX_PARAGRAPH_MULTI_LINE_BREAK;
2024-10-24 21:31:22 +01:00
// printf(F_RED_BG F_BLACK " Err " F_RESET " ");
2026-03-16 10:35:43 +00:00
fx_tty_puts(tty, 0, F_RED_BOLD "Err:" F_RESET " ");
fx_print_paragraph(s, tty, &format);
2024-10-24 21:31:22 +01:00
2026-03-16 10:35:43 +00:00
return FX_SUCCESS;
2024-10-24 21:31:22 +01:00
}
static print_function format_printers[] = {
2026-03-16 10:35:43 +00:00
[FX_PRINT_NORMAL] = print_normal,
[FX_PRINT_I] = print_i,
[FX_PRINT_WARN] = print_warn,
[FX_PRINT_ERR] = print_err,
2024-10-24 21:31:22 +01:00
};
static size_t nr_format_printers
2026-03-16 10:35:43 +00:00
= sizeof format_printers / sizeof format_printers[FX_PRINT_NORMAL];
2024-10-24 21:31:22 +01:00
2026-03-16 10:35:43 +00:00
fx_status fx_print(fx_print_format format, const char *str, ...)
2024-10-24 21:31:22 +01:00
{
char buf[1024];
va_list arg;
va_start(arg, str);
vsnprintf(buf, sizeof buf, str, arg);
va_end(arg);
if (format >= nr_format_printers) {
2026-03-16 10:35:43 +00:00
return FX_ERR_INVALID_ARGUMENT;
2024-10-24 21:31:22 +01:00
}
print_function printer = format_printers[format];
if (!printer) {
2026-03-16 10:35:43 +00:00
return FX_ERR_INVALID_ARGUMENT;
2024-10-24 21:31:22 +01:00
}
2026-03-16 10:35:43 +00:00
return printer(fx_stdtty, buf);
2024-10-24 21:31:22 +01:00
}
2026-03-16 10:35:43 +00:00
int fx_putc(char c)
2024-10-24 21:31:22 +01:00
{
2026-03-16 10:35:43 +00:00
return fx_tty_putc(fx_stdtty, 0, c);
2024-10-24 21:31:22 +01:00
}
2026-03-16 10:35:43 +00:00
int fx_puts(const char* s)
2024-10-24 21:31:22 +01:00
{
2026-03-16 10:35:43 +00:00
return fx_tty_puts(fx_stdtty, 0, s);
2024-10-24 21:31:22 +01:00
}
2026-03-16 10:35:43 +00:00
int fx_printf(const char* format, ...)
2024-10-24 21:31:22 +01:00
{
va_list arg;
va_start(arg, format);
2026-03-16 10:35:43 +00:00
int x = fx_tty_vprintf(
fx_stdtty, FX_TTY_DISABLE_INTERPOLATED_FORMATTING, format, arg);
2024-10-24 21:31:22 +01:00
va_end(arg);
return x;
}