term: extend tty interface with more features

This commit is contained in:
2024-11-20 22:12:36 +00:00
parent 7e440f213c
commit dafa74b1b9
13 changed files with 3163 additions and 317 deletions

View File

@@ -1,7 +1,7 @@
#include "print.h"
#include <blue/object/string.h>
#include <blue/term.h>
#include <blue/term/print.h>
#include <ctype.h>
#include <stdbool.h>
#include <stdlib.h>
@@ -10,17 +10,17 @@
#define DEFAULT_PARAGRAPH_WIDTH 160
static void indent(
struct b_paragraph_format *format, FILE *fp, unsigned int margin, bool tty)
struct b_paragraph_format *format, struct b_tty *tty, unsigned int margin)
{
unsigned int x = 0;
while (x < margin) {
fputs(" ", fp);
b_tty_puts(tty, 0, " ");
x++;
}
}
static unsigned int extract_line(
const char **sp, unsigned int line_length, b_string *out,
const char **sp, unsigned int line_length, struct b_string *out,
struct b_paragraph_format *format)
{
const char *start = *sp;
@@ -96,10 +96,10 @@ static unsigned int extract_line(
}
static b_status print_paragraph_tty(
const char *str, FILE *fp, struct b_paragraph_format *format)
const char *str, struct b_tty *tty, struct b_paragraph_format *format)
{
unsigned int w = 0, h = 0;
z__b_stream_dimensions(fp, &w, &h);
b_tty_get_dimensions(tty, &w, &h);
if (!w) {
w = DEFAULT_PARAGRAPH_WIDTH;
@@ -119,7 +119,7 @@ static b_status print_paragraph_tty(
}
if (!(format->p_flags & B_PARAGRAPH_DONT_INDENT_FIRST_LINE)) {
indent(format, fp, left_margin, true);
indent(format, tty, left_margin);
}
b_string *line = b_string_create();
@@ -128,7 +128,7 @@ static b_status print_paragraph_tty(
while (str) {
if (*str == '\n') {
if (format->p_flags & B_PARAGRAPH_DOUBLE_LINE_BREAK) {
fputc('\n', fp);
b_tty_putc(tty, 0, '\n');
}
str++;
@@ -136,7 +136,7 @@ static b_status print_paragraph_tty(
while (*str == '\n') {
if (format->p_flags & B_PARAGRAPH_MULTI_LINE_BREAK) {
fputc('\n', fp);
b_tty_putc(tty, 0, '\n');
}
str++;
@@ -151,11 +151,11 @@ static b_status print_paragraph_tty(
}
if (need_indent) {
indent(format, fp, left_margin, false);
indent(format, tty, left_margin);
}
b_fputs(b_string_ptr(line), fp);
fputc('\n', fp);
b_tty_puts(tty, 0, b_string_ptr(line));
b_tty_putc(tty, 0, '\n');
need_indent = true;
}
@@ -171,11 +171,7 @@ static b_status print_paragraph_file(
}
b_status b_print_paragraph(
const char *str, FILE *fp, struct b_paragraph_format *format)
const char *str, struct b_tty *fp, struct b_paragraph_format *format)
{
if (z__b_stream_is_tty(fp)) {
return print_paragraph_tty(str, fp, format);
}
return print_paragraph_tty(str, fp, format);
}