From 8a38a8d31edc23db15eb031193df14da66dcdaff Mon Sep 17 00:00:00 2001 From: Ondrej Oprala Date: Fri, 21 Mar 2014 12:37:52 +0100 Subject: [PATCH] libsmartcols: separate flags, add getters/setters Signed-off-by: Ondrej Oprala --- libsmartcols/src/column.c | 184 ++++++++++++++++++--- libsmartcols/src/libsmartcols.h.in | 57 ++++--- libsmartcols/src/libsmartcols.sym | 27 ++- libsmartcols/src/smartcolsP.h | 18 +- libsmartcols/src/table.c | 253 ++++++++++++++++++++++++++--- libsmartcols/src/table_print.c | 38 ++--- libsmartcols/src/test.c | 29 ++-- 7 files changed, 498 insertions(+), 108 deletions(-) diff --git a/libsmartcols/src/column.c b/libsmartcols/src/column.c index 6e6ec1759b..5fad5319fc 100644 --- a/libsmartcols/src/column.c +++ b/libsmartcols/src/column.c @@ -65,8 +65,12 @@ struct libscols_column *scols_copy_column(const struct libscols_column *cl) ret->width_max = cl->width_max; ret->width_avg = cl->width_avg; ret->width_hint = cl->width_hint; - ret->flags = cl->flags; ret->is_extreme = cl->is_extreme; + ret->trunc = cl->trunc; + ret->tree = cl->tree; + ret->right = cl->right; + ret->strict_width = cl->strict_width; + ret->no_extremes = cl->no_extremes; return ret; err: @@ -91,23 +95,6 @@ double scols_column_get_whint(struct libscols_column *cl) return cl ? cl->width_hint : -EINVAL; } -int scols_column_set_flags(struct libscols_column *cl, int flags) -{ - assert(cl); - - if (!cl) - return -EINVAL; - - cl->flags = flags; - return 0; -} - -int scols_column_get_flags(struct libscols_column *cl) -{ - assert(cl); - return cl ? cl->flags : -EINVAL; -} - struct libscols_cell *scols_column_get_header(struct libscols_column *cl) { assert(cl); @@ -147,5 +134,164 @@ const char *scols_column_get_color(struct libscols_column *cl) return cl ? cl->color : NULL; } +/** + * scols_column_is_trunc: + * @cl: column + * + * Get the value of trunc + * + * Returns: trunc flag value, negative value in case of an error. + */ +int scols_column_is_trunc(struct libscols_column *cl) +{ + assert(cl); + if (!cl) + return -EINVAL; + return cl->trunc; +} +/** + * scols_column_is_tree: + * @cl: column + * + * Get the value of tree + * + * Returns: tree flag value, negative value in case of an error. + */ +int scols_column_is_tree(struct libscols_column *cl) +{ + assert(cl); + if (!cl) + return -EINVAL; + return cl->tree; +} +/** + * scols_column_is_right: + * @cl: column + * + * Get the value of right + * + * Returns: right flag value, negative value in case of an error. + */ +int scols_column_is_right(struct libscols_column *cl) +{ + assert(cl); + if (!cl) + return -EINVAL; + return cl->right; +} +/** + * scols_column_is_strict_width: + * @cl: column + * + * Get the value of strict_width + * + * Returns: strict_width flag value, negative value in case of an error. + */ +int scols_column_is_strict_width(struct libscols_column *cl) +{ + assert(cl); + if (!cl) + return -EINVAL; + return cl->strict_width; +} +/** + * scols_column_is_no_extremes: + * @cl: column + * + * Get the value of no_extremes + * + * Returns: no_extremes flag value, negative value in case of an error. + */ +int scols_column_is_no_extremes(struct libscols_column *cl) +{ + assert(cl); + if (!cl) + return -EINVAL; + return cl->no_extremes; +} - +/** + * scols_column_set_trunc: + * @cl: column + * @enable: 1 or 0 + * + * Enable/disable trunc + * + * Returns: 0 on success, negative number in case of an error. + */ +int scols_column_set_trunc(struct libscols_column *cl, int enable) +{ + assert(cl); + if (!cl) + return -EINVAL; + cl->trunc = enable; + return 0; +} +/** + * scols_column_set_tree: + * @cl: column + * @enable: 1 or 0 + * + * Enable/disable tree + * + * Returns: 0 on success, negative number in case of an error. + */ +int scols_column_set_tree(struct libscols_column *cl, int enable) +{ + assert(cl); + if (!cl) + return -EINVAL; + cl->tree = enable; + return 0; +} +/** + * scols_column_set_right: + * @cl: column + * @enable: 1 or 0 + * + * Enable/disable right + * + * Returns: 0 on success, negative number in case of an error. + */ +int scols_column_set_right(struct libscols_column *cl, int enable) +{ + assert(cl); + if (!cl) + return -EINVAL; + cl->right = enable; + return 0; +} +/** + * scols_column_set_strict_width: + * @cl: column + * @enable: 1 or 0 + * + * Enable/disable strict_width + * + * Returns: 0 on success, negative number in case of an error. + */ +int scols_column_set_strict_width(struct libscols_column *cl, int enable) +{ + assert(cl); + if (!cl) + return -EINVAL; + cl->strict_width = enable; + return 0; +} +/** + * scols_column_set_no_extremes: + * @cl: column + * @enable: 1 or 0 + * + * Enable/disable no_extremes + * + * Returns: 0 on success, negative number in case of an error. + */ +int scols_column_set_no_extremes(struct libscols_column *cl, int enable) +{ + assert(cl); + if (!cl) + return -EINVAL; + cl->no_extremes = enable; + return 0; +} diff --git a/libsmartcols/src/libsmartcols.h.in b/libsmartcols/src/libsmartcols.h.in index cbc5f0a682..412c8cd09c 100644 --- a/libsmartcols/src/libsmartcols.h.in +++ b/libsmartcols/src/libsmartcols.h.in @@ -24,7 +24,7 @@ struct libscols_symbols; struct libscols_cell; struct libscols_line; struct libscols_table; - +struct libscols_column; /* iter.c */ enum { @@ -33,27 +33,6 @@ enum { SCOLS_ITER_BACKWARD }; - -enum { - /* - * Global flags - */ - SCOLS_FL_RAW = (1 << 1), - SCOLS_FL_ASCII = (1 << 2), - SCOLS_FL_NOHEADINGS = (1 << 3), - SCOLS_FL_EXPORT = (1 << 4), - SCOLS_FL_MAX = (1 << 5), - - /* - * Column flags - */ - SCOLS_FL_TRUNC = (1 << 15), /* truncate fields data if necessary */ - SCOLS_FL_TREE = (1 << 16), /* use tree "ascii art" */ - SCOLS_FL_RIGHT = (1 << 17), /* align to the right */ - SCOLS_FL_STRICTWIDTH = (1 << 18), /* don't reduce width if column is empty */ - SCOLS_FL_NOEXTREMES = (1 << 19), /* ignore extreme fields when count column width*/ -}; - extern struct libscols_iter *scols_new_iter(int direction); extern void scols_free_iter(struct libscols_iter *itr); extern void scols_reset_iter(struct libscols_iter *itr, int direction); @@ -79,14 +58,23 @@ extern int scols_cell_set_color(struct libscols_cell *ce, const char *color); extern const char *scols_cell_get_color(const struct libscols_cell *ce); /* column.c */ +extern int scols_column_is_tree(struct libscols_column *cl); +extern int scols_column_set_tree(struct libscols_column *cl, int enable); +extern int scols_column_is_trunc(struct libscols_column *cl); +extern int scols_column_set_trunc(struct libscols_column *cl, int enable); +extern int scols_column_is_right(struct libscols_column *cl); +extern int scols_column_set_right(struct libscols_column *cl, int enable); +extern int scols_column_is_strict_width(struct libscols_column *cl); +extern int scols_column_set_strict_width(struct libscols_column *cl, int enable); +extern int scols_column_is_no_extremes(struct libscols_column *cl); +extern int scols_column_set_no_extremes(struct libscols_column *cl, int enable); + extern struct libscols_column *scols_new_column(void); extern void scols_ref_column(struct libscols_column *cl); extern void scols_unref_column(struct libscols_column *cl); extern struct libscols_column *scols_copy_column(const struct libscols_column *cl); extern int scols_column_set_whint(struct libscols_column *cl, double whint); extern double scols_column_get_whint(struct libscols_column *cl); -extern int scols_column_set_flags(struct libscols_column *cl, int flags); -extern int scols_column_get_flags(struct libscols_column *cl); extern struct libscols_cell *scols_column_get_header(struct libscols_column *cl); extern int scols_column_set_color(struct libscols_column *cl, const char *color); extern const char *scols_column_get_color(struct libscols_column *cl); @@ -114,17 +102,32 @@ extern int scols_line_refer_data(struct libscols_line *ln, size_t n, char *data) extern struct libscols_line *scols_copy_line(struct libscols_line *ln); /* table */ -extern struct libscols_table *scols_new_table(int flags, struct libscols_symbols *syms); +extern int scols_table_colors_wanted(struct libscols_table *tb); +extern int scols_table_is_raw(struct libscols_table *tb); +extern int scols_table_is_ascii(struct libscols_table *tb); +extern int scols_table_is_no_headings(struct libscols_table *tb); +extern int scols_table_is_export(struct libscols_table *tb); +extern int scols_table_is_max(struct libscols_table *tb); +extern int scols_table_is_tree(struct libscols_table *tb); + +extern int scols_table_enable_colors(struct libscols_table *tb, int enable); +extern int scols_table_set_raw(struct libscols_table *tb, int enable); +extern int scols_table_set_ascii(struct libscols_table *tb, int enable); +extern int scols_table_set_no_headings(struct libscols_table *tb, int enable); +extern int scols_table_set_export(struct libscols_table *tb, int enable); +extern int scols_table_set_max(struct libscols_table *tb, int enable); +extern int scols_table_set_tree(struct libscols_table *tb, int enable); + +extern struct libscols_table *scols_new_table(struct libscols_symbols *syms); extern void scols_ref_table(struct libscols_table *tb); extern void scols_unref_table(struct libscols_table *tb); extern int scols_table_add_column(struct libscols_table *tb, struct libscols_column *cl); extern int scols_table_remove_column(struct libscols_table *tb, struct libscols_column *cl); extern int scols_table_remove_columns(struct libscols_table *tb); -extern struct libscols_column *scols_table_new_column(struct libscols_table *tb, const char *name, double whint, int flags); +extern struct libscols_column *scols_table_new_column(struct libscols_table *tb, const char *name, double whint); extern int scols_table_next_column(struct libscols_table *tb, struct libscols_iter *itr, struct libscols_column **cl); extern int scols_table_get_ncols(struct libscols_table *tb); extern int scols_table_get_nlines(struct libscols_table *tb); -extern int scols_table_get_flags(struct libscols_table *tb); extern struct libscols_column *scols_table_get_column(struct libscols_table *tb, size_t n); extern int scols_table_add_line(struct libscols_table *tb, struct libscols_line *ln); extern int scols_table_remove_line(struct libscols_table *tb, struct libscols_line *ln); diff --git a/libsmartcols/src/libsmartcols.sym b/libsmartcols/src/libsmartcols.sym index a02f364037..b6538a5bc8 100644 --- a/libsmartcols/src/libsmartcols.sym +++ b/libsmartcols/src/libsmartcols.sym @@ -10,11 +10,19 @@ global: scols_cell_set_color; scols_cell_set_data; scols_column_get_color; - scols_column_get_flags; scols_column_get_header; scols_column_get_whint; + scols_column_is_no_extremes; + scols_column_is_right; + scols_column_is_strict_width; + scols_column_is_tree; + scols_column_is_trunc; scols_column_set_color; - scols_column_set_flags; + scols_column_set_no_extremes; + scols_column_set_right; + scols_column_set_strict_width; + scols_column_set_tree; + scols_column_set_trunc; scols_column_set_whint; scols_copy_column; scols_copy_line; @@ -55,12 +63,19 @@ global: scols_symbols_set_vertical; scols_table_add_column; scols_table_add_line; + scols_table_colors_wanted; + scols_table_enable_colors; scols_table_get_column; - scols_table_get_flags; scols_table_get_line; scols_table_get_ncols; scols_table_get_nlines; scols_table_get_stream; + scols_table_is_ascii; + scols_table_is_export; + scols_table_is_max; + scols_table_is_no_headings; + scols_table_is_raw; + scols_table_is_tree; scols_table_new_column; scols_table_new_line; scols_table_next_column; @@ -70,8 +85,14 @@ global: scols_table_remove_columns; scols_table_remove_line; scols_table_remove_lines; + scols_table_set_ascii; + scols_table_set_export; + scols_table_set_max; + scols_table_set_no_headings; + scols_table_set_raw; scols_table_set_stream; scols_table_set_symbols; + scols_table_set_tree; scols_unref_column; scols_unref_line; scols_unref_symbols; diff --git a/libsmartcols/src/smartcolsP.h b/libsmartcols/src/smartcolsP.h index 20c262951e..be2b0cfbfb 100644 --- a/libsmartcols/src/smartcolsP.h +++ b/libsmartcols/src/smartcolsP.h @@ -24,7 +24,6 @@ # define assert(x) #endif - /* * Generic iterator */ @@ -68,12 +67,17 @@ struct libscols_column { size_t width_avg; /* average width, used to detect extreme fields */ double width_hint; /* hint (N < 1 is in percent of termwidth) */ - int flags; int is_extreme; char *color; /* default column color */ struct libscols_cell header; struct list_head cl_columns; + + unsigned int trunc :1; /* truncate fields data if necessary */ + unsigned int tree :1; /* use tree "ascii art" */ + unsigned int right :1; /* align to the right */ + unsigned int strict_width :1; /* don't reduce width if column is empty */ + unsigned int no_extremes :1; /* ignore extreme fields when count column width*/ }; /* @@ -106,12 +110,20 @@ struct libscols_table { size_t termwidth; /* terminal width */ size_t termreduce; /* extra blank space */ int is_term; /* is a tty? */ - int flags; FILE *out; /* output stream */ struct list_head tb_columns; struct list_head tb_lines; struct libscols_symbols *symbols; + + /* flags */ + unsigned int colors_wanted :1; + unsigned int raw :1; + unsigned int ascii :1; + unsigned int no_headings :1; + unsigned int export :1; + unsigned int max :1; + unsigned int tree :1; }; #define IS_ITER_FORWARD(_i) ((_i)->direction == SCOLS_ITER_FORWARD) diff --git a/libsmartcols/src/table.c b/libsmartcols/src/table.c index b142f7f79c..cb48ce4cb4 100644 --- a/libsmartcols/src/table.c +++ b/libsmartcols/src/table.c @@ -30,14 +30,13 @@ /* - * @flags: SCOLS_FL_* flags (usually SCOLS_FL_{ASCII,RAW}) * @syms: tree symbols or NULL for default * * Note that this function add a new reference to @syms * * Returns: newly allocated table */ -struct libscols_table *scols_new_table(int flags, struct libscols_symbols *syms) +struct libscols_table *scols_new_table(struct libscols_symbols *syms) { struct libscols_table *tb; @@ -45,7 +44,6 @@ struct libscols_table *scols_new_table(int flags, struct libscols_symbols *syms) if (!tb) return NULL; - tb->flags = flags; tb->refcount = 1; tb->out = stdout; @@ -131,7 +129,6 @@ int scols_table_remove_columns(struct libscols_table *tb) * @tb: table * @name: column header * @whint: column width hint (absolute width: N > 1; relative width: N < 1) - * @flags: usually SCOLS_FL_{TREE,TRUNCATE} * * This is shortcut for * @@ -147,8 +144,6 @@ int scols_table_remove_columns(struct libscols_table *tb) * the column header width * * @whint = 1..N - * @flags = SCOLS_FL_STRICTWIDTH - * : absolute width, empty colum won't be truncated * * The column is necessary to address (for example for scols_line_set_cell_data()) by * sequential number. The first defined column has the colnum = 0. For example: @@ -164,8 +159,7 @@ int scols_table_remove_columns(struct libscols_table *tb) */ struct libscols_column *scols_table_new_column(struct libscols_table *tb, const char *name, - double whint, - int flags) + double whint) { struct libscols_column *cl; struct libscols_cell *hr; @@ -185,10 +179,6 @@ struct libscols_column *scols_table_new_column(struct libscols_table *tb, goto err; scols_column_set_whint(cl, whint); - scols_column_set_flags(cl, flags); - - if (flags & SCOLS_FL_TREE) - tb->flags |= SCOLS_FL_TREE; if (scols_table_add_column(tb, cl)) /* this increments column ref-counter */ goto err; @@ -243,17 +233,6 @@ int scols_table_get_nlines(struct libscols_table *tb) return tb ? tb->nlines : -EINVAL; } -/* - * @tb: table - * - * Returns: flags integer - */ -int scols_table_get_flags(struct libscols_table *tb) -{ - assert(tb); - return tb ? tb->flags: -EINVAL; -} - int scols_table_set_stream(struct libscols_table *tb, FILE *stream) { assert(tb); @@ -459,7 +438,7 @@ struct libscols_table *scols_copy_table(struct libscols_table *tb) assert(tb); if (!tb) return NULL; - ret = scols_new_table(tb->flags, tb->symbols); + ret = scols_new_table(tb->symbols); if (!ret) return NULL; @@ -515,7 +494,7 @@ int scols_table_set_symbols(struct libscols_table *tb, if (!tb->symbols) return -ENOMEM; #if defined(HAVE_WIDECHAR) - if (!(tb->flags & SCOLS_FL_ASCII) && + if (!scols_table_is_ascii(tb) && !strcmp(nl_langinfo(CODESET), "UTF-8")) { scols_symbols_set_branch(tb->symbols, UTF_VR UTF_H); scols_symbols_set_vertical(tb->symbols, UTF_V " "); @@ -531,4 +510,228 @@ int scols_table_set_symbols(struct libscols_table *tb, return 0; } +/** + * scols_table_enable_colors: + * @tb: table + * @enable: 1 or 0 + * + * Enable/disable colors + * + * Returns: 0 on success, negative number in case of an error. + */ +int scols_table_enable_colors(struct libscols_table *tb, int enable) +{ + assert(tb); + if (!tb) + return -EINVAL; + tb->colors_wanted = enable; + return 0; +} +/** + * scols_table_set_raw: + * @tb: table + * @enable: 1 or 0 + * + * Enable/disable raw + * + * Returns: 0 on success, negative number in case of an error. + */ +int scols_table_set_raw(struct libscols_table *tb, int enable) +{ + assert(tb); + if (!tb) + return -EINVAL; + tb->raw = enable; + return 0; +} +/** + * scols_table_set_ascii: + * @tb: table + * @enable: 1 or 0 + * + * Enable/disable ascii + * + * Returns: 0 on success, negative number in case of an error. + */ +int scols_table_set_ascii(struct libscols_table *tb, int enable) +{ + assert(tb); + if (!tb) + return -EINVAL; + tb->ascii = enable; + return 0; +} +/** + * scols_table_set_no_headings: + * @tb: table + * @enable: 1 or 0 + * + * Enable/disable no_headings + * + * Returns: 0 on success, negative number in case of an error. + */ +int scols_table_set_no_headings(struct libscols_table *tb, int enable) +{ + assert(tb); + if (!tb) + return -EINVAL; + tb->no_headings = enable; + return 0; +} +/** + * scols_table_set_export: + * @tb: table + * @enable: 1 or 0 + * + * Enable/disable export + * + * Returns: 0 on success, negative number in case of an error. + */ +int scols_table_set_export(struct libscols_table *tb, int enable) +{ + assert(tb); + if (!tb) + return -EINVAL; + tb->export = enable; + return 0; +} +/** + * scols_table_set_max: + * @tb: table + * @enable: 1 or 0 + * + * Enable/disable max + * + * Returns: 0 on success, negative number in case of an error. + */ +int scols_table_set_max(struct libscols_table *tb, int enable) +{ + assert(tb); + if (!tb) + return -EINVAL; + tb->max = enable; + return 0; +} +/** + * scols_table_set_tree: + * @tb: table + * @enable: 1 or 0 + * + * Enable/disable tree + * + * Returns: 0 on success, negative number in case of an error. + */ +int scols_table_set_tree(struct libscols_table *tb, int enable) +{ + assert(tb); + if (!tb) + return -EINVAL; + tb->tree = enable; + return 0; +} +/** + * scols_table_colors_wanted: + * @tb: table + * + * Get the value of colors_wanted + * + * Returns: colors_wanted flag value, negative value in case of an error. + */ +int scols_table_colors_wanted(struct libscols_table *tb) +{ + assert(tb); + if (!tb) + return -EINVAL; + return tb->colors_wanted; +} +/** + * scols_table_is_raw: + * @tb: table + * + * Get the value of raw + * + * Returns: raw flag value, negative value in case of an error. + */ +int scols_table_is_raw(struct libscols_table *tb) +{ + assert(tb); + if (!tb) + return -EINVAL; + return tb->raw; +} +/** + * scols_table_is_ascii: + * @tb: table + * + * Get the value of ascii + * + * Returns: ascii flag value, negative value in case of an error. + */ +int scols_table_is_ascii(struct libscols_table *tb) +{ + assert(tb); + if (!tb) + return -EINVAL; + return tb->ascii; +} +/** + * scols_table_is_no_headings: + * @tb: table + * + * Get the value of no_headings + * + * Returns: no_headings flag value, negative value in case of an error. + */ +int scols_table_is_no_headings(struct libscols_table *tb) +{ + assert(tb); + if (!tb) + return -EINVAL; + return tb->no_headings; +} +/** + * scols_table_is_export: + * @tb: table + * + * Get the value of export + * + * Returns: export flag value, negative value in case of an error. + */ +int scols_table_is_export(struct libscols_table *tb) +{ + assert(tb); + if (!tb) + return -EINVAL; + return tb->export; +} +/** + * scols_table_is_max: + * @tb: table + * + * Get the value of max + * + * Returns: max flag value, negative value in case of an error. + */ +int scols_table_is_max(struct libscols_table *tb) +{ + assert(tb); + if (!tb) + return -EINVAL; + return tb->max; +} +/** + * scols_table_is_tree: + * @tb: table + * + * Get the value of tree + * + * Returns: tree flag value, negative value in case of an error. + */ +int scols_table_is_tree(struct libscols_table *tb) +{ + assert(tb); + if (!tb) + return -EINVAL; + return tb->tree; +} diff --git a/libsmartcols/src/table_print.c b/libsmartcols/src/table_print.c index 8f0297a4c9..9851be0e31 100644 --- a/libsmartcols/src/table_print.c +++ b/libsmartcols/src/table_print.c @@ -40,7 +40,7 @@ static void print_data(struct libscols_table *tb, data = ""; /* raw mode */ - if (tb->flags & SCOLS_FL_RAW) { + if (scols_table_is_raw(tb)) { fputs_nonblank(data, tb->out); if (!is_last_column(tb, cl)) fputc(' ', tb->out); @@ -48,7 +48,7 @@ static void print_data(struct libscols_table *tb, } /* NAME=value mode */ - if (tb->flags & SCOLS_FL_EXPORT) { + if (scols_table_is_export(tb)) { fprintf(tb->out, "%s=", scols_cell_get_data(&cl->header)); fputs_quoted(data, tb->out); if (!is_last_column(tb, cl)) @@ -56,7 +56,7 @@ static void print_data(struct libscols_table *tb, return; } - if (tb->is_term) { + if (tb->colors_wanted) { if (ce && !color) color = ce->color; if (ln && !color) @@ -77,11 +77,11 @@ static void print_data(struct libscols_table *tb, } width = cl->width; - if (is_last_column(tb, cl) && len < width && !(tb->flags & SCOLS_FL_MAX)) + if (is_last_column(tb, cl) && len < width && !scols_table_is_max(tb)) width = len; /* truncate data */ - if (len > width && (cl->flags & SCOLS_FL_TRUNC)) { + if (len > width && scols_column_is_trunc(cl)) { if (data) len = mbs_truncate(data, &width); if (!data || len == (size_t) -1) { @@ -90,7 +90,7 @@ static void print_data(struct libscols_table *tb, } } if (data) { - if (!(tb->flags & SCOLS_FL_RAW) && (cl->flags & SCOLS_FL_RIGHT)) { + if (!scols_table_is_raw(tb) && scols_column_is_right(cl)) { size_t xw = cl->width; if (color) fputs(color, tb->out); @@ -112,7 +112,7 @@ static void print_data(struct libscols_table *tb, fputc(' ', tb->out); /* padding */ if (!is_last_column(tb, cl)) { - if (len > width && !(cl->flags & SCOLS_FL_TRUNC)) { + if (len > width && !scols_column_is_trunc(cl)) { fputc('\n', tb->out); for (i = 0; i <= (size_t) cl->seqnum; i++) { struct libscols_column *x = scols_table_get_column(tb, i); @@ -177,7 +177,7 @@ static char *line_get_data(struct libscols_table *tb, if (!data) return NULL; - if (!(cl->flags & SCOLS_FL_TREE)) { + if (!scols_column_is_tree(cl)) { strncpy(buf, data, bufsz); buf[bufsz - 1] = '\0'; return buf; @@ -231,8 +231,8 @@ static void print_header(struct libscols_table *tb, char *buf, size_t bufsz) assert(tb); - if ((tb->flags & SCOLS_FL_NOHEADINGS) || - (tb->flags & SCOLS_FL_EXPORT) || + if (scols_table_is_no_headings(tb) || + scols_table_is_export(tb) || list_empty(&tb->tb_lines)) return; @@ -333,7 +333,7 @@ static void count_column_width(struct libscols_table *tb, if (cl->is_extreme && len > cl->width_avg * 2) continue; - else if (cl->flags & SCOLS_FL_NOEXTREMES) { + else if (scols_column_is_no_extremes(cl)) { sum += len; count++; } @@ -353,7 +353,7 @@ static void count_column_width(struct libscols_table *tb, cl->width_min = mbs_safe_width(scols_cell_get_data(&cl->header)); /* enlarge to minimal width */ - if (cl->width < cl->width_min && !(cl->flags & SCOLS_FL_STRICTWIDTH)) + if (cl->width < cl->width_min && !scols_column_is_strict_width(cl)) cl->width = cl->width_min; /* use relative size for large columns */ @@ -436,7 +436,7 @@ static void recount_widths(struct libscols_table *tb, char *buf, size_t bufsz) } } - if (width < tb->termwidth && (tb->flags & SCOLS_FL_MAX)) { + if (width < tb->termwidth && scols_table_is_max(tb)) { /* try enlarge all columns */ while (width < tb->termwidth) { scols_reset_iter(&itr, SCOLS_ITER_FORWARD); @@ -452,7 +452,7 @@ static void recount_widths(struct libscols_table *tb, char *buf, size_t bufsz) struct libscols_column *cl = list_entry( tb->tb_columns.prev, struct libscols_column, cl_columns); - if (!(cl->flags & SCOLS_FL_RIGHT) && tb->termwidth - width > 0) { + if (!scols_column_is_right(cl) && tb->termwidth - width > 0) { cl->width += tb->termwidth - width; width = tb->termwidth; } @@ -471,11 +471,11 @@ static void recount_widths(struct libscols_table *tb, char *buf, size_t bufsz) while (scols_table_next_column(tb, &itr, &cl) == 0) { if (width <= tb->termwidth) break; - if (cl->width_hint > 1 && !(cl->flags & SCOLS_FL_TRUNC)) + if (cl->width_hint > 1 && !scols_column_is_trunc(cl)) continue; /* never truncate columns with absolute sizes */ - if (cl->flags & SCOLS_FL_TREE) + if (scols_column_is_tree(cl)) continue; /* never truncate the tree */ - if (trunc_only && !(cl->flags & SCOLS_FL_TRUNC)) + if (trunc_only && !scols_column_is_trunc(cl)) continue; if (cl->width == cl->width_min) continue; @@ -570,10 +570,10 @@ int scols_print_table(struct libscols_table *tb) if (!line) return -ENOMEM; - if (!((tb->flags & SCOLS_FL_RAW) || (tb->flags & SCOLS_FL_EXPORT))) + if (!(scols_table_is_raw(tb) || scols_table_is_export(tb))) recount_widths(tb, line, line_sz); - if (tb->flags & SCOLS_FL_TREE) + if (scols_table_is_tree(tb)) print_tree(tb, line, line_sz); else print_table(tb, line, line_sz); diff --git a/libsmartcols/src/test.c b/libsmartcols/src/test.c index 372e2722d9..547a5fd003 100644 --- a/libsmartcols/src/test.c +++ b/libsmartcols/src/test.c @@ -34,22 +34,27 @@ int main(int argc, char *argv[]) { struct libscols_table *tb; struct libscols_column *cl; - int flags = 0, notree = 0, clone = 0, i, color = 0; + int notree = 0, clone = 0, i, color = 0; + + tb = scols_new_table(NULL); + if (!tb) + err(EXIT_FAILURE, "table initialization failed"); if (argc == 2 && !strcmp(argv[1], "--help")) { printf("%s [--max | --ascii | --raw | --export | --list | " "--color | --colortree | --clone | --clonetree]\n", program_invocation_short_name); + scols_unref_table(tb); return EXIT_SUCCESS; } else if (argc == 2 && !strcmp(argv[1], "--max")) { - flags |= SCOLS_FL_MAX; + scols_table_set_max(tb, 1); } else if (argc == 2 && !strcmp(argv[1], "--ascii")) { - flags |= SCOLS_FL_ASCII; + scols_table_set_ascii(tb, 1); } else if (argc == 2 && !strcmp(argv[1], "--raw")) { - flags |= SCOLS_FL_RAW; + scols_table_set_raw(tb, 1); notree = 1; } else if (argc == 2 && !strcmp(argv[1], "--export")) { - flags |= SCOLS_FL_EXPORT; + scols_table_set_export(tb, 1); notree = 1; } else if (argc == 2 && !strcmp(argv[1], "--list")) { notree = 1; @@ -71,23 +76,23 @@ int main(int argc, char *argv[]) bindtextdomain(PACKAGE, LOCALEDIR); textdomain(PACKAGE); - tb = scols_new_table(flags, NULL); - if (!tb) - err(EXIT_FAILURE, "table initialization failed"); + cl = scols_table_new_column(tb, "NAME", 0.3); + scols_table_set_tree(tb, !notree); + scols_table_enable_colors(tb, color); - cl = scols_table_new_column(tb, "NAME", 0.3, notree ? 0 : SCOLS_FL_TREE); if (color) scols_column_set_color(cl, UL_COLOR_RED); - cl = scols_table_new_column(tb, "FOO", 0.3, SCOLS_FL_TRUNC); + cl = scols_table_new_column(tb, "FOO", 0.3); + scols_column_set_trunc(cl, 1); if (color) { struct libscols_cell *h = scols_column_get_header(cl); scols_column_set_color(cl, UL_COLOR_BOLD_GREEN); scols_cell_set_color(h, UL_COLOR_GREEN); } - scols_table_new_column(tb, "BAR", 0.3, 0); - scols_table_new_column(tb, "PATH", 0.3, 0); + scols_table_new_column(tb, "BAR", 0.3); + scols_table_new_column(tb, "PATH", 0.3); for (i = 0; i < 2; i++) { struct libscols_line *ln = scols_table_new_line(tb, NULL); -- 2.47.3