new_inst->column_order[i].cell_content = NULL;
int col_idx = new_inst->column_order[i].idx;
new_inst->column_order[i].col_def = new_inst->columns + col_idx;
- new_inst->column_order[i].output_type = tbl_template->column_order[i].output_type;
+ new_inst->column_order[i].output_type = tbl_template->columns[col_idx].fmt;
}
new_inst->cols_to_output = tbl_template->cols_to_output;
tbl->column_order[i].idx = col_idx;
tbl->column_order[i].col_def = tbl->columns + col_idx;
tbl->column_order[i].cell_content = NULL;
- tbl->column_order[i].output_type = XTYPE_FMT_DEFAULT;
+ tbl->column_order[i].output_type = tbl->columns[col_idx].fmt;
}
table_update_ll(tbl);
}
tbl->column_order[curr_col_idx].col_def = tbl->columns + col_idx;
tbl->column_order[curr_col_idx].idx = col_idx;
tbl->column_order[curr_col_idx].cell_content = NULL;
- tbl->column_order[curr_col_idx].output_type = XTYPE_FMT_DEFAULT;
+ tbl->column_order[curr_col_idx].output_type = tbl->columns[col_idx].fmt;
if(tbl->columns[col_idx].type_def && tbl->columns[col_idx].set_col_instance_option) {
char *err = NULL;
tbl->columns[col_idx].set_col_instance_option(tbl, curr_col_idx, arg, &err);
return "Invalid argument to output-type option.";
}
return NULL;
+ } else if(strcmp(key, "cell-fmt") == 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);
+ 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;
TEST_ANY_COL0_INT, TEST_ANY_COL1_ANY
};
-static struct table_col_instance test_any_column_order[] = { TBL_COL(TEST_ANY_COL0_INT), TBL_COL(TEST_ANY_COL1_ANY) };
+static struct table_col_instance test_any_column_order[] = { TBL_COL(TEST_ANY_COL0_INT), TBL_COL_FMT(TEST_ANY_COL1_ANY, XTYPE_FMT_PRETTY) };
static struct table_template test_any_tbl = {
TBL_COLUMNS {
[TEST_ANY_COL0_INT] = TBL_COL_INT("col0_int", 8),
- [TEST_ANY_COL1_ANY] = TBL_COL_ANY("col1_any", 9),
+ [TEST_ANY_COL1_ANY] = TBL_COL_ANY_FMT("col1_any", 9, XTYPE_FMT_PRETTY),
TBL_COL_END
},
TBL_COL_ORDER(test_any_column_order),
#define TBL_COL_INTMAX(_name, _width) { .name = _name, .width = _width, .fmt = XTYPE_FMT_DEFAULT, .type_def = COL_TYPE_INTMAX, TBL_COL_LIST_INIT }
#define TBL_COL_UINTMAX(_name, _width) { .name = _name, .width = _width, .fmt = XTYPE_FMT_DEFAULT, .type_def = COL_TYPE_UINTMAX, TBL_COL_LIST_INIT }
#define TBL_COL_HEXUINT(_name, _width) { .name = _name, .width = _width, .fmt = XTYPE_FMT_DEFAULT, .type_def = COL_TYPE_UINT, TBL_COL_LIST_INIT }
-#define TBL_COL_DOUBLE(_name, _width) { .name = _name, .width = _width, .fmt = XTYPE_FMT_DEFAULT, .type_def = COL_TYPE_DOUBLE, TBL_COL_LIST_INIT }
+#define TBL_COL_DOUBLE(_name, _width) { .name = _name, .width = _width, .fmt = XTYPE_FMT_DEFAULT, .type_def = COL_TYPE_DOUBLE, TBL_COL_LIST_INIT }
#define TBL_COL_BOOL(_name, _width) { .name = _name, .width = _width, .fmt = XTYPE_FMT_DEFAULT, .type_def = COL_TYPE_BOOL, TBL_COL_LIST_INIT }
#define TBL_COL_ANY(_name, _width) { .name = _name, .width = _width, .fmt = XTYPE_FMT_DEFAULT, .type_def = COL_TYPE_ANY, TBL_COL_LIST_INIT }
#define TBL_COL_CUSTOM(_name, _width, _xtype) { .name = _name, .width = _width, .fmt = XTYPE_FMT_DEFAULT, .type_def = _xtype, TBL_COL_LIST_INIT }
#define TBL_COL_UINTMAX_FMT(_name, _width, _fmt) { .name = _name, .width = _width, .fmt = _fmt, .type_def = COL_TYPE_UINTMAX, TBL_COL_LIST_INIT }
#define TBL_COL_HEXUINT_FMT(_name, _width, _fmt) { .name = _name, .width = _width, .fmt = _fmt, .type_def = COL_TYPE_UINT, TBL_COL_LIST_INIT }
#define TBL_COL_BOOL_FMT(_name, _width, _fmt) { .name = _name, .width = _width, .fmt = _fmt, .type_def = COL_TYPE_BOOL, TBL_COL_LIST_INIT }
-#define TBL_COL_DOUBLE_FMT(_name, _width, _fmt) { .name = _name, .width = _width, .fmt = _fmt, .type_def = COL_TYPE_DOUBLE, TBL_COL_LIST_INIT }
+#define TBL_COL_ANY_FMT(_name, _width, _fmt) { .name = _name, .width = _width, .fmt = _fmt, .type_def = COL_TYPE_ANY, TBL_COL_LIST_INIT }
+#define TBL_COL_DOUBLE_FMT(_name, _width, _fmt) { .name = _name, .width = _width, .fmt = _fmt, .type_def = COL_TYPE_DOUBLE, TBL_COL_LIST_INIT }
#define TBL_COL_END { .name = 0, .width = 0, .fmt = 0, .type_def = NULL }
#define TBL_COL_ORDER(order) .column_order = (struct table_col_instance *) order, .cols_to_output = ARRAY_SIZE(order)
#define TBL_COL_DELIMITER(_delimiter_) .col_delimiter = _delimiter_
#define TBL_COL(_idx) { .idx = _idx, .output_type = XTYPE_FMT_DEFAULT, .next_column = -1 }
-#define TBL_COL_FMT(_idx, _fmt) { .idx = _idx, .output_type = XTYPE_FMT_DEFAULT, .next_column = -1, .fmt = _fmt }
+#define TBL_COL_FMT(_idx, _fmt) { .idx = _idx, .output_type = _fmt, .next_column = -1 }
#define TBL_COL_TYPE(_idx, _type) { .idx = _idx, .output_type = _type, .next_column = -1 }
#define TBL_OUTPUT_HUMAN_READABLE .formatter = &table_fmt_human_readable
TABLE_COL_PROTO(bool, bool);
/** macros that enables easy definitions of bodies of table_col_<something> functions **/
+// FIXME: these macros really do not do what they are supposed to do :)
+// the problem is the default formatting
#define TABLE_COL(_name_, _type_, _typeconst_) void table_col_##_name_(struct table *tbl, int col, _type_ val)\
{\
- enum xtype_fmt fmt = tbl->columns[col].fmt;\
- table_col_##_name_##_fmt(tbl, col, fmt, val);\
+ ASSERT_MSG(col < tbl->column_count && col >= 0, "Table column %d does not exist.", col);\
+ ASSERT(tbl->columns[col].type_def == COL_TYPE_ANY || _typeconst_ == 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) {\
+ if(tbl->columns[col].type_def != COL_TYPE_ANY) cell_content = tbl->columns[col].type_def->format(&val, curr_col->output_type, tbl->pool);\
+ else cell_content = (_typeconst_)->format(&val, curr_col->output_type, tbl->pool);\
+ curr_col->cell_content = cell_content;\
+ } TBL_COL_ITER_END\
}
#define TABLE_COL_STR(_name_, _type_, _typeconst_) void table_col_##_name_##_name(struct table *tbl, const char *col_name, _type_ val)\
tbl->last_printed_col = col;\
tbl->row_printing_started = 1;\
const char *cell_content = NULL;\
- if(tbl->columns[col].type_def != COL_TYPE_ANY) cell_content = tbl->columns[col].type_def->format(&val, fmt, tbl->pool);\
- else cell_content = (_typeconst_)->format(&val, fmt, tbl->pool);\
- table_set_all_inst_content(tbl, col, cell_content);\
+ if(tbl->columns[col].type_def != COL_TYPE_ANY) cell_content = tbl->columns[col].type_def->format(&val, fmt, tbl->pool);\
+ else cell_content = (_typeconst_)->format(&val, fmt, tbl->pool);\
+ TBL_COL_ITER_START(tbl, col, curr_col, curr_col_idx) {\
+ curr_col->cell_content = cell_content;\
+ } TBL_COL_ITER_END\
}
#define TABLE_COL_BODIES(_name_, _type_, _typeconst_) TABLE_COL(_name_, _type_, _typeconst_); \