libsmartcols: add functions to control terminal usage
Signed-off-by: Karel Zak <kzak@redhat.com>
This commit is contained in:
parent
fa4691833a
commit
19055a25ed
|
@ -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>
|
||||
|
||||
|
|
|
@ -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);
|
||||
|
|
|
@ -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;
|
||||
|
|
|
@ -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 */
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
|
|
|
@ -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;
|
||||
|
||||
|
|
Loading…
Reference in New Issue