* [PATCH v3 1/5] libsmartcols/src/libsmartcols.h.in: add function
@ 2014-05-14 5:09 Shakur Shams Mullick
2014-05-14 5:09 ` [PATCH v3 2/5] libsmartcols/src/libsmartcols.sym: add function symbol Shakur Shams Mullick
` (4 more replies)
0 siblings, 5 replies; 6+ messages in thread
From: Shakur Shams Mullick @ 2014-05-14 5:09 UTC (permalink / raw)
To: util-linux; +Cc: Shakur Shams Mullick
Signed-off-by: Shakur Shams Mullick <shakursmullick@gmail.com>
---
libsmartcols/src/libsmartcols.h.in | 7 ++++++-
1 file changed, 6 insertions(+), 1 deletion(-)
diff --git a/libsmartcols/src/libsmartcols.h.in b/libsmartcols/src/libsmartcols.h.in
index 9468226..5ead53f 100644
--- a/libsmartcols/src/libsmartcols.h.in
+++ b/libsmartcols/src/libsmartcols.h.in
@@ -115,6 +115,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 +136,9 @@ 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);
@@ -206,6 +210,7 @@ extern int scols_table_reduce_termwidth(struct libscols_table *tb, size_t reduce
/* 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);
+extern void scols_sort_table(struct libscols_table *tb, struct libscols_column *cl);
#ifdef __cplusplus
}
--
1.8.3.2
^ permalink raw reply related [flat|nested] 6+ messages in thread
* [PATCH v3 2/5] libsmartcols/src/libsmartcols.sym: add function symbol
2014-05-14 5:09 [PATCH v3 1/5] libsmartcols/src/libsmartcols.h.in: add function Shakur Shams Mullick
@ 2014-05-14 5:09 ` Shakur Shams Mullick
2014-05-14 5:09 ` [PATCH v3 3/5] libsmartcols/src/smartcolsP.h: add field to struct libscols_column Shakur Shams Mullick
` (3 subsequent siblings)
4 siblings, 0 replies; 6+ messages in thread
From: Shakur Shams Mullick @ 2014-05-14 5:09 UTC (permalink / raw)
To: util-linux; +Cc: Shakur Shams Mullick
Signed-off-by: Shakur Shams Mullick <shakursmullick@gmail.com>
---
libsmartcols/src/libsmartcols.sym | 4 ++++
1 file changed, 4 insertions(+)
diff --git a/libsmartcols/src/libsmartcols.sym b/libsmartcols/src/libsmartcols.sym
index 4b64f28..c4ca8b8 100644
--- a/libsmartcols/src/libsmartcols.sym
+++ b/libsmartcols/src/libsmartcols.sym
@@ -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;
@@ -51,6 +54,7 @@ global:
scols_new_table;
scols_parse_version_string;
scols_print_table;
+ scols_sort_table;
scols_print_table_to_string;
scols_ref_column;
scols_ref_line;
--
1.8.3.2
^ permalink raw reply related [flat|nested] 6+ messages in thread
* [PATCH v3 3/5] libsmartcols/src/smartcolsP.h: add field to struct libscols_column
2014-05-14 5:09 [PATCH v3 1/5] libsmartcols/src/libsmartcols.h.in: add function Shakur Shams Mullick
2014-05-14 5:09 ` [PATCH v3 2/5] libsmartcols/src/libsmartcols.sym: add function symbol Shakur Shams Mullick
@ 2014-05-14 5:09 ` Shakur Shams Mullick
2014-05-14 5:09 ` [PATCH v3 4/5] misc-utils/lsblk.c: add sorting support for lsblk command Shakur Shams Mullick
` (2 subsequent siblings)
4 siblings, 0 replies; 6+ messages in thread
From: Shakur Shams Mullick @ 2014-05-14 5:09 UTC (permalink / raw)
To: util-linux; +Cc: Shakur Shams Mullick
Signed-off-by: Shakur Shams Mullick <shakursmullick@gmail.com>
---
libsmartcols/src/smartcolsP.h | 4 +++-
1 file changed, 3 insertions(+), 1 deletion(-)
diff --git a/libsmartcols/src/smartcolsP.h b/libsmartcols/src/smartcolsP.h
index ebdc929..ee3ee1a 100644
--- a/libsmartcols/src/smartcolsP.h
+++ b/libsmartcols/src/smartcolsP.h
@@ -82,7 +82,9 @@ struct libscols_column {
int flags;
int is_extreme;
char *color; /* default column color */
-
+
+ int (*compare_data)(struct libscols_cell *, struct libscols_cell *, void *); /* comparison function to use*/
+
struct libscols_cell header;
struct list_head cl_columns;
};
--
1.8.3.2
^ permalink raw reply related [flat|nested] 6+ messages in thread
* [PATCH v3 4/5] misc-utils/lsblk.c: add sorting support for lsblk command
2014-05-14 5:09 [PATCH v3 1/5] libsmartcols/src/libsmartcols.h.in: add function Shakur Shams Mullick
2014-05-14 5:09 ` [PATCH v3 2/5] libsmartcols/src/libsmartcols.sym: add function symbol Shakur Shams Mullick
2014-05-14 5:09 ` [PATCH v3 3/5] libsmartcols/src/smartcolsP.h: add field to struct libscols_column Shakur Shams Mullick
@ 2014-05-14 5:09 ` Shakur Shams Mullick
2014-05-14 5:10 ` [PATCH v3 5/5] libsmartcols/src/table_print.c: add sorting for table Shakur Shams Mullick
2014-05-20 13:16 ` [PATCH v3 1/5] libsmartcols/src/libsmartcols.h.in: add function Karel Zak
4 siblings, 0 replies; 6+ messages in thread
From: Shakur Shams Mullick @ 2014-05-14 5:09 UTC (permalink / raw)
To: util-linux; +Cc: Shakur Shams Mullick
Signed-off-by: Shakur Shams Mullick <shakursmullick@gmail.com>
---
misc-utils/lsblk.c | 40 ++++++++++++++++++++++++++++++++++++++--
1 file changed, 38 insertions(+), 2 deletions(-)
diff --git a/misc-utils/lsblk.c b/misc-utils/lsblk.c
index 24853dc..7cc03e8 100644
--- a/misc-utils/lsblk.c
+++ b/misc-utils/lsblk.c
@@ -117,6 +117,12 @@ enum {
LSBLK_TREE = (1 << 4),
};
+enum {
+ sort_none = -1,
+ sort_name,
+ sort_size
+};
+
/* column names */
struct colinfo {
const char *name; /* header */
@@ -1355,6 +1361,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 column property\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);
@@ -1389,7 +1396,9 @@ 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;
@@ -1415,6 +1424,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 },
};
@@ -1439,7 +1449,7 @@ int main(int argc, char *argv[])
memset(lsblk, 0, sizeof(*lsblk));
while((c = getopt_long(argc, argv,
- "abdDe:fhlnmo:OpPiI:rstVS", longopts, NULL)) != -1) {
+ "abdDe:fhlnmo:OpPiI:rstVc:S", longopts, NULL)) != -1) {
err_exclusive_options(c, longopts, excl, excl_st);
@@ -1469,6 +1479,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;
@@ -1576,6 +1593,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;
@@ -1583,16 +1601,34 @@ 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 || sort_flags == sort_size) && !cl)
+ sort_flags = sort_none; /* sorting column not found. Do not sort */
+
+ if(sort_flags != sort_none)
+ {
+ 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_sort_table(lsblk->table, cl);
+ }
scols_print_table(lsblk->table);
--
1.8.3.2
^ permalink raw reply related [flat|nested] 6+ messages in thread
* [PATCH v3 5/5] libsmartcols/src/table_print.c: add sorting for table
2014-05-14 5:09 [PATCH v3 1/5] libsmartcols/src/libsmartcols.h.in: add function Shakur Shams Mullick
` (2 preceding siblings ...)
2014-05-14 5:09 ` [PATCH v3 4/5] misc-utils/lsblk.c: add sorting support for lsblk command Shakur Shams Mullick
@ 2014-05-14 5:10 ` Shakur Shams Mullick
2014-05-20 13:16 ` [PATCH v3 1/5] libsmartcols/src/libsmartcols.h.in: add function Karel Zak
4 siblings, 0 replies; 6+ messages in thread
From: Shakur Shams Mullick @ 2014-05-14 5:10 UTC (permalink / raw)
To: util-linux; +Cc: Shakur Shams Mullick
Signed-off-by: Shakur Shams Mullick <shakursmullick@gmail.com>
---
libsmartcols/src/table_print.c | 21 +++++++++++++++++++++
1 file changed, 21 insertions(+)
diff --git a/libsmartcols/src/table_print.c b/libsmartcols/src/table_print.c
index 24b73f9..fed0305 100644
--- a/libsmartcols/src/table_print.c
+++ b/libsmartcols/src/table_print.c
@@ -694,7 +694,28 @@ 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->compare_data(ca, cb, NULL);
+}
+void scols_sort_table(struct libscols_table *tb, struct libscols_column *cl)
+{
+ assert(tb);
+ list_sort(&tb->tb_lines, comparison_func, cl);
+}
/**
* scols_print_table:
--
1.8.3.2
^ permalink raw reply related [flat|nested] 6+ messages in thread
* Re: [PATCH v3 1/5] libsmartcols/src/libsmartcols.h.in: add function
2014-05-14 5:09 [PATCH v3 1/5] libsmartcols/src/libsmartcols.h.in: add function Shakur Shams Mullick
` (3 preceding siblings ...)
2014-05-14 5:10 ` [PATCH v3 5/5] libsmartcols/src/table_print.c: add sorting for table Shakur Shams Mullick
@ 2014-05-20 13:16 ` Karel Zak
4 siblings, 0 replies; 6+ messages in thread
From: Karel Zak @ 2014-05-20 13:16 UTC (permalink / raw)
To: Shakur Shams Mullick; +Cc: util-linux
On Wed, May 14, 2014 at 11:09:56AM +0600, Shakur Shams Mullick wrote:
> libsmartcols/src/libsmartcols.h.in | 7 ++++++-
> 1 file changed, 6 insertions(+), 1 deletion(-)
OK, I have changed many things and merged all. Now lsblk supports
sort by arbitrary output column, for example:
$ lsblk --sort SIZE
$ lsblk --topology --sort OPT-IO
Thanks.
Karel
--
Karel Zak <kzak@redhat.com>
http://karelzak.blogspot.com
^ permalink raw reply [flat|nested] 6+ messages in thread
end of thread, other threads:[~2014-05-20 13:16 UTC | newest]
Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2014-05-14 5:09 [PATCH v3 1/5] libsmartcols/src/libsmartcols.h.in: add function Shakur Shams Mullick
2014-05-14 5:09 ` [PATCH v3 2/5] libsmartcols/src/libsmartcols.sym: add function symbol Shakur Shams Mullick
2014-05-14 5:09 ` [PATCH v3 3/5] libsmartcols/src/smartcolsP.h: add field to struct libscols_column Shakur Shams Mullick
2014-05-14 5:09 ` [PATCH v3 4/5] misc-utils/lsblk.c: add sorting support for lsblk command Shakur Shams Mullick
2014-05-14 5:10 ` [PATCH v3 5/5] libsmartcols/src/table_print.c: add sorting for table Shakur Shams Mullick
2014-05-20 13:16 ` [PATCH v3 1/5] libsmartcols/src/libsmartcols.h.in: add function Karel Zak
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox