core: write-output now writes args and input values to the pipeline rather than stdout

the runtime will handle writing any output values to the terminal if necessary. instead,
write-output can now be used to write arbitrary values into the pipeline for later commands
to use.
This commit is contained in:
2026-06-13 13:49:00 +01:00
parent 8c926acd9d
commit 97a83addc8
+12 -57
View File
@@ -1,6 +1,5 @@
#include <bshell/command/cmdlet.h>
#include <bshell/command/command.h>
#include <bshell/format.h>
#include <bshell/runtime/pipeline.h>
#include <bshell/status.h>
#include <fx/collections/array.h>
@@ -19,69 +18,18 @@ FX_TYPE_CLASS_DECLARATION_BEGIN(bshell_write_output)
FX_TYPE_CLASS_DECLARATION_END(bshell_write_output)
struct bshell_write_output_p {
struct bshell_table_ctx w_table_ctx;
bool w_table_initialised;
size_t w_arg_count;
size_t w_arg_index;
};
static void initialise_table(
struct bshell_write_output_p *cmd,
const fx_value *first_record)
{
bshell_table_ctx_init(&cmd->w_table_ctx, fx_stdout);
const fx_type *ty = fx_type_get_by_id(first_record->v_type);
bshell_table_ctx_prepare_columns_from_format(
&cmd->w_table_ctx,
ty,
NULL);
bshell_table_ctx_print_headers(&cmd->w_table_ctx);
cmd->w_table_initialised = true;
}
static bool value_requires_table(const fx_value *value)
{
if (fx_type_is_value_type(value->v_type)) {
return false;
}
if (fx_value_is_type(value, FX_TYPE_STRING)
|| fx_value_is_type(value, FX_TYPE_ARRAY)) {
return false;
}
return true;
}
static void write_value(
struct bshell_write_output_p *cmd,
const fx_value *value)
{
if (!value_requires_table(value)) {
fx_value_to_string(value, fx_stdout, NULL);
fx_stream_write_char(fx_stdout, '\n');
return;
}
if (!cmd->w_table_initialised) {
initialise_table(cmd, value);
}
bshell_table_ctx_print_record(&cmd->w_table_ctx, value);
}
static enum bshell_status begin_processing(
bshell_cmdlet *cmdlet,
enum bshell_command_position position)
{
struct bshell_write_output_p *p
= fx_object_get_private(cmdlet, BSHELL_TYPE_WRITE_OUTPUT);
for (size_t i = 1;; i++) {
const fx_value *v = bshell_command_get_arg(cmdlet, i);
if (!v) {
break;
}
write_value(p, v);
}
p->w_arg_index = 1;
p->w_arg_count = bshell_command_get_arg_count(cmdlet);
return BSHELL_SUCCESS;
}
@@ -94,9 +42,16 @@ static enum bshell_status process_record(
struct bshell_write_output_p *p
= fx_object_get_private(cmdlet, BSHELL_TYPE_WRITE_OUTPUT);
if (p->w_arg_index < p->w_arg_count) {
const fx_value *arg
= bshell_command_get_arg(cmdlet, p->w_arg_index++);
bshell_pipeline_write_value(pipeline, *arg, false);
return BSHELL_SUCCESS;
}
fx_value in = bshell_pipeline_read_value(pipeline);
if (fx_value_is_set(&in)) {
write_value(p, &in);
bshell_pipeline_write_value(pipeline, in, false);
fx_value_unset(&in);
}