lib: [tt.c] clean up used types
Signed-off-by: Karel Zak <kzak@redhat.com>
This commit is contained in:
parent
b28f22673d
commit
3922ef8025
14
include/tt.h
14
include/tt.h
|
@ -30,8 +30,8 @@ enum {
|
||||||
};
|
};
|
||||||
|
|
||||||
struct tt {
|
struct tt {
|
||||||
int ncols; /* number of columns */
|
size_t ncols; /* number of columns */
|
||||||
int termwidth; /* terminal width */
|
size_t termwidth; /* terminal width */
|
||||||
int flags;
|
int flags;
|
||||||
int first_run;
|
int first_run;
|
||||||
|
|
||||||
|
@ -43,10 +43,10 @@ struct tt {
|
||||||
|
|
||||||
struct tt_column {
|
struct tt_column {
|
||||||
const char *name; /* header */
|
const char *name; /* header */
|
||||||
int seqnum;
|
size_t seqnum;
|
||||||
|
|
||||||
int width; /* real column width */
|
size_t width; /* real column width */
|
||||||
int width_min; /* minimal width (width of header) */
|
size_t width_min; /* minimal width (width of header) */
|
||||||
double width_hint; /* hint (N < 1 is in percent of termwidth) */
|
double width_hint; /* hint (N < 1 is in percent of termwidth) */
|
||||||
|
|
||||||
int flags;
|
int flags;
|
||||||
|
@ -58,7 +58,7 @@ struct tt_line {
|
||||||
struct tt *table;
|
struct tt *table;
|
||||||
char const **data;
|
char const **data;
|
||||||
void *userdata;
|
void *userdata;
|
||||||
ssize_t data_sz; /* strlen of all data */
|
size_t data_sz; /* strlen of all data */
|
||||||
|
|
||||||
struct list_head ln_lines; /* table lines */
|
struct list_head ln_lines; /* table lines */
|
||||||
|
|
||||||
|
@ -76,7 +76,7 @@ extern int tt_print_table(struct tt *tb);
|
||||||
extern struct tt_column *tt_define_column(struct tt *tb, const char *name,
|
extern struct tt_column *tt_define_column(struct tt *tb, const char *name,
|
||||||
double whint, int flags);
|
double whint, int flags);
|
||||||
|
|
||||||
extern struct tt_column *tt_get_column(struct tt *tb, int colnum);
|
extern struct tt_column *tt_get_column(struct tt *tb, size_t colnum);
|
||||||
|
|
||||||
extern struct tt_line *tt_add_line(struct tt *tb, struct tt_line *parent);
|
extern struct tt_line *tt_add_line(struct tt *tb, struct tt_line *parent);
|
||||||
|
|
||||||
|
|
36
lib/tt.c
36
lib/tt.c
|
@ -211,7 +211,7 @@ err:
|
||||||
*
|
*
|
||||||
* Returns: pointer to column or NULL
|
* Returns: pointer to column or NULL
|
||||||
*/
|
*/
|
||||||
struct tt_column *tt_get_column(struct tt *tb, int colnum)
|
struct tt_column *tt_get_column(struct tt *tb, size_t colnum)
|
||||||
{
|
{
|
||||||
struct list_head *p;
|
struct list_head *p;
|
||||||
|
|
||||||
|
@ -241,8 +241,10 @@ int tt_line_set_data(struct tt_line *ln, int colnum, const char *data)
|
||||||
if (!cl)
|
if (!cl)
|
||||||
return -1;
|
return -1;
|
||||||
|
|
||||||
if (ln->data[cl->seqnum])
|
if (ln->data[cl->seqnum]) {
|
||||||
ln->data_sz -= strlen(ln->data[cl->seqnum]);
|
size_t sz = strlen(ln->data[cl->seqnum]);;
|
||||||
|
ln->data_sz = ln->data_sz > sz ? ln->data_sz - sz : 0;
|
||||||
|
}
|
||||||
|
|
||||||
ln->data[cl->seqnum] = data;
|
ln->data[cl->seqnum] = data;
|
||||||
if (data)
|
if (data)
|
||||||
|
@ -345,7 +347,8 @@ static char *line_get_data(struct tt_line *ln, struct tt_column *cl,
|
||||||
static void recount_widths(struct tt *tb, char *buf, size_t bufsz)
|
static void recount_widths(struct tt *tb, char *buf, size_t bufsz)
|
||||||
{
|
{
|
||||||
struct list_head *p;
|
struct list_head *p;
|
||||||
int width = 0, trunc_only;
|
size_t width = 0;
|
||||||
|
int trunc_only;
|
||||||
|
|
||||||
/* set width according to the size of data
|
/* set width according to the size of data
|
||||||
*/
|
*/
|
||||||
|
@ -380,10 +383,10 @@ static void recount_widths(struct tt *tb, char *buf, size_t bufsz)
|
||||||
cl->width = cl->width_min;
|
cl->width = cl->width_min;
|
||||||
|
|
||||||
else if (cl->width_hint >= 1 &&
|
else if (cl->width_hint >= 1 &&
|
||||||
cl->width < (int) cl->width_hint &&
|
cl->width < (size_t) cl->width_hint &&
|
||||||
cl->width_min < (int) cl->width_hint)
|
cl->width_min < (size_t) cl->width_hint)
|
||||||
|
|
||||||
cl->width = (int) cl->width_hint;
|
cl->width = (size_t) cl->width_hint;
|
||||||
|
|
||||||
width += cl->width + (is_last_column(tb, cl) ? 0 : 1);
|
width += cl->width + (is_last_column(tb, cl) ? 0 : 1);
|
||||||
}
|
}
|
||||||
|
@ -395,7 +398,7 @@ static void recount_widths(struct tt *tb, char *buf, size_t bufsz)
|
||||||
struct tt_column *cl = list_entry(
|
struct tt_column *cl = list_entry(
|
||||||
tb->tb_columns.prev, struct tt_column, cl_columns);
|
tb->tb_columns.prev, struct tt_column, cl_columns);
|
||||||
|
|
||||||
if (!(cl->flags & TT_FL_RIGHT))
|
if (!(cl->flags & TT_FL_RIGHT) && tb->termwidth - width > 0)
|
||||||
cl->width += tb->termwidth - width;
|
cl->width += tb->termwidth - width;
|
||||||
goto leave;
|
goto leave;
|
||||||
}
|
}
|
||||||
|
@ -406,7 +409,7 @@ static void recount_widths(struct tt *tb, char *buf, size_t bufsz)
|
||||||
*/
|
*/
|
||||||
trunc_only = 1;
|
trunc_only = 1;
|
||||||
while(width > tb->termwidth) {
|
while(width > tb->termwidth) {
|
||||||
int org = width;
|
size_t org = width;
|
||||||
|
|
||||||
list_for_each(p, &tb->tb_columns) {
|
list_for_each(p, &tb->tb_columns) {
|
||||||
struct tt_column *cl =
|
struct tt_column *cl =
|
||||||
|
@ -424,13 +427,14 @@ static void recount_widths(struct tt *tb, char *buf, size_t bufsz)
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
/* truncate column with relative sizes */
|
/* truncate column with relative sizes */
|
||||||
if (cl->width_hint < 1 &&
|
if (cl->width_hint < 1 && cl->width > 0 && width > 0 &&
|
||||||
cl->width > cl->width_hint * tb->termwidth) {
|
cl->width > cl->width_hint * tb->termwidth) {
|
||||||
cl->width--;
|
cl->width--;
|
||||||
width--;
|
width--;
|
||||||
}
|
}
|
||||||
/* truncate column with absolute size */
|
/* truncate column with absolute size */
|
||||||
if (cl->width_hint > 1 && !trunc_only) {
|
if (cl->width_hint > 1 && cl->width > 0 && width > 0 &&
|
||||||
|
!trunc_only) {
|
||||||
cl->width--;
|
cl->width--;
|
||||||
width--;
|
width--;
|
||||||
}
|
}
|
||||||
|
@ -450,7 +454,7 @@ leave:
|
||||||
struct tt_column *cl =
|
struct tt_column *cl =
|
||||||
list_entry(p, struct tt_column, cl_columns);
|
list_entry(p, struct tt_column, cl_columns);
|
||||||
|
|
||||||
fprintf(stderr, "width: %s=%d [hint=%d]\n",
|
fprintf(stderr, "width: %s=%zd [hint=%d]\n",
|
||||||
cl->name, cl->width,
|
cl->name, cl->width,
|
||||||
cl->width_hint > 1 ? (int) cl->width_hint :
|
cl->width_hint > 1 ? (int) cl->width_hint :
|
||||||
(int) (cl->width_hint * tb->termwidth));
|
(int) (cl->width_hint * tb->termwidth));
|
||||||
|
@ -507,8 +511,8 @@ static void print_data(struct tt *tb, struct tt_column *cl, char *data)
|
||||||
}
|
}
|
||||||
if (data) {
|
if (data) {
|
||||||
if (!(tb->flags & TT_FL_RAW) && (cl->flags & TT_FL_RIGHT)) {
|
if (!(tb->flags & TT_FL_RAW) && (cl->flags & TT_FL_RIGHT)) {
|
||||||
int xw = cl->width;
|
size_t xw = cl->width;
|
||||||
fprintf(stdout, "%*s", xw, data);
|
fprintf(stdout, "%*s", (int) xw, data);
|
||||||
if (len < xw)
|
if (len < xw)
|
||||||
len = xw;
|
len = xw;
|
||||||
}
|
}
|
||||||
|
@ -521,9 +525,9 @@ static void print_data(struct tt *tb, struct tt_column *cl, char *data)
|
||||||
if (!is_last_column(tb, cl)) {
|
if (!is_last_column(tb, cl)) {
|
||||||
if (len > width && !(cl->flags & TT_FL_TRUNC)) {
|
if (len > width && !(cl->flags & TT_FL_TRUNC)) {
|
||||||
fputc('\n', stdout);
|
fputc('\n', stdout);
|
||||||
for (i = 0; i <= cl->seqnum; i++) {
|
for (i = 0; i <= (size_t) cl->seqnum; i++) {
|
||||||
struct tt_column *x = tt_get_column(tb, i);
|
struct tt_column *x = tt_get_column(tb, i);
|
||||||
printf("%*s ", -x->width, " ");
|
printf("%*s ", -((int)x->width), " ");
|
||||||
}
|
}
|
||||||
} else
|
} else
|
||||||
fputc(' ', stdout); /* columns separator */
|
fputc(' ', stdout); /* columns separator */
|
||||||
|
|
Loading…
Reference in New Issue