libsmartcols: allow to change cell padding char

Signed-off-by: Karel Zak <kzak@redhat.com>
This commit is contained in:
Karel Zak 2016-09-07 11:07:04 +02:00
parent 7208ef864b
commit 64ce7cbbc0
7 changed files with 33 additions and 8 deletions

View File

@ -86,6 +86,7 @@ scols_symbols_set_branch
scols_symbols_set_right
scols_symbols_set_vertical
scols_symbols_set_title_padding
scols_symbols_set_cell_padding
scols_unref_symbols
</SECTION>

View File

@ -118,6 +118,7 @@ extern int scols_symbols_set_branch(struct libscols_symbols *sb, const char *str
extern int scols_symbols_set_vertical(struct libscols_symbols *sb, const char *str);
extern int scols_symbols_set_right(struct libscols_symbols *sb, const char *str);
extern int scols_symbols_set_title_padding(struct libscols_symbols *sb, const char *str);
extern int scols_symbols_set_cell_padding(struct libscols_symbols *sb, const char *str);
/* cell.c */
extern int scols_reset_cell(struct libscols_cell *ce);

View File

@ -140,4 +140,5 @@ global:
SMARTCOLS_2.29 {
global:
scols_column_is_wrapnl;
scols_symbols_set_cell_padding;
} SMARTCOLS_2.28;

View File

@ -54,6 +54,7 @@ struct libscols_symbols {
char *vert;
char *right;
char *title_padding;
char *cell_padding;
};
/*

View File

@ -106,7 +106,7 @@ int scols_symbols_set_right(struct libscols_symbols *sb, const char *str)
/**
* scols_symbols_set_title_padding:
* @sb: a pointer to a struct libscols_symbols instance
* @str: a string which will represent the symbols which wraps title output
* @str: a string which will represent the symbols which fill title output
*
* The current implementation uses only the first byte from the padding string.
* A multibyte chars are not supported yet.
@ -120,6 +120,22 @@ int scols_symbols_set_title_padding(struct libscols_symbols *sb, const char *str
return strdup_to_struct_member(sb, title_padding, str);
}
/**
* scols_symbols_set_cell_padding:
* @sb: a pointer to a struct libscols_symbols instance
* @str: a string which will represent the symbols which fill cells
*
* The padding char has to take up just one cell on the terminal.
*
* Returns: 0, a negative value in case of an error.
*
* Since: 2.29
*/
int scols_symbols_set_cell_padding(struct libscols_symbols *sb, const char *str)
{
return strdup_to_struct_member(sb, cell_padding, str);
}
/**
* scols_copy_symbols:
* @sb: a pointer to a struct libscols_symbols instance
@ -146,6 +162,8 @@ struct libscols_symbols *scols_copy_symbols(const struct libscols_symbols *sb)
rc = scols_symbols_set_right(ret, sb->right);
if (!rc)
rc = scols_symbols_set_title_padding(ret, sb->title_padding);
if (!rc)
rc = scols_symbols_set_cell_padding(ret, sb->cell_padding);
if (!rc)
return ret;

View File

@ -695,6 +695,7 @@ int scols_table_set_symbols(struct libscols_table *tb,
scols_symbols_set_right(tb->symbols, "`-");
}
scols_symbols_set_title_padding(tb->symbols, " ");
scols_symbols_set_cell_padding(tb->symbols, " ");
}
return 0;

View File

@ -27,6 +27,11 @@
#include "carefulputc.h"
#include "smartcolsP.h"
#define colsep(tb) ((tb)->colsep ? (tb)->colsep : " ")
#define linesep(tb) ((tb)->linesep ? (tb)->linesep : "\n")
#define cellpadding(tb) ((tb) && (tb)->symbols && (tb)->symbols->cell_padding ? (tb)->symbols->cell_padding : " ")
/* This is private struct to work with output data */
struct libscols_buffer {
char *begin; /* begin of the buffer */
@ -190,9 +195,6 @@ static int is_last_column(struct libscols_column *cl)
return 0;
}
#define colsep(tb) ((tb)->colsep ? (tb)->colsep : " ")
#define linesep(tb) ((tb)->linesep ? (tb)->linesep : "\n")
static int has_pending_data(struct libscols_table *tb)
{
@ -248,7 +250,7 @@ static void print_empty_cell(struct libscols_table *tb,
/* fill rest of cell with space */
for(; len_pad < cl->width; ++len_pad)
fputc(' ', tb->out);
fputs(cellpadding(tb), tb->out);
fputs(colsep(tb), tb->out);
}
@ -393,7 +395,7 @@ static int print_pending_data(
return 0;
for (i = len; i < width; i++)
fputc(' ', tb->out); /* padding */
fputs(cellpadding(tb), tb->out); /* padding */
fputs(colsep(tb), tb->out); /* columns separator */
return 0;
@ -505,7 +507,7 @@ static int print_data(struct libscols_table *tb,
if (color)
fputs(color, tb->out);
for (i = len; i < width; i++)
fputc(' ', tb->out);
fputs(cellpadding(tb), tb->out);
fputs(data, tb->out);
if (color)
fputs(UL_COLOR_RESET, tb->out);
@ -528,7 +530,7 @@ static int print_data(struct libscols_table *tb,
fputs(data, tb->out);
}
for (i = len; i < width; i++)
fputc(' ', tb->out); /* padding */
fputs(cellpadding(tb), tb->out); /* padding */
if (is_last_column(cl))
return 0;