]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
lsblk: add --width option
authorKarel Zak <kzak@redhat.com>
Mon, 12 Oct 2020 10:10:33 +0000 (12:10 +0200)
committerKarel Zak <kzak@redhat.com>
Mon, 12 Oct 2020 10:19:48 +0000 (12:19 +0200)
Addresses: https://github.com/karelzak/util-linux/issues/1160
Signed-off-by: Karel Zak <kzak@redhat.com>
bash-completion/lsblk
misc-utils/lsblk.8
misc-utils/lsblk.c

index e54fc4d5e58078f2d30e1c75533bd47bdf1913fc..f239e1940eb5cc9c3704dfc0bc4bd9d122365b5e 100644 (file)
@@ -79,6 +79,7 @@ _lsblk_module()
                                --topology
                                --scsi
                                --sort
+                               --width
                                --help
                                --version"
                        COMPREPLY=( $(compgen -W "${OPTS[*]}" -- $cur) )
index 891b3f088fee7e8c80f4ddfa85652315ea3ba40f..eeefd82829330e68444ea45d4e28b8f652606cec 100644 (file)
@@ -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
index 1c4bae5bf37d564a0ab009cbeb7dc36067c36709..f398a4b52a9b99d8734490201f056953216f24ba 100644 (file)
@@ -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 <num>    specifies output width as number of characters\n"), out);
        fputs(_(" -x, --sort <column>  sort output by <column>\n"), out);
+       fputs(_(" -z, --zoned          print zone model\n"), out);
        fputs(_("     --sysroot <dir>  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);