lib/tt: add TT_FL_MAX to fill screen

Signed-off-by: Karel Zak <kzak@redhat.com>
This commit is contained in:
Karel Zak 2013-11-22 13:53:29 +01:00
parent 575a1de03a
commit b839bd4fa1
2 changed files with 41 additions and 8 deletions

View 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);

View File

@ -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 */