From: Robert Kessl Date: Fri, 20 Jun 2014 11:55:18 +0000 (+0200) Subject: tableprinter: added blockline formatter X-Git-Tag: v6.0~6 X-Git-Url: http://mj.ucw.cz/gitweb/?a=commitdiff_plain;h=a2d95ade72ecf275d658e93c1c7d649d7a30f8b7;p=libucw.git tableprinter: added blockline formatter --- diff --git a/ucw/table-test.t b/ucw/table-test.t index 6a0f54a2..cdf54881 100644 --- a/ucw/table-test.t +++ b/ucw/table-test.t @@ -64,6 +64,21 @@ trueAHOJsdsdf,aaaaaAHOJ1.50 falseAHOJtest,bbbbbAHOJ1.50 EOF +Run: ../obj/ucw/table-test -T 'cols:*' -T fmt:blockline +Out <column_order = mp_alloc(tbl->pool, sizeof(int) * tbl->column_count); + tbl->cols_to_output = tbl->column_count; + for(uint i = 0; i < tbl->cols_to_output; i++) tbl->column_order[i] = i; + + return NULL; + } + if(!col_order_str[0]) { tbl->column_order = mp_alloc(tbl->pool, 0); tbl->cols_to_output = 0; @@ -360,6 +368,7 @@ const char *table_set_option_value(struct table *tbl, const char *key, const cha } else if(strcmp(key, "fmt") == 0) { if(strcmp(value, "human") == 0) table_set_formatter(tbl, &table_fmt_human_readable); else if(strcmp(value, "machine") == 0) table_set_formatter(tbl, &table_fmt_machine_readable); + else if(strcmp(value, "blockline") == 0) table_set_formatter(tbl, &table_fmt_blockline); else { return "Tableprinter: invalid argument to output-type option."; } @@ -495,6 +504,36 @@ struct table_formatter table_fmt_machine_readable = { .table_start = table_start_machine_readable, }; + +/*** Blockline formatter ***/ + +static void table_row_blockline_output(struct table *tbl) +{ + for(uint i = 0; i < tbl->cols_to_output; i++) { + int col_idx = tbl->column_order[i]; + bprintf(tbl->out, "%s: %s\n", tbl->columns[col_idx].name, tbl->col_str_ptrs[col_idx]); + } + bputc(tbl->out, '\n'); +} + +static void table_start_blockline(struct table *tbl) +{ + if(tbl->col_delimiter == NULL) { + tbl->col_delimiter = " "; + } + + if(tbl->append_delimiter == NULL) { + tbl->append_delimiter = ","; + } +} + +struct table_formatter table_fmt_blockline = { + .row_output = table_row_blockline_output, + .table_start = table_start_blockline +}; + + + /*** Tests ***/ #ifdef TEST diff --git a/ucw/table.h b/ucw/table.h index c476cb18..e1a0b504 100644 --- a/ucw/table.h +++ b/ucw/table.h @@ -108,6 +108,7 @@ enum column_type { #define TBL_APPEND_DELIMITER(_delimiter_) .append_delimiter = _delimiter_ #define TBL_OUTPUT_HUMAN_READABLE .formatter = &table_fmt_human_readable +#define TBL_OUTPUT_BLOCKLINE .formatter = &table_fmt_blockline #define TBL_OUTPUT_MACHINE_READABLE .formatter = &table_fmt_machine_readable /*** @@ -298,6 +299,7 @@ struct table_formatter { // Standard formatters extern struct table_formatter table_fmt_human_readable; extern struct table_formatter table_fmt_machine_readable; +extern struct table_formatter table_fmt_blockline; /** * Process the table one option and sets the values in @tbl according to the command-line parameters.