libsmartcols/src/libsmartcols.h.in: add library functions
PATCH v2 1/8 libsmartcols/src/libsmartcols.h.in: add library functions
--- libsmartcols/src/libsmartcols.h.in | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-)
libsmartcols/src/libsmartcols.h.in +9 -1
@@ -83,6 +83,7 @@ enum { SCOLS_FL_RIGHT = (1 << 2), /* align to the right */ SCOLS_FL_STRICTWIDTH = (1 << 3), /* don't reduce width if column is empty */ SCOLS_FL_NOEXTREMES = (1 << 4), /* ignore extreme fields when count column width*/ + }; extern struct libscols_iter *scols_new_iter(int direction);
@@ -115,6 +116,8 @@ extern int scols_cell_refer_data(struct libscols_cell *ce, char *str); extern const char *scols_cell_get_data(const struct libscols_cell *ce); extern int scols_cell_set_color(struct libscols_cell *ce, const char *color); extern const char *scols_cell_get_color(const struct libscols_cell *ce); +extern int scols_cmpstr_cells(struct libscols_cell *a, struct libscols_cell *b, void *data); +extern int scols_cmpnum_cells(struct libscols_cell *a, struct libscols_cell *b, void *data); /* column.c */ extern int scols_column_is_tree(struct libscols_column *cl);
@@ -134,7 +137,10 @@ extern double scols_column_get_whint(struct libscols_column *cl); extern struct libscols_cell *scols_column_get_header(struct libscols_column *cl); extern int scols_column_set_color(struct libscols_column *cl, const char *color); extern const char *scols_column_get_color(struct libscols_column *cl); +extern int scols_column_set_sortcmp(struct libscols_column *cl, + int (*compar)(struct libscols_cell *a, struct libscols_cell *b, void *), void *data); + /* line.c */ extern struct libscols_line *scols_new_line(void); extern void scols_ref_line(struct libscols_line *ln);
@@ -157,7 +163,7 @@ extern int scols_line_set_data(struct libscols_line *ln, size_t n, const char *d extern int scols_line_refer_data(struct libscols_line *ln, size_t n, char *data); extern struct libscols_line *scols_copy_line(struct libscols_line *ln); -/* table */ +/* table.c */ extern int scols_table_colors_wanted(struct libscols_table *tb); extern int scols_table_is_raw(struct libscols_table *tb); extern int scols_table_is_ascii(struct libscols_table *tb);
@@ -202,6 +208,8 @@ extern int scols_table_set_symbols(struct libscols_table *tb, struct libscols_sy extern int scols_table_set_stream(struct libscols_table *tb, FILE *stream); extern FILE *scols_table_get_stream(struct libscols_table *tb); extern int scols_table_reduce_termwidth(struct libscols_table *tb, size_t reduce); +extern int scols_table_set_key_column(struct libscols_table *tb, struct libscols_column *cl); +struct libscols_column *scols_table_get_key_column(struct libscols_table *tb); /* table_print.c */ extern int scols_print_table(struct libscols_table *tb);
Signature · 1 line
1.8.3.2
PATCH v2 2/8 libsmartcols/src/libsmartcols.sym: add symbols
--- libsmartcols/src/libsmartcols.sym | 5 +++++ 1 file changed, 5 insertions(+)
libsmartcols/src/libsmartcols.sym +5 -0
@@ -9,6 +9,8 @@ global: scols_cell_refer_data; scols_cell_set_color; scols_cell_set_data; + scols_cmpstr_cells; + scols_cmpnum_cells; scols_column_get_color; scols_column_get_flags; scols_column_get_header;
@@ -21,6 +23,7 @@ global: scols_column_set_color; scols_column_set_flags; scols_column_set_whint; + scols_column_set_sortcmp; scols_copy_column; scols_copy_line; scols_copy_symbols;
@@ -97,6 +100,8 @@ global: scols_table_set_line_separator; scols_table_set_stream; scols_table_set_symbols; + scols_table_set_key_column; + scols_table_get_key_column; scols_unref_column; scols_unref_line; scols_unref_symbols;
Signature · 1 line
1.8.3.2
PATCH v2 3/8 libsmartcols/src/smartcolsP.h: add new field to struct libscols_table
In reply to Shakur Shams Mullick--- libsmartcols/src/smartcolsP.h | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-)
libsmartcols/src/smartcolsP.h +10 -7
@@ -44,7 +44,7 @@ UL_DEBUG_DECLARE_MASK(libsmartcols);struct libscols_iter {struct list_head *p;/* current position */ struct list_head *head;/* start position */ -int direction;/* SCOLS_ITER_{FOR,BACK}WARD */ +int direction;/* SCOLS_ITER_{FOR,BACK}WARD */ };/*
@@ -82,6 +82,8 @@ struct libscols_column {int flags;int is_extreme;char *color;/* default column color */ + +int (*compar)(struct libscols_cell *,struct libscols_cell *,void *);/* comparison function to use*/ struct libscols_cell header;struct list_head cl_columns;
@@ -108,7 +110,7 @@ struct libscols_line { };enum { -SCOLS_FMT_HUMAN =0 ,/* default, human readable */ +SCOLS_FMT_HUMAN =0 ,/* default, human readable */ SCOLS_FMT_RAW ,/* space separated */ SCOLS_FMT_EXPORT /* COLNAME="data" ... */ };
@@ -131,15 +133,16 @@ struct libscols_table {struct list_head tb_columns;struct list_head tb_lines;struct libscols_symbols *symbols; +struct libscols_column *key_cl;/* key column */ int format;/* SCOLS_FMT_* */ - +/* flags */ unsigned int ascii :1, /* don't use unicode */ - colors_wanted :1, /* enable colors */ - is_term :1, /* isatty() */ - maxout :1, /* maximalize output */ - no_headings :1; /* don't print header */ + colors_wanted :1, /* enable colors */ + is_term :1, /* isatty() */ + maxout :1, /* maximalize output */ + no_headings :1; /* don't print header */ };#define IS_ITER_FORWARD (_i) ((_i)->direction == SCOLS_ITER_FORWARD)
Signature · 1 line
1.8.3.2
PATCH 4/8 libsmartcols/src/cell.c: add two functions to compare number and string cells
In reply to Shakur Shams Mullickscols_cmpstr_cells and scols_cmpnum_cells compare string and number cells. When comparing data of two cells for the purpose of sorting, it is not necessary to use these two library functions. Developers can use their own implementation of cell comparison function.
Furthermore these two functions can be extended to fascilitate sorting in reverse order.
These two functions could be modified to fascilitate sorting in reverse order.
--- libsmartcols/src/cell.c | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+)
libsmartcols/src/cell.c +31 -0
@@ -25,6 +25,7 @@#include <ctype.h> #include "smartcolsP.h" +#include "strutils.h" /* * The cell has no ref-counting, free() and new() functions. All is
@@ -181,3 +182,33 @@ int scols_cell_copy_content(struct libscols_cell *dest, rc =scols_cell_set_color (dest,scols_cell_get_color (src));return rc; } + +int scols_cmpstr_cells (struct libscols_cell *a,struct libscols_cell *b,void *data) +{ +int n, m; +const char *adata, *bdata; + + adata =scols_cell_get_data (a); + bdata =scols_cell_get_data (b); + + n =strlen (adata); + m =strlen (bdata); + +return strncmp (adata, bdata, (n>=m ? n : m)); +} + +int scols_cmpnum_cells (struct libscols_cell *a,struct libscols_cell *b,void *data) +{ +uint64_t n, m; +const char *adata, *bdata; + + adata =scols_cell_get_data (a); + bdata =scols_cell_get_data (b); + +strtosize (adata, &n); +strtosize (bdata, &m); + +if (n >= m) +return 1 ; +return -1 ; +}
Signature · 1 line
1.8.3.2
PATCH 5/8 libsmartcols/src/column.c: add function to set comparison function scols_column_set_sortcmp() sets the cell comparison function to use
In reply to Shakur Shams Mullick--- libsmartcols/src/column.c | 12 ++++++++++++ 1 file changed, 12 insertions(+)
libsmartcols/src/column.c +12 -0
@@ -313,3 +313,15 @@ int scols_column_is_noextremes(struct libscols_column *cl)return -EINVAL ;return cl->flags &SCOLS_FL_NOEXTREMES ; } + +int scols_column_set_sortcmp (struct libscols_column *cl, +int (*compar)(struct libscols_cell *a,struct libscols_cell *b,void *data), +void *data) +{ +assert (cl); +if (!cl) +return -EINVAL ; + cl->compar = compar; + +return 0 ; +}
Signature · 1 line
1.8.3.2
PATCH v2 6/8 libsmartcols/src/table.c: set and get key column to use for sorting scols_table_set_key_column() sets the column to use scols_table_get_key_column() returns the column being used
In reply to Shakur Shams Mullick--- libsmartcols/src/table.c | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-)
libsmartcols/src/table.c +20 -1
@@ -229,7 +229,12 @@ struct libscols_column *scols_table_new_column(struct libscols_table *tb, goto err;if (scols_cell_set_data (hr, name)) goto err; - +/*if(strncmp(name,"NAME", 4) == 0) +scols_column_set_sortcmp(cl, scols_cmpstr_cells, &cl->seqnum); ++ else if(strncmp(name,"SIZE", 4) == 0) +scols_column_set_sortcmp(cl, scols_cmpnum_cells, &cl->seqnum);*/ +scols_column_set_whint (cl, whint);scols_column_set_flags (cl, flags);
@@ -977,3 +982,17 @@ char *scols_table_get_line_separator(struct libscols_table *tb)return tb->linesep; } + +int scols_table_set_key_column (struct libscols_table *tb,struct libscols_column *cl) +{ +assert (tb); +if (!tb) +return -EINVAL ; + tb->key_cl = cl; +} + +struct libscols_column *scols_table_get_key_column (struct libscols_table *tb) +{ +assert (tb); +return tb ? tb->key_cl :NULL ; +}
Signature · 1 line
1.8.3.2
PATCH v2 7/8 libsmartcols/src/table_print.c: sort the list before printing
In reply to Shakur Shams Mullick--- libsmartcols/src/table_print.c | 35 +++++++++++++++++++++++++++++++---- 1 file changed, 31 insertions(+), 4 deletions(-)
libsmartcols/src/table_print.c +31 -4
@@ -20,6 +20,7 @@#include <string.h> #include <termios.h> #include <ctype.h> +#include <list.h> #include "nls.h" #include "mbsalign.h"
@@ -27,6 +28,7 @@#include "ttyutils.h" #include "carefulputc.h" #include "smartcolsP.h" +#include "strutils.h" /* This is private struct to work with output data */ struct libscols_buffer {
@@ -38,6 +40,8 @@ struct libscols_buffer {size_t art_idx;/* begin of the tree ascii art or zero */ }; +static uint sq; +static struct libscols_buffer *new_buffer (size_t sz) {struct libscols_buffer *buf =malloc (sz +sizeof (struct libscols_buffer ));
@@ -694,7 +698,22 @@ static size_t strlen_line(struct libscols_line *ln)return sz; } - +int comparison_func (struct list_head *a,struct list_head *b,void *data) +{ +struct libscols_column *cl; +struct libscols_line *ra, *rb; +struct libscols_cell *ca, *cb; + + cl = (struct libscols_column *)(data); + + ra = list_entry(a, struct libscols_line, ln_lines); + rb = list_entry(b, struct libscols_line, ln_lines); + + ca =scols_line_get_cell (ra, cl->seqnum); + cb =scols_line_get_cell (rb, cl->seqnum); + +return cl->compar (ca, cb,NULL ); +}/** * scols_print_table:
@@ -709,9 +728,12 @@ int scols_print_table(struct libscols_table *tb)int rc =0 ;size_t bufsz;struct libscols_line *ln; +struct libscols_column *cl;struct libscols_iter itr;struct libscols_buffer *buf; - +struct list_head *p; +char *sdata; +assert (tb);if (!tb)return -1 ;
@@ -742,10 +764,15 @@ int scols_print_table(struct libscols_table *tb) rc =recount_widths (tb, buf);if (scols_table_is_tree (tb)) - rc =print_tree (tb, buf); + rc =print_tree (tb, buf); +else if (!scols_table_get_key_column (tb)) + rc =print_table (tb, buf);else + { + cl =scols_table_get_key_column (tb); +list_sort (&tb->tb_lines, comparison_func, cl); rc =print_table (tb, buf); - + }free_buffer (buf);return rc; }
Signature · 1 line
1.8.3.2
PATCH v2 8/8 misc-utils/lsblk.c: adds sorting support for lsblk
In reply to Shakur Shams Mullicklsblk --order-by <column>
--- misc-utils/lsblk.c | 55 ++++++++++++++++++++++++++++++++++++++++++------------ 1 file changed, 43 insertions(+), 12 deletions(-)
misc-utils/lsblk.c +43 -12
@@ -61,6 +61,7 @@#include "closestream.h" #include "mangle.h" #include "optutils.h" +#include "list.h" /* column IDs */ enum {
@@ -110,18 +111,24 @@ enum {/* basic table settings */ enum { -LSBLK_ASCII = (1 <<0 ), -LSBLK_RAW = (1 <<1 ), -LSBLK_NOHEADINGS = (1 <<2 ), -LSBLK_EXPORT = (1 <<3 ), -LSBLK_TREE = (1 <<4 ), +LSBLK_ASCII = (1 <<0 ), +LSBLK_RAW = (1 <<1 ), +LSBLK_NOHEADINGS = (1 <<2 ), +LSBLK_EXPORT = (1 <<3 ), +LSBLK_TREE = (1 <<4 ), +}; + +enum { + sort_none =-1 , + sort_name, + sort_size };/* column names */ struct colinfo {const char *name;/* header */ double whint;/* width hint (N < 1 is in percent of termwidth) */ -int flags;/* SCOLS_FL_* */ +int flags;/* SCOLS_FL_* */ const char *help; };
@@ -1355,6 +1362,7 @@ static void __attribute__((__noreturn__)) help(FILE *out)fputs (_ (" -i, --ascii use ascii characters only\n" ), out);fputs (_ (" -I, --include <list> show only devices with specified major numbers\n" ), out);fputs (_ (" -l, --list use list format output\n" ), out); +fputs (_ (" -c, --order-by <column> show list sorted by name\n" ), out);fputs (_ (" -m, --perms output info about permissions\n" ), out);fputs (_ (" -n, --noheadings don't print headings\n" ), out);fputs (_ (" -o, --output <list> output columns\n" ), out);
@@ -1388,21 +1396,23 @@ static void check_sysdevblock(void)int main (int argc,char *argv[]) {struct lsblk _ls; +struct libscols_column *cl, *tmp;int scols_flags =LSBLK_TREE ; +int sort_flags = sort_none;int i, c, status =EXIT_FAILURE ;char *outarg =NULL ;static const struct option longopts[] = { - {"all" ,0 ,0 ,'a' }, + {"all" ,0 ,0 ,'a' }, {"bytes" ,0 ,0 ,'b' }, {"nodeps" ,0 ,0 ,'d' }, {"discard" ,0 ,0 ,'D' }, - {"help" ,0 ,0 ,'h' }, + {"help" ,0 ,0 ,'h' }, {"output" ,1 ,0 ,'o' }, {"perms" ,0 ,0 ,'m' }, {"noheadings" ,0 ,0 ,'n' }, {"list" ,0 ,0 ,'l' }, - {"ascii" ,0 ,0 ,'i' }, + {"ascii" ,0 ,0 ,'i' }, {"raw" ,0 ,0 ,'r' }, {"inverse" ,0 ,0 ,'s' }, {"fs" ,0 ,0 ,'f' },
@@ -1413,6 +1423,7 @@ int main(int argc, char *argv[]) { "pairs", 0, 0, 'P' }, { "scsi", 0, 0, 'S' }, { "version", 0, 0, 'V' }, + { "order-by", 1, 0, 'c' }, { NULL, 0, 0, 0 }, };
@@ -1432,7 +1443,7 @@ int main(int argc, char *argv[])memset (lsblk,0 ,sizeof (*lsblk));while ((c =getopt_long (argc, argv, -"abdDe:fhlnmo:pPiI:rstVS" , longopts,NULL )) !=-1 ) { +"abdDe:fhlnmo:pPiI:rstVc:S" , longopts,NULL )) !=-1 ) {err_exclusive_options (c, longopts, excl, excl_st);
@@ -1462,6 +1473,13 @@ int main(int argc, char *argv[])case 'l' : scols_flags &= ~LSBLK_TREE ;/* disable the default */ break ; +case 'c' : +if (strncasecmp (optarg,"size" ,4 ) ==0 ) + sort_flags = sort_size; +else if (strncasecmp (optarg,"name" ,4 ) ==0 ) + sort_flags = sort_name; + scols_flags &= ~LSBLK_TREE ;/* disable the default */ +break ;case 'n' : scols_flags |=LSBLK_NOHEADINGS ;break ;
@@ -1565,6 +1583,7 @@ int main(int argc, char *argv[])scols_table_enable_ascii (lsblk->table, !!(scols_flags &LSBLK_ASCII ));scols_table_enable_noheadings (lsblk->table, !!(scols_flags &LSBLK_NOHEADINGS )); + cl = tmp =NULL ;for (i =0 ; i < ncolumns; i++) {struct colinfo *ci =get_column_info (i);int fl = ci->flags;
@@ -1572,17 +1591,29 @@ int main(int argc, char *argv[])if (!(scols_flags &LSBLK_TREE ) &&get_column_id (i) ==COL_NAME ) fl &= ~SCOLS_FL_TREE ; -if (!scols_table_new_column (lsblk->table, ci->name, ci->whint, fl)) { +if (!(tmp =scols_table_new_column (lsblk->table, ci->name, ci->whint, fl))) {warn (_ ("failed to initialize output column" )); goto leave; } +if (((sort_flags == sort_name) &&get_column_id (i) ==COL_NAME ) || + ((sort_flags == sort_size) &&get_column_id (i) ==COL_SIZE )){ + cl = tmp; + } }if (optind == argc) status =iterate_block_devices ();else while (optind < argc) status =process_one_device (argv[optind++]); - + + +if (sort_flags == sort_name) +scols_column_set_sortcmp (cl, scols_cmpstr_cells,NULL ); + +else if (sort_flags == sort_size) +scols_column_set_sortcmp (cl, scols_cmpnum_cells,NULL ); + +scols_table_set_key_column (lsblk->table, cl);scols_print_table (lsblk->table); leave:
Signature · 1 line
1.8.3.2
Karel Zak
maintainerRe: [PATCH 4/8] libsmartcols/src/cell.c: add two functions to compare number and string cells
In reply to Shakur Shams Mullicklibsmartcols/src/cell.c int scols_cell_copy_content(struct libscols_cell *dest, · line 198
Quoted 13 lines · Shakur Shams Mullick
libsmartcols/src/cell.c +13
-0+int scols_cmpstr_cells (struct libscols_cell *a,struct libscols_cell *b,void *data) +{ +int n, m; +const char *adata, *bdata; + + adata =scols_cell_get_data (a); + bdata =scols_cell_get_data (b); + + n =strlen (adata); + m =strlen (bdata); + +return strncmp (adata, bdata, (n>=m ? n : m)); +}
This looks good.
libsmartcols/src/cell.c int scols_cell_copy_content(struct libscols_cell *dest, · line 209
Quoted 10 lines
libsmartcols/src/cell.c +10
-0+int scols_cmpnum_cells (struct libscols_cell *a,struct libscols_cell *b,void *data) +{ +uint64_t n, m; +const char *adata, *bdata; + + adata =scols_cell_get_data (a); + bdata =scols_cell_get_data (b); + +strtosize (adata, &n); +strtosize (bdata, &m);
I have doubts that we can scols_cmpnum_cells() to the library API. (Sorry for the suggestion.)
The size strings are composed by applications and no by the library, the size_to_human_string() is private util-linux function, so rely on strtosize() seems strange. The cmpnum_cells() have to be within lsblk.c.
It would be better to provide scols_cmpstr_cells() only.
Note that you don't have to send v3, I'm going to fix/improve it before merge. Thanks!
Karel
Quoted 12 lines
libsmartcols/src/cell.c +4
-0+if (n >= m) +return 1 ; +return -1 ; +}Signature · 6 lines
1.8.3.2 -- To unsubscribe from this list: send the line "unsubscribe util-linux" in the body of a message to [email protected] More majordomo info at http://vger.kernel.org/majordomo-info.html
Signature · 2 lines
Karel Zak <[email protected]> http://karelzak.blogspot.com
Karel Zak
maintainerRe: [PATCH v2 7/8] libsmartcols/src/table_print.c: sort the list before printing
In reply to Shakur Shams MullickQuoted 5 lines · Shakur Shams Mullick
libsmartcols/src/table_print.c +5
-0+int comparison_func (struct list_head *a,struct list_head *b,void *data) +{ +struct libscols_column *cl; +struct libscols_line *ra, *rb; +struct libscols_cell *ca, *cb;
libsmartcols/src/table_print.c static size_t strlen_line(struct libscols_line *ln) · line 715
Quoted 10 lines
libsmartcols/src/table_print.c +10
-0+ + cl = (struct libscols_column *)(data); + + ra = list_entry(a, struct libscols_line, ln_lines); + rb = list_entry(b, struct libscols_line, ln_lines); + + ca =scols_line_get_cell (ra, cl->seqnum); + cb =scols_line_get_cell (rb, cl->seqnum); + +return cl->compar (ca, cb,NULL );
^^^^^
I guess that we need something like cl->compare_data.
libsmartcols/src/table_print.c int scols_print_table(struct libscols_table *tb) · line 772
Quoted 1 line
libsmartcols/src/table_print.c +1
-0+ cl =scols_table_get_key_column (tb);
Do we really need table->cl_key? Maybe it would be better to add
scols_sort_table(tb, cl)
and keep control of the sort column in the applications. It will also allow to sort table by more columns.
Karel
Signature · 2 lines
Karel Zak <[email protected]> http://karelzak.blogspot.com
Re: [PATCH v2 7/8] libsmartcols/src/table_print.c: sort the list before printing
Quoted 9 lines
I guess that we need something like
cl->compare_data.libsmartcols/src/table_print.c int scols_print_table(struct libscols_table *tb) · line 772
Quoted 1 line
libsmartcols/src/table_print.c +1
-0+ cl =scols_table_get_key_column (tb);Do we really need
table->cl_key? Maybe it would be better to add
scols_sort_table(tb, cl)
Ok. I will submit v3 for this and related patches.
Quoted 33 lines · Karel Zak
Quoted 5 lines · Shakur Shams Mullick
libsmartcols/src/table_print.c +5
-0+int comparison_func (struct list_head *a,struct list_head *b,void *data) +{ +struct libscols_column *cl; +struct libscols_line *ra, *rb; +struct libscols_cell *ca, *cb;libsmartcols/src/table_print.c static size_t strlen_line(struct libscols_line *ln) · line 715
Quoted 10 lines
libsmartcols/src/table_print.c +10
-0+ + cl = (struct libscols_column *)(data); + + ra = list_entry(a, struct libscols_line, ln_lines); + rb = list_entry(b, struct libscols_line, ln_lines); + + ca =scols_line_get_cell (ra, cl->seqnum); + cb =scols_line_get_cell (rb, cl->seqnum); + +return cl->compar (ca, cb,NULL );^^^^^I guess that we need something like
cl->compare_data.libsmartcols/src/table_print.c int scols_print_table(struct libscols_table *tb) · line 772
Quoted 1 line
libsmartcols/src/table_print.c +1
-0+ cl =scols_table_get_key_column (tb);Do we really need
table->cl_key? Maybe it would be better to add
scols_sort_table(tb, cl)and keep control of the sort column in the applications. It will also allow to sort table by more columns.
Karel