From: Karel Zak Date: Wed, 20 Sep 2023 12:34:09 +0000 (+0200) Subject: lsblk: add --highlight X-Git-Tag: v2.40-rc1~151^2~58 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=c3317cdc76f348910baa48cb397f361cdbd766ea;p=thirdparty%2Futil-linux.git lsblk: add --highlight 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 --- diff --git a/meson.build b/meson.build index 55ad30d6ac..80320d323d 100644 --- a/meson.build +++ b/meson.build @@ -2616,6 +2616,7 @@ exe = executable( link_with : [lib_common, lib_blkid, lib_mount, + lib_tcolors, lib_smartcols], dependencies : lib_udev, install : true) diff --git a/misc-utils/Makemodule.am b/misc-utils/Makemodule.am index e794d8cb12..f55faf3d06 100644 --- a/misc-utils/Makemodule.am +++ b/misc-utils/Makemodule.am @@ -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 diff --git a/misc-utils/lsblk.c b/misc-utils/lsblk.c index 07b92fb0a7..6050cbf40a 100644 --- a/misc-utils/lsblk.c +++ b/misc-utils/lsblk.c @@ -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 restrict output\n"), out); + fputs(_(" -H, --highlight 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(); diff --git a/misc-utils/lsblk.h b/misc-utils/lsblk.h index 4310ac8594..d5eee617f5 100644 --- a/misc-utils/lsblk.h +++ b/misc-utils/lsblk.h @@ -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_* */