};
static struct format *in_format, *out_format;
-static int want_trim;
+static int want_trim, want_equalize, want_stats;
struct field {
int start_pos;
static void update_stats(void)
{
+ if (!want_stats)
+ return;
+
for (int i = 0; i < fields_count(&out_fields); i++) {
struct field *f = fields_nth(&out_fields, i);
intarray_t *w = &column_widths;
unsigned char *p = get_field(&out_fields, i, &len);
fw = field_chars(fields_nth(&out_fields, i));
if (fw > cw) {
- warn(fmt, "Internal error: Wrongly calculated column width (%d > %d)", fw, cw);
+ warn(fmt, "Internal error: Wrongly calculated width of column %d (%d > %d)", i, fw, cw);
cw = fw;
}
while (len--)
}
}
+static void equalize_fields(void)
+{
+ while (fields_count(&out_fields) < intarray_count(&column_widths)) {
+ struct field *f = fields_push(&out_fields);
+ f->start_pos = f->len = 0;
+ }
+}
+
/*** Field names and headers ***/
struct field_names {
// This is tricky: when we are formatting a table, field names are normally
// calculated in pass 1, but the header is written in pass 2, so we have to
// update column statistics, because field name can be too wide to fit.
+ want_stats++;
update_stats();
+ want_stats--;
+ if (want_equalize)
+ equalize_fields();
write_grid(-1);
write_line();
write_grid(0);
else
select_all_fields();
- if (out_format->needs_stats)
- update_stats();
-
+ if (want_equalize && (pass & 2))
+ equalize_fields();
+ update_stats();
write_line();
}
out_format->read_line = tmp_read;
out_format->write_line = tmp_write;
out_format->tmp_file = tmpfile();
- out_format->needs_stats = final_format->needs_stats;
out_format->field_names = in_format->field_names;
one_pass(1);
rewind(in_format->tmp_file);
line_number = 0;
out_format = final_format;
- out_format->needs_stats = 0;
+ want_stats = 0;
one_pass(2);
fclose(in_format->tmp_file);
}
\n\
Other options:\n\
--trim Trim leading and trailing whitespaces in fields\n\
+ --equalize Pad all lines to the maximum number of fields\n\
");
exit(0);
}
OPT_TABLE,
OPT_TABLE_SEP,
OPT_GRID,
+ OPT_EQUALIZE,
};
static const struct option long_options[] = {
{ "always-quote", 0, NULL, OPT_ALWAYS_QUOTE },
{ "csv", 0, NULL, 'c' },
+ { "equalize", 0, NULL, OPT_EQUALIZE },
{ "fields", 1, NULL, 'f' },
{ "fs", 1, NULL, 'd' },
{ "grid", 0, NULL, OPT_GRID },
case OPT_GRID:
current_format()->table_grid = 1;
break;
+ case OPT_EQUALIZE:
+ want_equalize = 1;
+ break;
default:
bad_args(NULL);
}
}
finish_parse_selectors();
- if (out_format->needs_stats)
+ want_stats = out_format->needs_stats | want_equalize;
+ if (want_stats)
two_pass();
else
one_pass(3);