runtime: format: add truncation and single-line support to value_writer

This commit is contained in:
2026-06-20 15:10:15 +01:00
parent c262b53eab
commit 4a567a1bb9
7 changed files with 160 additions and 53 deletions
+29 -24
View File
@@ -167,7 +167,7 @@ enum bshell_status bshell_table_ctx_prepare_columns_from_format(
if (column_defs[i].col_width <= tty_width) {
columns[i].col_width = column_defs[i].col_width;
tty_width -= columns[i].col_width;
tty_width -= (columns[i].col_width + 1);
continue;
}
@@ -289,6 +289,10 @@ enum bshell_status bshell_table_ctx_print_headers(struct bshell_table_ctx *ctx)
fx_stream_write_char(ctx->ctx_out, ' ');
}
if (i < ctx->ctx_columns_count - 1) {
fx_stream_write_char(ctx->ctx_out, ' ');
}
if (to_write != title_chars_remaining) {
repeat = true;
}
@@ -316,6 +320,10 @@ enum bshell_status bshell_table_ctx_print_headers(struct bshell_table_ctx *ctx)
for (; x < c->col_width; x++) {
fx_stream_write_char(ctx->ctx_out, ' ');
}
if (i < ctx->ctx_columns_count - 1) {
fx_stream_write_char(ctx->ctx_out, ' ');
}
}
reset_tty_colour();
@@ -327,14 +335,22 @@ enum bshell_status bshell_table_ctx_print_record(
struct bshell_table_ctx *ctx,
const fx_value *record)
{
fx_stringstream *strm = fx_stringstream_create();
struct bshell_value_writer writer;
size_t written = 0;
for (size_t i = 0; i < ctx->ctx_columns_count; i++) {
struct bshell_table_ctx_column *c = &ctx->ctx_columns[i];
if (c->col_width == 0) {
continue;
}
fx_stringstream_reset(strm);
bshell_value_writer_init(
&writer,
BSHELL_VALUE_WRITER_NO_NEWLINE
| BSHELL_VALUE_WRITER_ONELINE
| BSHELL_VALUE_WRITER_TRUNCATE,
c->col_width,
ctx->ctx_out);
fx_value column_value = FX_VALUE_EMPTY;
if (c->col_property) {
fx_status status = fx_property_get_value(
@@ -342,34 +358,23 @@ enum bshell_status bshell_table_ctx_print_record(
record,
&column_value);
if (FX_OK(status)) {
fx_value_to_string(&column_value, strm, NULL);
written = bshell_value_writer_write(
&writer,
&column_value);
}
}
size_t to_write = fx_stringstream_get_length(strm);
const char *s = fx_stringstream_get_cstr(strm);
bool overflow = false;
if (to_write > c->col_width) {
to_write = c->col_width - 2;
overflow = true;
}
size_t x;
for (x = 0; x < to_write; x++) {
fx_stream_write_char(ctx->ctx_out, s[x]);
}
if (overflow) {
fx_stream_write_char(ctx->ctx_out, L'');
x++;
}
for (; x < c->col_width; x++) {
for (size_t x = written; x < c->col_width; x++) {
fx_stream_write_char(ctx->ctx_out, ' ');
}
if (i < ctx->ctx_columns_count - 1) {
fx_stream_write_char(ctx->ctx_out, ' ');
}
bshell_value_writer_cleanup(&writer);
}
fx_stream_write_char(ctx->ctx_out, '\n');
fx_stringstream_unref(strm);
return BSHELL_SUCCESS;
}