From 19055a25ed2a28d005460ee41627d2595ae7ad34 Mon Sep 17 00:00:00 2001 From: Karel Zak Date: Tue, 13 Sep 2016 14:06:01 +0200 Subject: [PATCH] libsmartcols: add functions to control terminal usage Signed-off-by: Karel Zak --- libsmartcols/docs/libsmartcols-sections.txt | 8 ++- libsmartcols/src/libsmartcols.h.in | 14 +++++ libsmartcols/src/libsmartcols.sym | 4 ++ libsmartcols/src/smartcolsP.h | 1 + libsmartcols/src/table.c | 59 +++++++++++++++++++++ libsmartcols/src/table_print.c | 16 +++--- 6 files changed, 94 insertions(+), 8 deletions(-) diff --git a/libsmartcols/docs/libsmartcols-sections.txt b/libsmartcols/docs/libsmartcols-sections.txt index f2bbbf38f..324f6e15f 100644 --- a/libsmartcols/docs/libsmartcols-sections.txt +++ b/libsmartcols/docs/libsmartcols-sections.txt @@ -96,6 +96,7 @@ libscols_table scols_copy_table scols_new_table scols_ref_table +scols_sort_table scols_table_add_column scols_table_add_line scols_table_colors_wanted @@ -115,6 +116,9 @@ scols_table_get_line_separator scols_table_get_ncols scols_table_get_nlines scols_table_get_stream +scols_table_get_termforce +scols_table_get_termwidth +scols_table_get_title scols_table_is_ascii scols_table_is_empty scols_table_is_export @@ -137,8 +141,8 @@ scols_table_set_line_separator scols_table_set_name scols_table_set_stream scols_table_set_symbols -scols_table_get_title -scols_sort_table +scols_table_set_termforce +scols_table_set_termwidth scols_unref_table diff --git a/libsmartcols/src/libsmartcols.h.in b/libsmartcols/src/libsmartcols.h.in index 1165e0c2d..41f514d71 100644 --- a/libsmartcols/src/libsmartcols.h.in +++ b/libsmartcols/src/libsmartcols.h.in @@ -246,6 +246,20 @@ extern int scols_table_reduce_termwidth(struct libscols_table *tb, size_t reduce extern int scols_sort_table(struct libscols_table *tb, struct libscols_column *cl); +/* + * + */ +enum { + SCOLS_TERMFORCE_AUTO = 0, + SCOLS_TERMFORCE_NEVER, + SCOLS_TERMFORCE_ALWAYS +}; +extern int scols_table_set_termforce(struct libscols_table *tb, int force); +extern int scols_table_get_termforce(struct libscols_table *tb); +extern int scols_table_set_termwidth(struct libscols_table *tb, size_t width); +extern size_t scols_table_get_termwidth(struct libscols_table *tb); + + /* table_print.c */ extern int scols_print_table(struct libscols_table *tb); extern int scols_print_table_to_string(struct libscols_table *tb, char **data); diff --git a/libsmartcols/src/libsmartcols.sym b/libsmartcols/src/libsmartcols.sym index 7b2cb88bb..d599a5030 100644 --- a/libsmartcols/src/libsmartcols.sym +++ b/libsmartcols/src/libsmartcols.sym @@ -141,4 +141,8 @@ SMARTCOLS_2.29 { global: scols_column_is_wrapnl; scols_symbols_set_cell_padding; + scols_table_get_termforce; + scols_table_get_termwidth; + scols_table_set_termforce; + scols_table_set_termwidth; } SMARTCOLS_2.28; diff --git a/libsmartcols/src/smartcolsP.h b/libsmartcols/src/smartcolsP.h index 01b132752..e4aeec46a 100644 --- a/libsmartcols/src/smartcolsP.h +++ b/libsmartcols/src/smartcolsP.h @@ -139,6 +139,7 @@ struct libscols_table { size_t nlines; /* number of lines */ size_t termwidth; /* terminal width */ size_t termreduce; /* extra blank space */ + int termforce; /* SCOLS_TERMFORCE_* */ FILE *out; /* output stream */ char *colsep; /* column separator */ diff --git a/libsmartcols/src/table.c b/libsmartcols/src/table.c index 10320f0be..32c991e49 100644 --- a/libsmartcols/src/table.c +++ b/libsmartcols/src/table.c @@ -25,6 +25,7 @@ #include #include "nls.h" +#include "ttyutils.h" #include "smartcolsP.h" #ifdef HAVE_WIDECHAR @@ -1104,3 +1105,61 @@ int scols_sort_table(struct libscols_table *tb, struct libscols_column *cl) list_sort(&tb->tb_lines, cells_cmp_wrapper, cl); return 0; } + +/** + * scols_table_set_termforce: + * @tb: table + * @force: SCOLS_TERMFORCE_{NEVER,ALWAYS,AUTO} + * + * Forces library to use stdout as terminal, non-terminal or use automatical + * detection (default). + * + * Returns: 0, a negative value in case of an error. + */ +int scols_table_set_termforce(struct libscols_table *tb, int force) +{ + if (!tb) + return -EINVAL; + tb->termforce = force; + return 0; +} + +/** + * scols_table_get_termforce: + * @tb: table + * + * Returns: SCOLS_TERMFORCE_{NEVER,ALWAYS,AUTO} or a negative value in case of an error. + */ +int scols_table_get_termforce(struct libscols_table *tb) +{ + return tb->termforce; +} + +/** + * scols_table_set_termwidth + * @tb: table + * @width: terminal width + * + * The library automatically detects terminal width or defaults to 80 chars if + * detections is unsuccessful. This function override this behaviour. + * + * Returns: 0, a negative value in case of an error. + */ +int scols_table_set_termwidth(struct libscols_table *tb, size_t width) +{ + tb->termwidth = width; + return 0; +} + +/** + * scols_table_get_termwidth + * @tb: table + * + * Returns: terminal width or a negative value in case of an error. + */ +size_t scols_table_get_termwidth(struct libscols_table *tb) +{ + if (tb->termwidth == 0) + tb->termwidth = get_terminal_width(80); + return tb->termwidth; +} diff --git a/libsmartcols/src/table_print.c b/libsmartcols/src/table_print.c index 7d5fe0ffa..ff8a23686 100644 --- a/libsmartcols/src/table_print.c +++ b/libsmartcols/src/table_print.c @@ -23,7 +23,6 @@ #include #include "mbsalign.h" -#include "ttyutils.h" #include "carefulputc.h" #include "smartcolsP.h" @@ -1334,13 +1333,18 @@ static int initialize_printing(struct libscols_table *tb, struct libscols_buffer scols_table_set_symbols(tb, NULL); /* use default */ if (tb->format == SCOLS_FMT_HUMAN) - tb->is_term = isatty(STDOUT_FILENO) ? 1 : 0; + tb->is_term = tb->termforce == SCOLS_TERMFORCE_NEVER ? 0 : + tb->termforce == SCOLS_TERMFORCE_ALWAYS ? 1 : + isatty(STDOUT_FILENO); if (tb->is_term) { - tb->termwidth = get_terminal_width(80); - if (tb->termreduce > 0 && tb->termreduce < tb->termwidth) - tb->termwidth -= tb->termreduce; - bufsz = tb->termwidth; + size_t width = (size_t) scols_table_get_termwidth(tb); + + if (tb->termreduce > 0 && tb->termreduce < width) { + width -= tb->termreduce; + scols_table_set_termwidth(tb, width); + } + bufsz = width; } else bufsz = BUFSIZ;