]> mj.ucw.cz Git - libucw.git/commitdiff
tableprinter: added blockline formatter
authorRobert Kessl <kesslr@centrum.cz>
Fri, 20 Jun 2014 11:55:18 +0000 (13:55 +0200)
committerRobert Kessl <kesslr@centrum.cz>
Fri, 20 Jun 2014 11:55:18 +0000 (13:55 +0200)
ucw/table-test.t
ucw/table.c
ucw/table.h

index 6a0f54a2ca72d026805c3288c2c0c62de436a69e..cdf54881bb7501f7b20aca44a3ad3bf471471d23 100644 (file)
@@ -64,6 +64,21 @@ trueAHOJsdsdf,aaaaaAHOJ1.50
 falseAHOJtest,bbbbbAHOJ1.50
 EOF
 
+Run: ../obj/ucw/table-test -T 'cols:*' -T fmt:blockline
+Out <<EOF
+col0_str: sdsdf,aaaaa
+col1_int: 10000
+col2_uint: XXX-22222
+col3_bool: true
+col4_double: 1.50
+
+col0_str: test,bbbbb
+col1_int: -100
+col2_uint: 100
+col3_bool: false
+col4_double: 1.50
+
+EOF
 
 Run: ../obj/ucw/table-test -n
 Out <<EOF
index 02076ec4ea42efa259c055287c47f177bbf38843..cc3c9add3701865b8e340bf54aa313b5c4b61217 100644 (file)
@@ -133,6 +133,14 @@ void table_set_col_order(struct table *tbl, int *col_order, int cols_to_output)
  **/
 const char * table_set_col_order_by_name(struct table *tbl, const char *col_order_str)
 {
+  if(col_order_str[0] == '*') {
+    tbl->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
index c476cb18888c84f46cca8cc436817bcd6087555b..e1a0b50417ea512c2b13564e469ebbe7a93971cd 100644 (file)
@@ -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.