struct tt_column *cl =
list_entry(p, struct tt_column, cl_columns);
- cl->width_min = mbs_width(cl->name);
+ if (cl->name)
+ cl->width_min = mbs_width(cl->name);
if (cl->width < cl->width_min)
cl->width = cl->width_min;
+
else if (cl->width_hint >= 1 &&
+ cl->width < (int) cl->width_hint &&
cl->width_min < (int) cl->width_hint)
+
cl->width = (int) cl->width_hint;
width += cl->width + (is_last_column(tb, cl) ? 0 : 1);
struct tt_column *cl = list_entry(
tb->tb_columns.prev, struct tt_column, cl_columns);
- cl->width += tb->termwidth - width;
+ if (!(cl->flags & TT_FL_RIGHT))
+ cl->width += tb->termwidth - width;
goto leave;
}
continue; /* never truncate columns with absolute sizes */
if (cl->flags & TT_FL_TREE)
continue; /* never truncate the tree */
- if (trunc_only && !(cl->flags & TT_FL_TRUNCATE))
+ if (trunc_only && !(cl->flags & TT_FL_TRUNC))
continue;
if (cl->width == cl->width_min)
continue;
struct tt_column *cl =
list_entry(p, struct tt_column, cl_columns);
- fprintf(stderr, "width: %s=%d [%d]\n",
+ fprintf(stderr, "width: %s=%d [hint=%d]\n",
cl->name, cl->width,
cl->width_hint > 1 ? (int) cl->width_hint :
(int) (cl->width_hint * tb->termwidth));
width = len;
/* truncate data */
- if (len > width && (cl->flags & TT_FL_TRUNCATE)) {
+ if (len > width && (cl->flags & TT_FL_TRUNC)) {
len = mbs_truncate(data, width);
if (!data || len == (size_t) -1) {
len = 0;
data = NULL;
}
}
- if (data)
- fputs(data, stdout);
+ if (data) {
+ if (!(tb->flags & TT_FL_RAW) && (cl->flags & TT_FL_RIGHT)) {
+ int xw = cl->width;
+ fprintf(stdout, "%*s", xw, data);
+ if (len < xw)
+ len = xw;
+ }
+ else
+ fputs(data, stdout);
+ }
for (i = len; i < width; i++)
fputc(' ', stdout); /* padding */
if (!is_last_column(tb, cl)) {
- if (len > width && !(cl->flags & TT_FL_TRUNCATE)) {
+ if (len > width && !(cl->flags & TT_FL_TRUNC)) {
fputc('\n', stdout);
for (i = 0; i <= cl->seqnum; i++) {
struct tt_column *x = tt_get_column(tb, i);
return 0;
}
+int tt_parse_columns_list(const char *list, int cols[], int *ncols,
+ int (name2id)(const char *, size_t))
+{
+ const char *begin = NULL, *p;
+
+ if (!list || !*list || !cols || !ncols || !name2id)
+ return -1;
+
+ *ncols = 0;
+
+ for (p = list; p && *p; p++) {
+ const char *end = NULL;
+ int id;
+
+ if (!begin)
+ begin = p; /* begin of the column name */
+ if (*p == ',')
+ end = p; /* terminate the name */
+ if (*(p + 1) == '\0')
+ end = p + 1; /* end of string */
+ if (!begin || !end)
+ continue;
+ if (end <= begin)
+ return -1;
+
+ id = name2id(begin, end - begin);
+ if (id == -1)
+ return -1;
+ cols[ *ncols ] = id;
+ (*ncols)++;
+ begin = NULL;
+ if (end && !*end)
+ break;
+ }
+ return 0;
+}
+
#ifdef TEST_PROGRAM
#include <err.h>
#include <errno.h>
err(EXIT_FAILURE, "table initialization failed");
tt_define_column(tb, "NAME", 0.3, notree ? 0 : TT_FL_TREE);
- tt_define_column(tb, "FOO", 0.3, TT_FL_TRUNCATE);
+ tt_define_column(tb, "FOO", 0.3, TT_FL_TRUNC);
tt_define_column(tb, "BAR", 0.3, 0);
tt_define_column(tb, "PATH", 0.3, 0);