]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
lsblk: add --highlight
authorKarel Zak <kzak@redhat.com>
Wed, 20 Sep 2023 12:34:09 +0000 (14:34 +0200)
committerKarel Zak <kzak@redhat.com>
Mon, 20 Nov 2023 21:25:46 +0000 (22:25 +0100)
The new option can colorize lines specified by scols query. The
columns (in the expression) does not have to be in the output.

Signed-off-by: Karel Zak <kzak@redhat.com>
meson.build
misc-utils/Makemodule.am
misc-utils/lsblk.c
misc-utils/lsblk.h

index 55ad30d6ac7b91b9f898a20ee5b15a6c8d90a634..80320d323d7e9f9e0144d21f6e228db8396a9f21 100644 (file)
@@ -2616,6 +2616,7 @@ exe = executable(
   link_with : [lib_common,
                lib_blkid,
                lib_mount,
+               lib_tcolors,
                lib_smartcols],
   dependencies : lib_udev,
   install : true)
index e794d8cb1202433773db509e1ada01263fd2af9b..f55faf3d062fc37ea092583ea110470515a6c143 100644 (file)
@@ -92,7 +92,8 @@ lsblk_SOURCES = \
        misc-utils/lsblk-properties.c \
        misc-utils/lsblk-devtree.c \
        misc-utils/lsblk.h
-lsblk_LDADD = $(LDADD) libblkid.la libmount.la libcommon.la libsmartcols.la
+lsblk_LDADD = $(LDADD) libblkid.la libmount.la libcommon.la \
+               libsmartcols.la libtcolors.la
 lsblk_CFLAGS = $(AM_CFLAGS) -I$(ul_libblkid_incdir) -I$(ul_libmount_incdir) -I$(ul_libsmartcols_incdir)
 if HAVE_UDEV
 lsblk_LDADD += -ludev
index 07b92fb0a7acf960bb7c88757cdc7f7192538738..6050cbf40aadd6c734525b57d7bbd568583319c0 100644 (file)
@@ -52,6 +52,7 @@
 #include "fileutils.h"
 #include "loopdev.h"
 #include "buffer.h"
+#include "colors.h"
 
 #include "lsblk.h"
 
@@ -1361,6 +1362,15 @@ static void device_to_scols(
                device_to_scols(child, dev, tab, ln);
                DBG(DEV, ul_debugobj(dev, "%s <- child done", dev->name));
        }
+
+       if (lsblk->scols_hlighter) {
+               int status = 0;
+
+               if (scols_line_apply_filter(ln, lsblk->scols_hlighter, &status) == 0
+                   && status)
+                       scols_line_set_color(ln, lsblk->hlighter_seq);
+       }
+
 done:
        /* Let's be careful with number of open files */
        ul_path_close_dirfd(dev->sysfs);
@@ -2113,6 +2123,7 @@ static void __attribute__((__noreturn__)) usage(void)
        fputs(_(" -O, --output-all     output all columns\n"), out);
        fputs(_(" -P, --pairs          use key=\"value\" output format\n"), out);
        fputs(_(" -Q, --filter <query> restrict output\n"), out);
+       fputs(_(" -H, --highlight <query> colorize lines maching the query\n"), out);
        fputs(_(" -S, --scsi           output info about SCSI devices\n"), out);
        fputs(_(" -N, --nvme           output info about NVMe devices\n"), out);
        fputs(_(" -v, --virtio         output info about virtio devices\n"), out);
@@ -2166,7 +2177,7 @@ int main(int argc, char *argv[])
        };
        struct lsblk_devtree *tr = NULL;
        int c, status = EXIT_FAILURE;
-       char *outarg = NULL, *f_query = NULL;
+       char *outarg = NULL, *f_query = NULL, *h_query = NULL;
        size_t i;
        unsigned int width = 0;
        int force_tree = 0, has_tree_col = 0;
@@ -2188,6 +2199,7 @@ int main(int argc, char *argv[])
                { "output",     required_argument, NULL, 'o' },
                { "output-all", no_argument,       NULL, 'O' },
                { "filter",     required_argument, NULL, 'Q' },
+               { "highlight",  required_argument, NULL, 'H' },
                { "merge",      no_argument,       NULL, 'M' },
                { "perms",      no_argument,       NULL, 'm' },
                { "noheadings", no_argument,       NULL, 'n' },
@@ -2237,7 +2249,7 @@ int main(int argc, char *argv[])
        lsblk_init_debug();
 
        while((c = getopt_long(argc, argv,
-                               "AabdDzE:e:fhJlNnMmo:OpPQ:iI:rstVvST::w:x:y",
+                               "AabdDzE:e:fH:hJlNnMmo:OpPQ:iI:rstVvST::w:x:y",
                                longopts, NULL)) != -1) {
 
                err_exclusive_options(c, longopts, excl, excl_st);
@@ -2275,6 +2287,9 @@ int main(int argc, char *argv[])
                case 'e':
                        parse_excludes(optarg);
                        break;
+               case 'H':
+                       h_query = optarg;
+                       break;
                case 'J':
                        lsblk->flags |= LSBLK_JSON;
                        break;
@@ -2558,6 +2573,12 @@ int main(int argc, char *argv[])
        if (f_query)
                lsblk->scols_filter = init_scols_filter(lsblk->table, f_query);
 
+       if (h_query && colors_init(UL_COLORMODE_AUTO, "lsblk") > 0) {
+               lsblk->hlighter_seq = color_scheme_get_sequence("highlight-line", UL_COLOR_RED);
+               scols_table_enable_colors(lsblk->table, 1);
+               lsblk->scols_hlighter = init_scols_filter(lsblk->table, h_query);
+       }
+
        tr = lsblk_new_devtree();
        if (!tr)
                err(EXIT_FAILURE, _("failed to allocate device tree"));
@@ -2602,6 +2623,7 @@ leave:
 
        scols_unref_table(lsblk->table);
        scols_unref_filter(lsblk->scols_filter);
+       scols_unref_filter(lsblk->scols_hlighter);
 
        lsblk_mnt_deinit();
        lsblk_properties_deinit();
index 4310ac8594a490f6417fd46e1f0be2b4fcd37ca4..d5eee617f55239cdc10f65d9af32653d850892fc 100644 (file)
@@ -42,6 +42,8 @@ struct lsblk {
        int dedup_id;
 
        struct libscols_filter *scols_filter;
+       struct libscols_filter *scols_hlighter;
+       const char *hlighter_seq;
 
        const char *sysroot;
        int flags;                      /* LSBLK_* */