]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
libsmartcols: support continuous printing
authorKarel Zak <kzak@redhat.com>
Fri, 19 Feb 2016 15:43:28 +0000 (16:43 +0100)
committerKarel Zak <kzak@redhat.com>
Fri, 19 Feb 2016 15:43:28 +0000 (16:43 +0100)
This patch allows to disable line-breaks. This feature is usable when
you want to re-print the same line more than once -- move terminal
cursor to the begin of the line and print again and again (aka
progress bar).

Signed-off-by: Karel Zak <kzak@redhat.com>
libsmartcols/docs/libsmartcols-sections.txt
libsmartcols/src/cell.c
libsmartcols/src/libsmartcols.h.in
libsmartcols/src/libsmartcols.sym
libsmartcols/src/smartcolsP.h
libsmartcols/src/table.c
libsmartcols/src/table_print.c

index 4ac1291418975e22783549c0dc502dd0133b8618..c9bbec45b4db471e85ca73a20c681933f8f8c079 100644 (file)
@@ -103,6 +103,7 @@ scols_table_enable_export
 scols_table_enable_json
 scols_table_enable_maxout
 scols_table_enable_noheadings
+scols_table_enable_nolinesep
 scols_table_enable_nowrap
 scols_table_enable_raw
 scols_table_get_column
index c7aab353e95c395a04c703f9f0da956b7727aadb..a9e69c0b8b853cc0b6d973336fe6aa058b1be483 100644 (file)
@@ -56,7 +56,7 @@ int scols_reset_cell(struct libscols_cell *ce)
  * @ce: a pointer to a struct libscols_cell instance
  * @str: data (used for scols_print_table())
  *
- * Stores a copy of the @str in @ce.
+ * Stores a copy of the @str in @ce, the old data are deallocated by free().
  *
  * Returns: 0, a negative value in case of an error.
  */
index 5f87d965f055fa22e81bb670b5578641e06a2152..664822e77e1a22879a301cc77533a417f0b35fb2 100644 (file)
@@ -210,6 +210,7 @@ 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 int scols_table_enable_nowrap(struct libscols_table *tb, int enable);
+extern int scols_table_enable_nolinesep(struct libscols_table *tb, int enable);
 
 extern int scols_table_set_column_separator(struct libscols_table *tb, const char *sep);
 extern int scols_table_set_line_separator(struct libscols_table *tb, const char *sep);
index 250c06d6dd3644679057b6c94f01c9cc653744a7..8f64c09c9f6be2535e8606aed269fc308c672c68 100644 (file)
@@ -133,4 +133,5 @@ global:
        scols_cell_get_flags;
        scols_cell_set_flags;
        scols_table_print_range;
+       scols_table_enable_nolinesep;
 } SMARTCOLS_2.27;
index 5add218498eeb0948ed6b188dd10c05865f3a232..377ab804518cf71123deef5f9b2ba0b816630361 100644 (file)
@@ -156,7 +156,9 @@ struct libscols_table {
                        colors_wanted   :1,     /* enable colors */
                        is_term         :1,     /* isatty() */
                        maxout          :1,     /* maximalize output */
+                       header_printed  :1,     /* header already printed */
                        no_headings     :1,     /* don't print header */
+                       no_linesep      :1,     /* don't print line separator */
                        no_wrap         :1;     /* never wrap lines */
 };
 
index fdbc7cd3f091cb953198562e9eeec55f70b8fd7b..cc19e4ab20f17f9d2fcf420c8f9e63b6086a796c 100644 (file)
@@ -707,6 +707,27 @@ int scols_table_set_symbols(struct libscols_table *tb,
        return 0;
 }
 
+/**
+ * scols_table_enable_nolinesep
+ * @tb: table
+ * @enable: 1 or 0
+ *
+ * Enable/disable line separator printing. This is usefull if you want to
+ * re-printing the same line more than once (e.g. progress bar). Don't use it
+ * if you're not sure.
+ *
+ * Returns: 0 on success, negative number in case of an error.
+ */
+int scols_table_enable_nolinesep(struct libscols_table *tb, int enable)
+{
+       if (!tb)
+               return -EINVAL;
+
+       DBG(TAB, ul_debugobj(tb, "nolinesep: %s", enable ? "ENABLE" : "DISABLE"));
+       tb->no_linesep = enable;
+       return 0;
+}
+
 /**
  * scols_table_enable_colors:
  * @tb: table
index d8965726e74fc0cbda157b218e010c1c0650e5f4..75e828eddccaad5d2667ed5af61705baa5b80440 100644 (file)
@@ -645,7 +645,8 @@ static void fput_line_close(struct libscols_table *tb, int last)
                        fput_indent(tb);
                fputs(last ? "}" : "},", tb->out);
        }
-       fputs(linesep(tb), tb->out);
+       if (!tb->no_linesep)
+               fputs(linesep(tb), tb->out);
        tb->indent_last_sep = 1;
 }
 
@@ -778,7 +779,8 @@ static int print_header(struct libscols_table *tb, struct libscols_buffer *buf)
 
        assert(tb);
 
-       if (scols_table_is_noheadings(tb) ||
+       if (tb->header_printed == 1 ||
+           scols_table_is_noheadings(tb) ||
            scols_table_is_export(tb) ||
            scols_table_is_json(tb) ||
            list_empty(&tb->tb_lines))
@@ -798,6 +800,8 @@ static int print_header(struct libscols_table *tb, struct libscols_buffer *buf)
 
        if (rc == 0)
                fputs(linesep(tb), tb->out);
+
+       tb->header_printed = 1;
        return rc;
 }
 
@@ -1306,6 +1310,9 @@ err:
  * @start: first printed line or NULL to print from the beggin of the table
  * @end: last printed line or NULL to print all from start.
  *
+ * If the start is the first line in the table than prints table header too.
+ * The header is printed only once.
+ *
  * Returns: 0, a negative value in case of an error.
  */
 int scols_table_print_range(   struct libscols_table *tb,
@@ -1332,10 +1339,16 @@ int scols_table_print_range(    struct libscols_table *tb,
        } else
                scols_reset_iter(&itr, SCOLS_ITER_FORWARD);
 
-       rc = print_range(tb, buf, &itr, end);
+       if (itr.p == tb->tb_lines.next) {
+               rc = print_header(tb, buf);
+               if (rc)
+                       goto done;
+       }
 
+       rc = print_range(tb, buf, &itr, end);
+done:
        free_buffer(buf);
-       return 0;
+       return rc;
 }
 
 /**
@@ -1361,6 +1374,7 @@ int scols_print_table(struct libscols_table *tb)
                return 0;
        }
 
+       tb->header_printed = 0;
        rc = initialize_printting(tb, &buf);
        if (rc)
                return rc;