]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
lib/tt: add TT_FL_MAX to fill screen
authorKarel Zak <kzak@redhat.com>
Fri, 22 Nov 2013 12:53:29 +0000 (13:53 +0100)
committerKarel Zak <kzak@redhat.com>
Tue, 11 Mar 2014 10:35:12 +0000 (11:35 +0100)
Signed-off-by: Karel Zak <kzak@redhat.com>
include/tt.h
lib/tt.c

index ce76524ac70175b7b5c651cce5d5a38f983ba28f..b1fbf927e56f3da125aecda243a225f6c7461c35 100644 (file)
@@ -19,17 +19,18 @@ enum {
        TT_FL_ASCII       = (1 << 2),
        TT_FL_NOHEADINGS  = (1 << 3),
        TT_FL_EXPORT      = (1 << 4),
+       TT_FL_MAX         = (1 << 5),   /* maximalize column width if possible */
 
        /*
         * Column flags
         */
-       TT_FL_TRUNC       = (1 << 5),   /* truncate fields data if necessary */
-       TT_FL_TREE        = (1 << 6),   /* use tree "ascii art" */
-       TT_FL_RIGHT       = (1 << 7),   /* align to the right */
-       TT_FL_STRICTWIDTH = (1 << 8),   /* don't reduce width if column is empty */
-       TT_FL_NOEXTREMES  = (1 << 9),   /* ignore extreme fields when count column width*/
+       TT_FL_TRUNC       = (1 << 10),  /* truncate fields data if necessary */
+       TT_FL_TREE        = (1 << 11),  /* use tree "ascii art" */
+       TT_FL_RIGHT       = (1 << 12),  /* align to the right */
+       TT_FL_STRICTWIDTH = (1 << 13),  /* don't reduce width if column is empty */
+       TT_FL_NOEXTREMES  = (1 << 14),   /* ignore extreme fields when count column width*/
 
-       TT_FL_FREEDATA    = (1 << 10),  /* free() data in tt_free_table() */
+       TT_FL_FREEDATA    = (1 << 15),  /* free() data in tt_free_table() */
 };
 
 struct tt {
@@ -77,6 +78,8 @@ struct tt_line {
 };
 
 extern struct tt *tt_new_table(int flags);
+extern int tt_get_flags(struct tt *tb);
+extern void tt_set_flags(struct tt *tb, int flags);
 extern void tt_free_table(struct tt *tb);
 extern void tt_remove_lines(struct tt *tb);
 extern int tt_print_table(struct tt *tb);
index e94524e8615bc5eacec3625b60fa3817c2b2f945..b5beb7ad734ec8e50e762369e9dcbc055b78d351 100644 (file)
--- a/lib/tt.c
+++ b/lib/tt.c
@@ -216,6 +216,22 @@ struct tt *tt_new_table(int flags)
        return tb;
 }
 
+/*
+ * Be careful, the best way is to use:
+ *
+ *     tt_set_flags(tb, tb_get_flags(tb) | TT_FL_xxx));
+ */
+void tt_set_flags(struct tt *tb, int flags)
+{
+       if (tb)
+               tb->flags = flags;
+}
+
+int tt_get_flags(struct tt *tb)
+{
+       return tb ? tb->flags : 0;
+}
+
 void tt_set_stream(struct tt *tb, FILE *out)
 {
        if (!tb)
@@ -587,6 +603,7 @@ static void recount_widths(struct tt *tb, char *buf, size_t bufsz)
                }
        }
 
+       /* Cool, we have extra space, use it! */
        if (width < tb->termwidth) {
                /* try to found extreme column which fits into available space
                 */
@@ -617,7 +634,20 @@ static void recount_widths(struct tt *tb, char *buf, size_t bufsz)
                                        break;
                        }
                }
-               if (width < tb->termwidth) {
+
+               if (width < tb->termwidth && (tb->flags & TT_FL_MAX)) {
+                       /* try enlarge all columns */
+                       while (width < tb->termwidth) {
+                               list_for_each(p, &tb->tb_columns) {
+                                       struct tt_column *cl =
+                                               list_entry(p, struct tt_column, cl_columns);
+                                       cl->width++;
+                                       width++;
+                                       if (width == tb->termwidth)
+                                               break;
+                               }
+                       }
+               } else if (width < tb->termwidth) {
                        /* enalarge the last column */
                        struct tt_column *cl = list_entry(
                                tb->tb_columns.prev, struct tt_column, cl_columns);
@@ -769,7 +799,7 @@ static void print_data(struct tt *tb, struct tt_column *cl, char *data)
        }
        width = cl->width;
 
-       if (is_last_column(tb, cl) && len < width)
+       if (is_last_column(tb, cl) && len < width && !(tb->flags & TT_FL_MAX))
                width = len;
 
        /* truncate data */