cmd: switch to new tty interface

This commit is contained in:
2024-11-20 22:12:50 +00:00
parent dafa74b1b9
commit 033e44f83f
3 changed files with 39 additions and 49 deletions

View File

@@ -3,12 +3,13 @@
#include <blue/cmd.h>
#include <blue/core/btree.h>
#include <blue/object/string.h>
#include <blue/term.h>
#include <blue/term/print.h>
#include <blue/term/tty.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define OUTPUT_STREAM stderr
#define OUTPUT_STREAM b_stderr
static struct b_btree command_list = {0};
@@ -414,7 +415,7 @@ static void get_usage_string(
b_string_append_cstr(out, " ");
if (opt->opt == (struct b_command_option *)B_COMMAND_INVALID_ID) {
if (opt->opt == (struct b_command_option *)(uintptr_t)B_COMMAND_INVALID_ID) {
b_string_append_cstr(out, "[OPTIONS]");
} else {
z__b_get_option_usage_string(
@@ -432,7 +433,7 @@ static void get_usage_string(
b_string_append_cstr(out, " ");
if (arg->arg == (struct b_command_arg *)B_COMMAND_INVALID_ID) {
if (arg->arg == (struct b_command_arg *)(uintptr_t)B_COMMAND_INVALID_ID) {
b_string_append_cstr(out, "[ARGS]");
} else {
z__b_get_arg_usage_string(arg->arg, false, out);
@@ -517,7 +518,7 @@ static void get_command_description(struct b_command *cmd, b_string *out)
static void print_options_list(struct b_command *cmd)
{
unsigned int term_width = 0;
b_term_get_dimensions(OUTPUT_STREAM, &term_width, NULL);
b_tty_get_dimensions(OUTPUT_STREAM, &term_width, NULL);
unsigned int newline_threshold = 1000000;
@@ -525,7 +526,7 @@ static void print_options_list(struct b_command *cmd)
newline_threshold = term_width - 12 - 35;
}
b_fprintf(OUTPUT_STREAM, "\n" F_YELLOW "OPTIONS:" F_RESET "\n");
b_tty_printf(OUTPUT_STREAM, 0, "\n" F_YELLOW "OPTIONS:" F_RESET "\n");
size_t desb_margin = 0;
b_string *opt_str = b_string_create();
@@ -565,8 +566,7 @@ static void print_options_list(struct b_command *cmd)
size_t i = 0;
b_queue_foreach (&it, &cmd->b_opt) {
struct b_command_option *opt
= b_unbox(struct b_command_option, it.entry, opt_entry);
struct b_command_option *opt = b_unbox(struct b_command_option, it.entry, opt_entry);
if (!opt) {
continue;
}
@@ -576,28 +576,24 @@ static void print_options_list(struct b_command *cmd)
z__b_get_option_usage_string(opt, CMD_STR_COLOUR, opt_str);
z__b_get_option_description(opt, desc_str);
size_t opt_len = b_string_get_size(
opt_str, B_STRLEN_IGNORE_ESC
| B_STRLEN_IGNORE_MOD)
+ 4;
size_t desc_len = b_string_get_size(
desc_str, B_STRLEN_IGNORE_ESC | B_STRLEN_IGNORE_MOD);
size_t opt_len = b_string_get_size(opt_str, B_STRLEN_IGNORE_ESC | B_STRLEN_IGNORE_MOD) + 4;
size_t desc_len = b_string_get_size(desc_str, B_STRLEN_IGNORE_ESC | B_STRLEN_IGNORE_MOD);
bool new_paragraph
= description_on_separate_line(opt_len, desc_len);
if (new_paragraph) {
fputc('\n', OUTPUT_STREAM);
b_tty_putc(OUTPUT_STREAM, 0, '\n');
}
fputs(" ", OUTPUT_STREAM);
b_fputs(b_string_ptr(opt_str), OUTPUT_STREAM);
b_tty_puts(OUTPUT_STREAM, 0, " ");
b_tty_puts(OUTPUT_STREAM, 0, b_string_ptr(opt_str));
if (new_paragraph) {
format.p_flags = 0;
format.p_left_margin = 8;
format.p_right_margin = 4;
fputc('\n', OUTPUT_STREAM);
b_tty_putc(OUTPUT_STREAM, 0, '\n');
} else {
format.p_flags = B_PARAGRAPH_DONT_INDENT_FIRST_LINE;
format.p_left_margin = desb_margin + 4;
@@ -606,14 +602,14 @@ static void print_options_list(struct b_command *cmd)
unsigned int len = opt_len;
while (len < format.p_left_margin) {
fputc(' ', OUTPUT_STREAM);
b_tty_putc(OUTPUT_STREAM, 0, ' ');
len++;
}
b_print_paragraph(b_string_ptr(desc_str), OUTPUT_STREAM, &format);
if (new_paragraph) {
fputc('\n', OUTPUT_STREAM);
b_tty_putc(OUTPUT_STREAM, 0, '\n');
}
}
@@ -623,7 +619,7 @@ static void print_options_list(struct b_command *cmd)
static void print_args_list(struct b_command *cmd)
{
b_fprintf(OUTPUT_STREAM, "\n" F_YELLOW "ARGS:" F_RESET "\n");
b_tty_printf(OUTPUT_STREAM, 0, "\n" F_YELLOW "ARGS:" F_RESET "\n");
size_t desb_margin = 0;
b_string *str = b_string_create();
@@ -657,14 +653,11 @@ static void print_args_list(struct b_command *cmd)
b_string_clear(str);
z__b_get_arg_usage_string(arg, true, str);
fputs(" ", OUTPUT_STREAM);
b_fputs(b_string_ptr(str), OUTPUT_STREAM);
unsigned int len
= b_string_get_size(
str, B_STRLEN_IGNORE_ESC | B_STRLEN_IGNORE_MOD)
+ 4;
b_tty_puts(OUTPUT_STREAM, 0, " ");
b_tty_puts(OUTPUT_STREAM, 0, b_string_ptr(str));
unsigned int len = b_string_get_size(str, B_STRLEN_IGNORE_ESC | B_STRLEN_IGNORE_MOD) + 4;
while (len < format.p_left_margin) {
fputc(' ', OUTPUT_STREAM);
b_tty_putc(OUTPUT_STREAM, 0, ' ');
len++;
}
@@ -679,7 +672,7 @@ static void print_args_list(struct b_command *cmd)
static void print_commands_list(struct b_command *cmd)
{
b_fprintf(OUTPUT_STREAM, "\n" F_YELLOW "COMMANDS:" F_RESET "\n");
b_tty_puts(OUTPUT_STREAM, 0, "\n" F_YELLOW "COMMANDS:" F_RESET "\n");
size_t desb_margin = 0;
b_string *str = b_string_create();
@@ -691,9 +684,7 @@ static void print_commands_list(struct b_command *cmd)
b_string_clear(str);
get_command_string(sub, str);
size_t len = b_string_get_size(
str, B_STRLEN_IGNORE_ESC | B_STRLEN_IGNORE_MOD)
+ 4;
size_t len = b_string_get_size( str, B_STRLEN_IGNORE_ESC | B_STRLEN_IGNORE_MOD) + 4;
if (len > desb_margin) {
desb_margin = len;
@@ -713,14 +704,11 @@ static void print_commands_list(struct b_command *cmd)
b_string_clear(str);
get_command_string(sub, str);
fputs(" ", OUTPUT_STREAM);
b_fputs(b_string_ptr(str), OUTPUT_STREAM);
unsigned int len
= b_string_get_size(
str, B_STRLEN_IGNORE_ESC | B_STRLEN_IGNORE_MOD)
+ 4;
b_tty_puts(OUTPUT_STREAM, 0, " ");
b_tty_puts(OUTPUT_STREAM, 0, b_string_ptr(str));
unsigned int len = b_string_get_size(str, B_STRLEN_IGNORE_ESC | B_STRLEN_IGNORE_MOD) + 4;
while (len < format.p_left_margin) {
fputc(' ', OUTPUT_STREAM);
b_tty_putc(OUTPUT_STREAM, 0, ' ');
len++;
}
@@ -833,7 +821,7 @@ static void print_usage(struct b_command *cmd)
b_paragraph_format format = {0};
format.p_left_margin = format.p_right_margin = 4;
b_fprintf(OUTPUT_STREAM, F_YELLOW "USAGE:" F_RESET "\n");
b_tty_puts(OUTPUT_STREAM, 0, F_YELLOW "USAGE:" F_RESET "\n");
if (b_queue_empty(&cmd->b_usage)) {
b_string *usage = z__b_command_default_usage_string(cmd, NULL);
@@ -864,14 +852,14 @@ static void print_help(struct b_command *cmd)
b_paragraph_format format = {0};
if (!cmd->b_parent) {
b_fprintf(OUTPUT_STREAM, F_GREEN "%s" F_RESET "\n", cmd->b_name);
b_tty_printf(OUTPUT_STREAM, 0, F_GREEN "%s" F_RESET "\n", cmd->b_name);
}
if (cmd->b_description) {
b_print_paragraph(cmd->b_description, OUTPUT_STREAM, &format);
}
b_fprintf(OUTPUT_STREAM, "\n");
b_tty_putc(OUTPUT_STREAM, 0, '\n');
print_usage(cmd);