summaryrefslogtreecommitdiff
path: root/libsmartcols
diff options
context:
space:
mode:
authorKarel Zak <kzak@redhat.com>2014-04-03 16:09:57 +0200
committerKarel Zak <kzak@redhat.com>2014-04-03 16:09:57 +0200
commit0925a9dd6cd65feb8cb2d4d66551363f4af161e9 (patch)
tree9981e45aee1274f959a9b32f3cbdffabc8a45d8c /libsmartcols
parent7fa74cb098d57a6d770e19ba361c01382c9e924c (diff)
downloadutil-linux-0925a9dd6cd65feb8cb2d4d66551363f4af161e9.tar.gz
libsmartcols: clean up flags usage
* rename flags functions to scols_table_enable_* * rename *_no_foo() functions to _nofoo() * output formats are mutually exclusive, so don't use flags there * don't assume symbols in scols_new_table(), use scols_table_set_symbols() Signed-off-by: Karel Zak <kzak@redhat.com>
Diffstat (limited to 'libsmartcols')
-rw-r--r--libsmartcols/docs/libsmartcols-sections.txt15
-rw-r--r--libsmartcols/src/column.c4
-rw-r--r--libsmartcols/src/libsmartcols.h.in23
-rw-r--r--libsmartcols/src/libsmartcols.sym17
-rw-r--r--libsmartcols/src/smartcolsP.h22
-rw-r--r--libsmartcols/src/table.c199
-rw-r--r--libsmartcols/src/table_print.c10
7 files changed, 139 insertions, 151 deletions
diff --git a/libsmartcols/docs/libsmartcols-sections.txt b/libsmartcols/docs/libsmartcols-sections.txt
index 85a9feb14..dc2203702 100644
--- a/libsmartcols/docs/libsmartcols-sections.txt
+++ b/libsmartcols/docs/libsmartcols-sections.txt
@@ -17,7 +17,7 @@ scols_column_get_color
scols_column_get_flags
scols_column_get_header
scols_column_get_whint
-scols_column_is_no_extremes
+scols_column_is_noextremes
scols_column_is_right
scols_column_is_strict_width
scols_column_is_tree
@@ -86,6 +86,10 @@ scols_table_add_column
scols_table_add_line
scols_table_colors_wanted
scols_table_enable_colors
+scols_table_enable_export
+scols_table_enable_maxout
+scols_table_enable_noheadings
+scols_table_enable_raw
scols_table_get_column
scols_table_get_line
scols_table_get_ncols
@@ -94,8 +98,8 @@ scols_table_get_stream
scols_table_is_ascii
scols_table_is_empty
scols_table_is_export
-scols_table_is_max
-scols_table_is_no_headings
+scols_table_is_maxout
+scols_table_is_noheadings
scols_table_is_raw
scols_table_is_tree
scols_table_new_column
@@ -108,13 +112,8 @@ 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_table
</SECTION>
diff --git a/libsmartcols/src/column.c b/libsmartcols/src/column.c
index b01119d28..91e3655cb 100644
--- a/libsmartcols/src/column.c
+++ b/libsmartcols/src/column.c
@@ -299,14 +299,14 @@ int scols_column_is_strict_width(struct libscols_column *cl)
return cl->flags & SCOLS_FL_STRICTWIDTH;
}
/**
- * scols_column_is_no_extremes:
+ * scols_column_is_noextremes:
* @cl: a pointer to a struct libscols_column instance
*
* Gets the value of @cl's flag no_extremes.
*
* Returns: no_extremes flag value, negative value in case of an error.
*/
-int scols_column_is_no_extremes(struct libscols_column *cl)
+int scols_column_is_noextremes(struct libscols_column *cl)
{
assert(cl);
if (!cl)
diff --git a/libsmartcols/src/libsmartcols.h.in b/libsmartcols/src/libsmartcols.h.in
index b77b11e37..98d3e4c28 100644
--- a/libsmartcols/src/libsmartcols.h.in
+++ b/libsmartcols/src/libsmartcols.h.in
@@ -109,7 +109,7 @@ extern int scols_column_is_tree(struct libscols_column *cl);
extern int scols_column_is_trunc(struct libscols_column *cl);
extern int scols_column_is_right(struct libscols_column *cl);
extern int scols_column_is_strict_width(struct libscols_column *cl);
-extern int scols_column_is_no_extremes(struct libscols_column *cl);
+extern int scols_column_is_noextremes(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);
@@ -149,24 +149,23 @@ extern struct libscols_line *scols_copy_line(struct libscols_line *ln);
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_noheadings(struct libscols_table *tb);
extern int scols_table_is_empty(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_maxout(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 int scols_table_enable_raw(struct libscols_table *tb, int enable);
+extern int scols_table_enable_ascii(struct libscols_table *tb, int enable);
+extern int scols_table_enable_noheadings(struct libscols_table *tb, int enable);
+extern int scols_table_enable_export(struct libscols_table *tb, int enable);
+extern int scols_table_enable_maxout(struct libscols_table *tb, int enable);
+
+extern struct libscols_table *scols_new_table(void);
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, int flags);
+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);
diff --git a/libsmartcols/src/libsmartcols.sym b/libsmartcols/src/libsmartcols.sym
index 19c260198..40cc12c86 100644
--- a/libsmartcols/src/libsmartcols.sym
+++ b/libsmartcols/src/libsmartcols.sym
@@ -13,7 +13,7 @@ global:
scols_column_get_flags;
scols_column_get_header;
scols_column_get_whint;
- scols_column_is_no_extremes;
+ scols_column_is_noextremes;
scols_column_is_right;
scols_column_is_strict_width;
scols_column_is_tree;
@@ -61,7 +61,12 @@ global:
scols_table_add_column;
scols_table_add_line;
scols_table_colors_wanted;
+ scols_table_enable_ascii;
scols_table_enable_colors;
+ scols_table_enable_export;
+ scols_table_enable_maxout;
+ scols_table_enable_noheadings;
+ scols_table_enable_raw;
scols_table_get_column;
scols_table_get_line;
scols_table_get_ncols;
@@ -70,8 +75,8 @@ global:
scols_table_is_ascii;
scols_table_is_empty;
scols_table_is_export;
- scols_table_is_max;
- scols_table_is_no_headings;
+ scols_table_is_maxout;
+ scols_table_is_noheadings;
scols_table_is_raw;
scols_table_is_tree;
scols_table_new_column;
@@ -83,14 +88,8 @@ 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 585b5e196..431b91b8b 100644
--- a/libsmartcols/src/smartcolsP.h
+++ b/libsmartcols/src/smartcolsP.h
@@ -93,30 +93,36 @@ struct libscols_line {
struct libscols_line *parent;
};
+enum {
+ SCOLS_FMT_HUMAN = 0, /* default, human readable */
+ SCOLS_FMT_RAW, /* space separated */
+ SCOLS_FMT_EXPORT /* COLNAME="data" ... */
+};
+
/*
* The table
*/
struct libscols_table {
int refcount;
size_t ncols; /* number of columns */
+ size_t ntreecols; /* number of columns with SCOLS_FL_TREE */
size_t nlines; /* number of lines */
size_t termwidth; /* terminal width */
size_t termreduce; /* extra blank space */
- int is_term; /* is a tty? */
FILE *out; /* output stream */
struct list_head tb_columns;
struct list_head tb_lines;
struct libscols_symbols *symbols;
+ int format; /* SCOLS_FMT_* */
+
/* 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;
+ unsigned int ascii :1, /* don't use unicode */
+ colors_wanted :1, /* enable colors */
+ is_term :1, /* isatty() */
+ maxout :1, /* maximalize output */
+ no_headings :1; /* don't print header */
};
#define IS_ITER_FORWARD(_i) ((_i)->direction == SCOLS_ITER_FORWARD)
diff --git a/libsmartcols/src/table.c b/libsmartcols/src/table.c
index dd26410fe..7b08bc3d6 100644
--- a/libsmartcols/src/table.c
+++ b/libsmartcols/src/table.c
@@ -40,13 +40,10 @@
/**
* scols_new_table:
- * @syms: tree symbols or NULL for default
- *
- * Note that this function add a new reference to @syms.
*
* Returns: A newly allocated table.
*/
-struct libscols_table *scols_new_table(struct libscols_symbols *syms)
+struct libscols_table *scols_new_table(void)
{
struct libscols_table *tb;
@@ -60,13 +57,7 @@ struct libscols_table *scols_new_table(struct libscols_symbols *syms)
INIT_LIST_HEAD(&tb->tb_lines);
INIT_LIST_HEAD(&tb->tb_columns);
- if (syms && scols_table_set_symbols(tb, syms) != 0)
- goto err;
-
return tb;
-err:
- scols_unref_table(tb);
- return NULL;
}
/**
@@ -107,7 +98,7 @@ void scols_unref_table(struct libscols_table *tb)
*
* Returns: 0, a negative number in case of an error.
*/
-int scols_table_add_column(struct libscols_table *tb, struct libscols_column *cl, int flags)
+int scols_table_add_column(struct libscols_table *tb, struct libscols_column *cl)
{
assert(tb);
assert(cl);
@@ -115,8 +106,8 @@ int scols_table_add_column(struct libscols_table *tb, struct libscols_column *cl
if (!tb || !cl || !list_empty(&tb->tb_lines))
return -EINVAL;
- if (flags & SCOLS_FL_TREE)
- scols_table_set_tree(tb, 1);
+ if (cl->flags & SCOLS_FL_TREE)
+ tb->ntreecols++;
list_add_tail(&cl->cl_columns, &tb->tb_columns);
cl->seqnum = tb->ncols++;
@@ -149,6 +140,9 @@ int scols_table_remove_column(struct libscols_table *tb,
if (!tb || !cl || !list_empty(&tb->tb_lines))
return -EINVAL;
+ if (cl->flags & SCOLS_FL_TREE)
+ tb->ntreecols--;
+
list_del_init(&cl->cl_columns);
tb->ncols--;
scols_unref_column(cl);
@@ -238,7 +232,7 @@ struct libscols_column *scols_table_new_column(struct libscols_table *tb,
scols_column_set_whint(cl, whint);
scols_column_set_flags(cl, flags);
- if (scols_table_add_column(tb, cl, flags)) /* this increments column ref-counter */
+ if (scols_table_add_column(tb, cl)) /* this increments column ref-counter */
goto err;
scols_unref_column(cl);
@@ -581,17 +575,20 @@ struct libscols_table *scols_copy_table(struct libscols_table *tb)
assert(tb);
if (!tb)
return NULL;
- ret = scols_new_table(tb->symbols);
+ ret = scols_new_table();
if (!ret)
return NULL;
+ if (tb->symbols)
+ scols_table_set_symbols(ret, tb->symbols);
+
/* columns */
scols_reset_iter(&itr, SCOLS_ITER_FORWARD);
while (scols_table_next_column(tb, &itr, &cl) == 0) {
cl = scols_copy_column(cl);
if (!cl)
goto err;
- if (scols_table_add_column(ret, cl, tb->tree ? SCOLS_FL_TREE : 0))
+ if (scols_table_add_column(ret, cl))
goto err;
scols_unref_column(cl);
}
@@ -622,7 +619,12 @@ err:
/**
* scols_table_set_symbols:
* @tb: table
- * @sy: symbols
+ * @sy: symbols or NULL
+ *
+ * Add a reference to @sy from the table. The symbols are used by library to
+ * draw tree output. If no symbols are specified then library checks the
+ * current environment to select ASCII or UTF8 symbols. This default behavior
+ * could be controlled by scols_table_enable_ascii().
*
* Returns: 0, a negative value in case of an error.
*/
@@ -665,7 +667,7 @@ int scols_table_set_symbols(struct libscols_table *tb,
* @tb: table
* @enable: 1 or 0
*
- * Enable/disable colors
+ * Enable/disable colors.
*
* Returns: 0 on success, negative number in case of an error.
*/
@@ -678,221 +680,204 @@ int scols_table_enable_colors(struct libscols_table *tb, int enable)
return 0;
}
/**
- * scols_table_set_raw:
+ * scols_table_enable_raw:
* @tb: table
* @enable: 1 or 0
*
- * Enable/disable raw
+ * Enable/disable raw output format. The parsable output formats
+ * (export and raw) are mutually exclusive.
*
* Returns: 0 on success, negative number in case of an error.
*/
-int scols_table_set_raw(struct libscols_table *tb, int enable)
+int scols_table_enable_raw(struct libscols_table *tb, int enable)
{
assert(tb);
if (!tb)
return -EINVAL;
- tb->raw = enable;
+
+ if (enable)
+ tb->format = SCOLS_FMT_RAW;
+ else if (tb->format == SCOLS_FMT_RAW)
+ tb->format = 0;
return 0;
}
+
/**
- * scols_table_set_ascii:
+ * scols_table_enable_export:
* @tb: table
* @enable: 1 or 0
*
- * Enable/disable ascii
+ * Enable/disable export output format (COLUMNAME="value" ...).
+ * The parsable output formats (export and raw) are mutually exclusive.
*
* Returns: 0 on success, negative number in case of an error.
*/
-int scols_table_set_ascii(struct libscols_table *tb, int enable)
+int scols_table_enable_export(struct libscols_table *tb, int enable)
{
assert(tb);
if (!tb)
return -EINVAL;
- tb->ascii = enable;
+ if (enable)
+ tb->format = SCOLS_FMT_EXPORT;
+ else if (tb->format == SCOLS_FMT_EXPORT)
+ tb->format = 0;
return 0;
}
+
/**
- * scols_table_set_no_headings:
+ * scols_table_enable_ascii:
* @tb: table
* @enable: 1 or 0
*
- * Enable/disable no_headings
+ * The ASCII-only output is relevant for tree-like outputs. The library
+ * checks if the current environment is UTF8 compatible by default. This
+ * function overrides this check and force the library to use ASCII chars
+ * for the tree.
*
- * 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
+ * If a custom libcols_symbols are specified (see scols_table_set_symbols()
+ * then ASCII flag setting is ignored.
*
* Returns: 0 on success, negative number in case of an error.
*/
-int scols_table_set_export(struct libscols_table *tb, int enable)
+int scols_table_enable_ascii(struct libscols_table *tb, int enable)
{
assert(tb);
if (!tb)
return -EINVAL;
- tb->export = enable;
+ tb->ascii = enable ? 1 : 0;
return 0;
}
+
/**
- * scols_table_set_max:
+ * scols_table_enable_noheadings:
* @tb: table
* @enable: 1 or 0
*
- * Enable/disable max
+ * Enable/disable header line.
*
* Returns: 0 on success, negative number in case of an error.
*/
-int scols_table_set_max(struct libscols_table *tb, int enable)
+int scols_table_enable_noheadings(struct libscols_table *tb, int enable)
{
assert(tb);
if (!tb)
return -EINVAL;
- tb->max = enable;
+ tb->no_headings = enable ? 1 : 0;
return 0;
}
/**
- * scols_table_set_tree:
+ * scols_table_enable_maxout:
* @tb: table
* @enable: 1 or 0
*
- * Enable/disable tree
+ * The extra space after last column is ignored by default. The output
+ * maximization use the extra space for all columns.
*
* Returns: 0 on success, negative number in case of an error.
*/
-int scols_table_set_tree(struct libscols_table *tb, int enable)
+int scols_table_enable_maxout(struct libscols_table *tb, int enable)
{
assert(tb);
if (!tb)
return -EINVAL;
- tb->tree = enable;
+ tb->maxout = enable ? 1 : 0;
return 0;
}
+
/**
* scols_table_colors_wanted:
* @tb: table
*
- * Gets the value of the colors_wanted flag.
- *
- * Returns: colors_wanted flag value, negative value in case of an error.
+ * Returns: 1 if colors are enabled.
*/
int scols_table_colors_wanted(struct libscols_table *tb)
{
assert(tb);
- if (!tb)
- return -EINVAL;
- return tb->colors_wanted;
+ return tb && tb->colors_wanted;
}
/**
* scols_table_is_empty:
* @tb: table
*
- * Returns: 1 if empty, 0 if not (or in case of an error).
+ * Returns: 1 if the table is empty.
*/
int scols_table_is_empty(struct libscols_table *tb)
{
+ assert(tb);
return !tb || !tb->nlines;
}
+
/**
- * scols_table_is_raw:
+ * scols_table_is_ascii:
* @tb: table
*
- * Gets the value of the raw flag.
- *
- * Returns: raw flag value, negative value in case of an error.
+ * Returns: 1 if ASCII tree is enabled.
*/
-int scols_table_is_raw(struct libscols_table *tb)
+int scols_table_is_ascii(struct libscols_table *tb)
{
assert(tb);
- if (!tb)
- return -EINVAL;
- return tb->raw;
+ return tb && tb->ascii;
}
+
/**
- * scols_table_is_ascii:
+ * scols_table_is_noheadings:
* @tb: table
*
- * Gets the value of the ascii flag.
- *
- * Returns: ascii flag value, negative value in case of an error.
+ * Returns: 1 if header output is disabled.
*/
-int scols_table_is_ascii(struct libscols_table *tb)
+int scols_table_is_noheadings(struct libscols_table *tb)
{
assert(tb);
- if (!tb)
- return -EINVAL;
- return tb->ascii;
+ return tb && tb->no_headings;
}
+
/**
- * scols_table_is_no_headings:
+ * scols_table_is_export:
* @tb: table
*
- * Gets the value of the no_headings flag.
- *
- * Returns: no_headings flag value, negative value in case of an error.
+ * Returns: 1 if export output format is enabled.
*/
-int scols_table_is_no_headings(struct libscols_table *tb)
+int scols_table_is_export(struct libscols_table *tb)
{
assert(tb);
- if (!tb)
- return -EINVAL;
- return tb->no_headings;
+ return tb && tb->format == SCOLS_FMT_EXPORT;
}
+
/**
- * scols_table_is_export:
+ * scols_table_is_raw:
* @tb: table
*
- * Gets the value of the export flag.
- *
- * Returns: export flag value, negative value in case of an error.
+ * Returns: 1 if raw output format is enabled.
*/
-int scols_table_is_export(struct libscols_table *tb)
+int scols_table_is_raw(struct libscols_table *tb)
{
assert(tb);
- if (!tb)
- return -EINVAL;
- return tb->export;
+ return tb && tb->format == SCOLS_FMT_RAW;
}
+
+
/**
- * scols_table_is_max:
+ * scols_table_is_maxout
* @tb: table
*
- * Gets the value of the max flag.
- *
- * Returns: max flag value, negative value in case of an error.
+ * Returns: 1 if output maximization is enabled, negative value in case of an error.
*/
-int scols_table_is_max(struct libscols_table *tb)
+int scols_table_is_maxout(struct libscols_table *tb)
{
assert(tb);
- if (!tb)
- return -EINVAL;
- return tb->max;
+ return tb && tb->maxout;
}
+
/**
* scols_table_is_tree:
* @tb: table
*
- * Gets the value of the tree flag.
- *
- * Returns: tree flag value, negative value in case of an error.
+ * Returns: returns 1 tree-like output is expected.
*/
int scols_table_is_tree(struct libscols_table *tb)
{
assert(tb);
- if (!tb)
- return -EINVAL;
- return tb->tree;
+ return tb && tb->ntreecols > 0;
}
diff --git a/libsmartcols/src/table_print.c b/libsmartcols/src/table_print.c
index f3cdf70c7..6b846d49d 100644
--- a/libsmartcols/src/table_print.c
+++ b/libsmartcols/src/table_print.c
@@ -77,7 +77,7 @@ static void print_data(struct libscols_table *tb,
}
width = cl->width;
- if (is_last_column(tb, cl) && len < width && !scols_table_is_max(tb))
+ if (is_last_column(tb, cl) && len < width && !scols_table_is_maxout(tb))
width = len;
/* truncate data */
@@ -231,7 +231,7 @@ static void print_header(struct libscols_table *tb, char *buf, size_t bufsz)
assert(tb);
- if (scols_table_is_no_headings(tb) ||
+ if (scols_table_is_noheadings(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 (scols_column_is_no_extremes(cl)) {
+ else if (scols_column_is_noextremes(cl)) {
sum += len;
count++;
}
@@ -436,7 +436,7 @@ static void recount_widths(struct libscols_table *tb, char *buf, size_t bufsz)
}
}
- if (width < tb->termwidth && scols_table_is_max(tb)) {
+ if (width < tb->termwidth && scols_table_is_maxout(tb)) {
/* try enlarge all columns */
while (width < tb->termwidth) {
scols_reset_iter(&itr, SCOLS_ITER_FORWARD);
@@ -556,7 +556,7 @@ int scols_print_table(struct libscols_table *tb)
if (!tb->symbols)
scols_table_set_symbols(tb, NULL); /* use default */
- tb->is_term = isatty(STDOUT_FILENO);
+ tb->is_term = isatty(STDOUT_FILENO) ? 1 : 0;
tb->termwidth = tb->is_term ? get_terminal_width() : 0;
if (tb->termwidth <= 0)
tb->termwidth = 80;