]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
libsmartcols: add functions to control terminal usage
authorKarel Zak <kzak@redhat.com>
Tue, 13 Sep 2016 12:06:01 +0000 (14:06 +0200)
committerKarel Zak <kzak@redhat.com>
Tue, 13 Sep 2016 12:06:01 +0000 (14:06 +0200)
Signed-off-by: Karel Zak <kzak@redhat.com>
libsmartcols/docs/libsmartcols-sections.txt
libsmartcols/src/libsmartcols.h.in
libsmartcols/src/libsmartcols.sym
libsmartcols/src/smartcolsP.h
libsmartcols/src/table.c
libsmartcols/src/table_print.c

index f2bbbf38fcd0777f81b65fe26e7f3c3d4796a8db..324f6e15fb43bad3d922bc103cc1ad2c2630a26f 100644 (file)
@@ -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
 </SECTION>
 
index 1165e0c2def25a6b9738fdef6bd43809b4d43100..41f514d711d90b4b098c8c4eb2cc413e8da65b73 100644 (file)
@@ -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);
index 7b2cb88bb4250ebcef72919dbc45d570258f41c8..d599a5030daa5d99f9ceed4be8423abe6d62a2d5 100644 (file)
@@ -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;
index 01b132752ce83a2ccf867530934f252c6cedfb7f..e4aeec46ac3b71ddddf946096418caf27e520411 100644 (file)
@@ -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 */
index 10320f0be0ec9469a33ae7fafc62034499369162..32c991e49085296e745b5a6a4db5ed17a5badfdc 100644 (file)
@@ -25,6 +25,7 @@
 #include <ctype.h>
 
 #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;
+}
index 7d5fe0ffa730a49d81e06cfee11d90db2d4cef17..ff8a23686607360622cef5210572e040ebfecb5c 100644 (file)
@@ -23,7 +23,6 @@
 #include <ctype.h>
 
 #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;