runtime: move value formatting functionality to a dedicated api
This commit is contained in:
@@ -21,17 +21,10 @@ static void format_value_fallback(const fx_value *value, fx_stream *dest)
|
|||||||
|
|
||||||
enum bshell_status format_value_default(const fx_value *value, fx_stream *dest)
|
enum bshell_status format_value_default(const fx_value *value, fx_stream *dest)
|
||||||
{
|
{
|
||||||
if (fx_type_is_value_type(value->v_type)) {
|
struct bshell_value_writer writer;
|
||||||
format_value_fallback(value, dest);
|
bshell_value_writer_init(&writer, dest);
|
||||||
return BSHELL_SUCCESS;
|
bshell_value_writer_write(&writer, value);
|
||||||
}
|
bshell_value_writer_cleanup(&writer);
|
||||||
|
|
||||||
fx_object *object = NULL;
|
|
||||||
fx_value_get_object(value, &object);
|
|
||||||
enum bshell_status status = format_object_table(object, dest);
|
|
||||||
if (status != BSHELL_SUCCESS) {
|
|
||||||
format_value_fallback(value, dest);
|
|
||||||
}
|
|
||||||
|
|
||||||
return BSHELL_SUCCESS;
|
return BSHELL_SUCCESS;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,85 @@
|
|||||||
|
#include <bshell/format.h>
|
||||||
|
#include <fx/collections/array.h>
|
||||||
|
#include <fx/string.h>
|
||||||
|
#include <fx/type.h>
|
||||||
|
#include <fx/value-type.h>
|
||||||
|
|
||||||
|
static void initialise_table(
|
||||||
|
struct bshell_value_writer *w,
|
||||||
|
const fx_value *first_record)
|
||||||
|
{
|
||||||
|
bshell_table_ctx_init(&w->w_table_ctx, w->w_dest);
|
||||||
|
const fx_type *ty = fx_type_get_by_id(first_record->v_type);
|
||||||
|
bshell_table_ctx_prepare_columns_from_format(&w->w_table_ctx, ty, NULL);
|
||||||
|
bshell_table_ctx_print_headers(&w->w_table_ctx);
|
||||||
|
w->w_table_initialised = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
static void write_iterable(struct bshell_value_writer *w, const fx_value *value)
|
||||||
|
{
|
||||||
|
fx_array *array = NULL;
|
||||||
|
fx_value_get_object(value, &array);
|
||||||
|
|
||||||
|
const fx_iterator *it = fx_iterator_begin(array);
|
||||||
|
fx_foreach(v, it)
|
||||||
|
{
|
||||||
|
bshell_value_writer_write(w, v);
|
||||||
|
}
|
||||||
|
fx_iterator_unref(it);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void write_string(struct bshell_value_writer *w, const fx_value *value)
|
||||||
|
{
|
||||||
|
const char *str = NULL;
|
||||||
|
fx_value_get_cstr(value, &str);
|
||||||
|
fx_stream_write_cstr(w->w_dest, str, NULL);
|
||||||
|
fx_stream_write_char(w->w_dest, '\n');
|
||||||
|
}
|
||||||
|
|
||||||
|
static void write_value(struct bshell_value_writer *w, const fx_value *value)
|
||||||
|
{
|
||||||
|
const fx_type *ty = fx_type_get_by_id(value->v_type);
|
||||||
|
if (fx_value_is_type(value, FX_TYPE_STRING)) {
|
||||||
|
write_string(w, value);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (fx_type_get_interface(ty, FX_TYPE_ITERABLE)) {
|
||||||
|
write_iterable(w, value);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (fx_type_is_value_type(value->v_type)) {
|
||||||
|
fx_value_to_string(value, w->w_dest, NULL);
|
||||||
|
fx_stream_write_char(w->w_dest, '\n');
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!w->w_table_initialised) {
|
||||||
|
initialise_table(w, value);
|
||||||
|
}
|
||||||
|
|
||||||
|
bshell_table_ctx_print_record(&w->w_table_ctx, value);
|
||||||
|
}
|
||||||
|
|
||||||
|
void bshell_value_writer_init(struct bshell_value_writer *w, fx_stream *dest)
|
||||||
|
{
|
||||||
|
memset(w, 0x00, sizeof *w);
|
||||||
|
w->w_dest = dest;
|
||||||
|
}
|
||||||
|
|
||||||
|
void bshell_value_writer_write(
|
||||||
|
struct bshell_value_writer *w,
|
||||||
|
const fx_value *value)
|
||||||
|
{
|
||||||
|
write_value(w, value);
|
||||||
|
}
|
||||||
|
|
||||||
|
void bshell_value_writer_cleanup(struct bshell_value_writer *w)
|
||||||
|
{
|
||||||
|
if (w->w_table_initialised) {
|
||||||
|
bshell_table_ctx_cleanup(&w->w_table_ctx);
|
||||||
|
}
|
||||||
|
|
||||||
|
w->w_table_initialised = false;
|
||||||
|
}
|
||||||
@@ -47,6 +47,12 @@ struct bshell_table_ctx {
|
|||||||
fx_stream *ctx_out;
|
fx_stream *ctx_out;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
struct bshell_value_writer {
|
||||||
|
struct bshell_table_ctx w_table_ctx;
|
||||||
|
bool w_table_initialised;
|
||||||
|
fx_stream *w_dest;
|
||||||
|
};
|
||||||
|
|
||||||
extern enum bshell_status bshell_table_init(void);
|
extern enum bshell_status bshell_table_init(void);
|
||||||
extern const struct bshell_table *bshell_table_get_for_type(const fx_type *ty);
|
extern const struct bshell_table *bshell_table_get_for_type(const fx_type *ty);
|
||||||
extern enum bshell_status format_object_table(
|
extern enum bshell_status format_object_table(
|
||||||
@@ -74,4 +80,12 @@ extern enum bshell_status bshell_table_ctx_print_record(
|
|||||||
struct bshell_table_ctx *ctx,
|
struct bshell_table_ctx *ctx,
|
||||||
const fx_value *record);
|
const fx_value *record);
|
||||||
|
|
||||||
|
extern void bshell_value_writer_init(
|
||||||
|
struct bshell_value_writer *w,
|
||||||
|
fx_stream *dest);
|
||||||
|
extern void bshell_value_writer_write(
|
||||||
|
struct bshell_value_writer *w,
|
||||||
|
const fx_value *value);
|
||||||
|
extern void bshell_value_writer_cleanup(struct bshell_value_writer *w);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
@@ -3,6 +3,7 @@
|
|||||||
|
|
||||||
#include <bshell/command/command.h>
|
#include <bshell/command/command.h>
|
||||||
#include <bshell/command/function.h>
|
#include <bshell/command/function.h>
|
||||||
|
#include <bshell/format.h>
|
||||||
#include <bshell/runtime/script-block.h>
|
#include <bshell/runtime/script-block.h>
|
||||||
#include <bshell/runtime/var.h>
|
#include <bshell/runtime/var.h>
|
||||||
#include <fx/collections/hashtable.h>
|
#include <fx/collections/hashtable.h>
|
||||||
@@ -15,6 +16,7 @@ struct bshell_runtime {
|
|||||||
struct bshell_runtime_scope *rt_global;
|
struct bshell_runtime_scope *rt_global;
|
||||||
fx_hashtable *rt_aliases;
|
fx_hashtable *rt_aliases;
|
||||||
fx_queue rt_scope;
|
fx_queue rt_scope;
|
||||||
|
struct bshell_value_writer rt_writer;
|
||||||
};
|
};
|
||||||
|
|
||||||
extern struct bshell_runtime *bshell_runtime_create(void);
|
extern struct bshell_runtime *bshell_runtime_create(void);
|
||||||
@@ -53,4 +55,10 @@ extern struct bshell_runtime_scope *bshell_runtime_get_parent_scope(
|
|||||||
struct bshell_runtime *rt,
|
struct bshell_runtime *rt,
|
||||||
struct bshell_runtime_scope *scope);
|
struct bshell_runtime_scope *scope);
|
||||||
|
|
||||||
|
extern void bshell_runtime_begin_output(struct bshell_runtime *rt);
|
||||||
|
extern void bshell_runtime_output_value(
|
||||||
|
struct bshell_runtime *rt,
|
||||||
|
const fx_value *value);
|
||||||
|
extern void bshell_runtime_end_output(struct bshell_runtime *rt);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
@@ -353,10 +353,8 @@ static enum bshell_status eval_instruction(
|
|||||||
}
|
}
|
||||||
|
|
||||||
int end_of_data = 0;
|
int end_of_data = 0;
|
||||||
bool columns_initialised = false;
|
|
||||||
fx_value record = FX_VALUE_EMPTY;
|
fx_value record = FX_VALUE_EMPTY;
|
||||||
struct bshell_table_ctx table_ctx;
|
bshell_runtime_begin_output(rt);
|
||||||
bshell_table_ctx_init(&table_ctx, fx_stdout);
|
|
||||||
while (1) {
|
while (1) {
|
||||||
bstatus = bshell_pipeline_pump_record(
|
bstatus = bshell_pipeline_pump_record(
|
||||||
pipeline,
|
pipeline,
|
||||||
@@ -371,22 +369,11 @@ static enum bshell_status eval_instruction(
|
|||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!columns_initialised) {
|
bshell_runtime_output_value(rt, &record);
|
||||||
const fx_type *ty
|
|
||||||
= fx_type_get_by_id(record.v_type);
|
|
||||||
bshell_table_ctx_prepare_columns_from_format(
|
|
||||||
&table_ctx,
|
|
||||||
ty,
|
|
||||||
NULL);
|
|
||||||
bshell_table_ctx_print_headers(&table_ctx);
|
|
||||||
columns_initialised = true;
|
|
||||||
}
|
|
||||||
|
|
||||||
bshell_table_ctx_print_record(&table_ctx, &record);
|
|
||||||
fx_value_unset(&record);
|
fx_value_unset(&record);
|
||||||
}
|
}
|
||||||
|
|
||||||
bshell_table_ctx_cleanup(&table_ctx);
|
bshell_runtime_end_output(rt);
|
||||||
bshell_pipeline_unref(pipeline);
|
bshell_pipeline_unref(pipeline);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,20 @@
|
|||||||
|
#include <bshell/runtime/runtime.h>
|
||||||
|
#include <fx/type.h>
|
||||||
|
#include <fx/value-type.h>
|
||||||
|
|
||||||
|
void bshell_runtime_begin_output(struct bshell_runtime *rt)
|
||||||
|
{
|
||||||
|
bshell_value_writer_init(&rt->rt_writer, fx_stdout);
|
||||||
|
}
|
||||||
|
|
||||||
|
void bshell_runtime_output_value(
|
||||||
|
struct bshell_runtime *rt,
|
||||||
|
const fx_value *value)
|
||||||
|
{
|
||||||
|
bshell_value_writer_write(&rt->rt_writer, value);
|
||||||
|
}
|
||||||
|
|
||||||
|
void bshell_runtime_end_output(struct bshell_runtime *rt)
|
||||||
|
{
|
||||||
|
bshell_value_writer_cleanup(&rt->rt_writer);
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user