From 7636d906efc4edcccccfeff61ae42d93148d99b7 Mon Sep 17 00:00:00 2001 From: Karel Zak Date: Mon, 12 Oct 2020 12:10:33 +0200 Subject: [PATCH] lsblk: add --width option Addresses: https://github.com/karelzak/util-linux/issues/1160 Signed-off-by: Karel Zak --- bash-completion/lsblk | 1 + misc-utils/lsblk.8 | 9 +++++++++ misc-utils/lsblk.c | 14 ++++++++++++-- 3 files changed, 22 insertions(+), 2 deletions(-) diff --git a/bash-completion/lsblk b/bash-completion/lsblk index e54fc4d5e5..f239e1940e 100644 --- a/bash-completion/lsblk +++ b/bash-completion/lsblk @@ -79,6 +79,7 @@ _lsblk_module() --topology --scsi --sort + --width --help --version" COMPREPLY=( $(compgen -W "${OPTS[*]}" -- $cur) ) diff --git a/misc-utils/lsblk.8 b/misc-utils/lsblk.8 index 891b3f088f..eeefd82829 100644 --- a/misc-utils/lsblk.8 +++ b/misc-utils/lsblk.8 @@ -157,6 +157,15 @@ This option is equivalent to .BR \-V , " \-\-version" Display version information and exit. .TP +.BR \-w , " \-\-width " \fInumber\fP +Specifies output width as a number of characters. The default is the number of +the terminal columns, and if not executed on a terminal, then output width is not +restricted at all by default. This option also forces lsblk to assume that terminal +control characters and unsafe characters are not allowed. The expected use-case is +for example when lsblk used by +.BR watch (1) +command. +.TP .BR \-x , " \-\-sort " \fIcolumn\fP Sort output lines by \fIcolumn\fP. This option enables \fB\-\-list\fR output format by default. It is possible to use the option \fI\-\-tree\fP to force tree-like output and diff --git a/misc-utils/lsblk.c b/misc-utils/lsblk.c index 1c4bae5bf3..f398a4b52a 100644 --- a/misc-utils/lsblk.c +++ b/misc-utils/lsblk.c @@ -1804,8 +1804,9 @@ static void __attribute__((__noreturn__)) usage(void) fputs(_(" -r, --raw use raw output format\n"), out); fputs(_(" -s, --inverse inverse dependencies\n"), out); fputs(_(" -t, --topology output info about topology\n"), out); - fputs(_(" -z, --zoned print zone model\n"), out); + fputs(_(" -w, --width specifies output width as number of characters\n"), out); fputs(_(" -x, --sort sort output by \n"), out); + fputs(_(" -z, --zoned print zone model\n"), out); fputs(_(" --sysroot use specified directory as system root\n"), out); fputs(USAGE_SEPARATOR, out); printf(USAGE_HELP_OPTIONS(22)); @@ -1839,6 +1840,7 @@ int main(int argc, char *argv[]) int c, status = EXIT_FAILURE; char *outarg = NULL; size_t i; + unsigned int width = 0; int force_tree = 0, has_tree_col = 0; enum { @@ -1874,6 +1876,7 @@ int main(int argc, char *argv[]) { "sysroot", required_argument, NULL, OPT_SYSROOT }, { "tree", optional_argument, NULL, 'T' }, { "version", no_argument, NULL, 'V' }, + { "width", required_argument, NULL, 'w' }, { NULL, 0, NULL, 0 }, }; @@ -1901,7 +1904,7 @@ int main(int argc, char *argv[]) lsblk_init_debug(); while((c = getopt_long(argc, argv, - "abdDzE:e:fhJlnMmo:OpPiI:rstVST::x:", longopts, NULL)) != -1) { + "abdDzE:e:fhJlnMmo:OpPiI:rstVST::w:x:", longopts, NULL)) != -1) { err_exclusive_options(c, longopts, excl, excl_st); @@ -2027,6 +2030,9 @@ int main(int argc, char *argv[]) break; errtryhelp(EXIT_FAILURE); break; + case 'w': + width = strtou32_or_err(optarg, _("invalid output width number argument")); + break; case 'x': lsblk->flags &= ~LSBLK_TREE; /* disable the default */ lsblk->sort_id = column_name_to_id(optarg, strlen(optarg)); @@ -2105,6 +2111,10 @@ int main(int argc, char *argv[]) if (lsblk->flags & LSBLK_JSON) scols_table_set_name(lsblk->table, "blockdevices"); + if (width) { + scols_table_set_termwidth(lsblk->table, width); + scols_table_set_termforce(lsblk->table, SCOLS_TERMFORCE_ALWAYS); + } for (i = 0; i < ncolumns; i++) { struct colinfo *ci = get_column_info(i); -- 2.47.2