]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
lsblk: allow to --sort by hidden column
authorKarel Zak <kzak@redhat.com>
Thu, 10 Dec 2015 12:03:46 +0000 (13:03 +0100)
committerKarel Zak <kzak@redhat.com>
Thu, 10 Dec 2015 12:03:46 +0000 (13:03 +0100)
For example:

lsblk -o NAME --sort SIZE

prints NAMEs, but sort by SIZEs.

Signed-off-by: Karel Zak <kzak@redhat.com>
Documentation/TODO
misc-utils/lsblk.c

index 22fa0e06f7f5336e837baa8b5f30bf39caa12c7d..9bd0134e7e2d3439e1ceb06e55be1884936801ec 100644 (file)
@@ -64,13 +64,6 @@ libmount (mount/umount)
 
    (all this already supported by libmount)
 
-libsmartcols
------------
-
- - (!) add SCOLS_FL_HIDDEN flag to make it possible to hide a column. Then we 
-   can use such column in lsblk(8) to only sort lines, but do not print the column.
-
-     lsblk -o NAME --sort SIZE
 
 partx
 -----
index 2604e84c26d8967208e5940302d482789290e592..fff1584d3bcf48f25f4da5a870187ee2d772cb3c 100644 (file)
@@ -214,6 +214,7 @@ struct lsblk {
        unsigned int nodeps:1;          /* don't print slaves/holders */
        unsigned int scsi:1;            /* print only device with HCTL (SCSI) */
        unsigned int paths:1;           /* print devnames with "/dev" prefix */
+       unsigned int sort_hidden:1;     /* sort column not between output columns */
 };
 
 struct lsblk *lsblk;   /* global handler */
@@ -1849,8 +1850,11 @@ int main(int argc, char *argv[])
        if (nexcludes == 0 && nincludes == 0)
                excludes[nexcludes++] = 1;      /* default: ignore RAM disks */
 
-       if (lsblk->sort_id >= 0 && column_id_to_number(lsblk->sort_id) < 0)
-               errx(EXIT_FAILURE, _("the sort column has to be among the output columns"));
+       if (lsblk->sort_id >= 0 && column_id_to_number(lsblk->sort_id) < 0) {
+               /* the sort column is not between output columns -- add as hidden */
+               add_column(columns, ncolumns++, lsblk->sort_id);
+               lsblk->sort_hidden = 1;
+       }
 
        mnt_init_debug(0);
        scols_init_debug(0);
@@ -1876,6 +1880,8 @@ int main(int argc, char *argv[])
 
                if (!(scols_flags & LSBLK_TREE) && id == COL_NAME)
                        fl &= ~SCOLS_FL_TREE;
+               if (lsblk->sort_hidden && lsblk->sort_id == id)
+                       fl |= SCOLS_FL_HIDDEN;
 
                cl = scols_table_new_column(lsblk->table, ci->name, ci->whint, fl);
                if (!cl) {