From 3e451589d5eaecfa8ddff46877f94ad79bc010f0 Mon Sep 17 00:00:00 2001 From: Karel Zak Date: Thu, 18 Nov 2010 21:04:13 +0100 Subject: [PATCH] lib: [tt] add TT_FL_RIGHT, add columns list parser Signed-off-by: Karel Zak --- include/tt.h | 8 +++++-- lib/tt.c | 68 +++++++++++++++++++++++++++++++++++++++++++++------- 2 files changed, 65 insertions(+), 11 deletions(-) diff --git a/include/tt.h b/include/tt.h index bf24d550..e6a63ed3 100644 --- a/include/tt.h +++ b/include/tt.h @@ -12,11 +12,12 @@ #include "list.h" enum { - TT_FL_TRUNCATE = (1 << 1), + TT_FL_TRUNC = (1 << 1), TT_FL_TREE = (1 << 2), TT_FL_RAW = (1 << 3), TT_FL_ASCII = (1 << 4), - TT_FL_NOHEADINGS = (1 << 5) + TT_FL_NOHEADINGS = (1 << 5), + TT_FL_RIGHT = (1 << 6), }; struct tt { @@ -68,4 +69,7 @@ extern struct tt_line *tt_add_line(struct tt *tb, struct tt_line *parent); extern int tt_line_set_data(struct tt_line *ln, int colnum, const char *data); +extern int tt_parse_columns_list(const char *list, int cols[], int *ncols, + int (name2id)(const char *, size_t)); + #endif /* UTIL_LINUX_TT_H */ diff --git a/lib/tt.c b/lib/tt.c index a4aabfe0..f371a93a 100644 --- a/lib/tt.c +++ b/lib/tt.c @@ -394,12 +394,16 @@ static void recount_widths(struct tt *tb, char *buf, size_t bufsz) 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); @@ -412,7 +416,8 @@ static void recount_widths(struct tt *tb, char *buf, size_t bufsz) 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; } @@ -434,7 +439,7 @@ static void recount_widths(struct tt *tb, char *buf, size_t bufsz) 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; @@ -458,7 +463,7 @@ leave: 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)); @@ -498,20 +503,28 @@ static void print_data(struct tt *tb, struct tt_column *cl, char *data) 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); @@ -633,6 +646,43 @@ int tt_print_table(struct tt *tb) 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 #include @@ -666,7 +716,7 @@ int main(int argc, char *argv[]) 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); -- 2.39.5