[TEST_COL1_TS] = TBL_COL_TIMESTAMP("ts", 20),
TBL_COL_END
},
- TBL_OUTPUT_HUMAN_READABLE,
+ TBL_FMT_HUMAN_READABLE,
};
static void do_test(void)
[TEST_COL1_TS] = TBL_COL_TIMESTAMP("ts", 20),
TBL_COL_END
},
- TBL_OUTPUT_HUMAN_READABLE,
+ TBL_FMT_HUMAN_READABLE,
};
static void do_test2(void)
[test_col4_double] = TBL_COL_DOUBLE_FMT("col4_double", 11 | CELL_ALIGN_LEFT, XTYPE_FMT_DEFAULT),
TBL_COL_END
},
- TBL_OUTPUT_HUMAN_READABLE,
+ TBL_FMT_HUMAN_READABLE,
TBL_COL_DELIMITER("\t"),
};
TBL_COL_END
},
TBL_COL_ORDER(test_column_order),
- TBL_OUTPUT_HUMAN_READABLE,
+ TBL_FMT_HUMAN_READABLE,
TBL_COL_DELIMITER("\t"),
};
[TEST_DEFAULT_ORDER_COL2_INT] = TBL_COL_INT("col2_int", 9),
TBL_COL_END
},
- TBL_OUTPUT_HUMAN_READABLE,
+ TBL_FMT_HUMAN_READABLE,
TBL_COL_DELIMITER("\t"),
};
ASSERT(tbl->columns[col].type_def == COL_TYPE_ANY || expected_type == tbl->columns[col].type_def);
tbl->last_printed_col = col;
tbl->row_printing_started = 1;
- const char *cell_content = NULL;
TBL_COL_ITER_START(tbl, col, curr_col, curr_col_idx) {
enum xtype_fmt fmt = curr_col->output_type;
- cell_content = expected_type->format(value, fmt, tbl->pool);
- curr_col->cell_content = cell_content;
+ curr_col->cell_content = expected_type->format(value, fmt, tbl->pool);
} TBL_COL_ITER_END
}
TABLE_COL_BODY(s64, s64)
TABLE_COL_BODY(u64, u64)
TABLE_COL_BODY(bool, bool)
-
-void table_col_str(struct table *tbl, int col, const char *val) {
- table_col_generic_format(tbl, col, (void*)val, &xt_str);
-}
+TABLE_COL_BODY(str, const char *)
void table_reset_row(struct table *tbl)
{
return "Invalid argument to output-type option.";
}
return NULL;
- } else if(strcmp(key, "cell-fmt") == 0) {
+ } else if(strcmp(key, "cells") == 0) {
u32 fmt = 0;
const char *err = xtype_parse_fmt(NULL, value, &fmt, tbl->pool);
if(err) return mp_printf(tbl->pool, "Invalid cell format: '%s'.", err);
tbl->column_order[i].output_type = fmt;
}
return NULL;
+ } else if(strcmp(key, "raw") == 0 || strcmp(key, "pretty") == 0) {
+ u32 fmt = 0;
+ const char *err = xtype_parse_fmt(NULL, key, &fmt, tbl->pool);
+ if(err) return mp_printf(tbl->pool, "Invalid cell format: '%s'.", err);
+ for(uint i = 0; i < tbl->cols_to_output; i++) {
+ tbl->column_order[i].output_type = fmt;
+ }
+ return NULL;
} else if(strcmp(key, "col-delim") == 0) {
char * d = mp_printf(tbl->pool, "%s", value);
tbl->col_delimiter = d;
TBL_COL_END
},
TBL_COL_ORDER(test_column_order),
- TBL_OUTPUT_HUMAN_READABLE,
+ TBL_FMT_HUMAN_READABLE,
TBL_COL_DELIMITER("\t"),
};
TBL_COL_END
},
TBL_COL_ORDER(test_any_column_order),
- TBL_OUTPUT_HUMAN_READABLE,
+ TBL_FMT_HUMAN_READABLE,
TBL_COL_DELIMITER("\t"),
};
* * `TBL_COL_END` ends the column definitions
* * `TBL_COL_ORDER` specifies custom ordering of columns in the output
* * `TBL_COL_DELIMITER` and `TBL_APPEND_DELIMITER` override default delimiters
- * * `TBL_OUTPUT_HUMAN_READABLE` requests human-readable formatting (this is the default)
- * * `TBL_OUTPUT_MACHINE_READABLE` requests machine-readable TSV output
- * * `TBL_OUTPUT_BLOCKLINE` requests block formatting (each cell printed a pair of a key and value on its own line)
+ * * `TBL_FMT__HUMAN_READABLE` requests human-readable formatting (this is the default)
+ * * `TBL_FMT_MACHINE_READABLE` requests machine-readable TSV output
+ * * `TBL_FMT_BLOCKLINE` requests block formatting (each cell printed a pair of a key and value on its own line)
*
***/
#define TBL_COL(_idx) { .idx = _idx, .output_type = XTYPE_FMT_DEFAULT, .next_column = -1 }
#define TBL_COL_FMT(_idx, _fmt) { .idx = _idx, .output_type = _fmt, .next_column = -1 }
-#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
+#define TBL_FMT_HUMAN_READABLE .formatter = &table_fmt_human_readable
+#define TBL_FMT_BLOCKLINE .formatter = &table_fmt_blockline
+#define TBL_FMT_MACHINE_READABLE .formatter = &table_fmt_machine_readable
/**
* The TBL_COL_ITER_START macro are used for iterating over all instances of a particular column in
* a custom printf-like format string
* * `table_col_`'type'`_name(table, name, value)` refers to the column
* by its name instead of its index.
- * * `table_append_`'type'`(table, value)` appends a value to the most
- * recently accessed cell.
***/
* | `cols` | comma-separated column list | set order of columns
* | `fmt` | `human`/`machine`/`block` | set table formatter to one of the built-in formatters
* | `col-delim`| string | set column delimiter
-* | `cell-fmt` | string | set column format type
+* | `cells` | string | set column format mode
* |===================================================================================================
**/
const char *table_set_option_value(struct table *tbl, const char *key, const char *value);
errno = 0;
double result = strtod(str, &endptr);
if(*endptr != 0 || endptr == str) return "Could not parse double.";
- if(errno == ERANGE) return "Could not parse double: overflow happend during parsing.";
+ if(errno == ERANGE) return "Could not parse double.";
*((double *) dest) = result;
return NULL;
}
-const struct xtype xt_double = {
- .size = sizeof(double),
- .name = "double",
- .parse = xt_double_parse,
- .format = xt_double_format,
-};
+XTYPE_NUM_STRUCT(double, double)
/* bool */
return "Could not parse bool.";
}
-const struct xtype xt_bool = {
- .size = sizeof(bool),
- .name = "bool",
- .parse = xt_bool_parse,
- .format = xt_bool_format,
-};
+XTYPE_NUM_STRUCT(bool, bool)
/* str */
static const char *xt_str_format(void *src, u32 fmt UNUSED, struct mempool *pool)
{
- return mp_strdup(pool, (const char *) src);
+ return mp_strdup(pool, *((const char **) src));
}
static const char *xt_str_parse(const char *str, void *dest, struct mempool *pool UNUSED)
return NULL;
}
-const struct xtype xt_str = {
- .size = sizeof(char *),
- .name = "str",
- .parse = xt_str_parse,
- .format = xt_str_format,
-};
+XTYPE_NUM_STRUCT(char *, str)