]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
lsblk: add --hyperlink command line option
authorKarel Zak <kzak@redhat.com>
Mon, 2 Dec 2024 11:02:38 +0000 (12:02 +0100)
committerKarel Zak <kzak@redhat.com>
Mon, 9 Dec 2024 09:25:06 +0000 (10:25 +0100)
Signed-off-by: Karel Zak <kzak@redhat.com>
bash-completion/lsblk
misc-utils/lsblk-mnt.c
misc-utils/lsblk.8.adoc
misc-utils/lsblk.c
misc-utils/lsblk.h

index 41c2b2c8fee40ba6879afae56d97c2e48e458d75..ff826858137a4fd664f9026394a9fbd761d8595a 100644 (file)
@@ -71,6 +71,7 @@ _lsblk_module()
                                --fs
                                --filter
                                --highlight
+                               --hyperlink
                                --ct
                                --ct-filter
                                --help
index 81ded5ea23fe82d779729f09e1af5a9620475a04..3217e3f554455f688bcb383e77d85b6b260bf7b6 100644 (file)
@@ -64,6 +64,9 @@ static void add_filesystem(struct lsblk_device *dev, struct libmnt_fs *fs)
        dev->fss[dev->nfss] = fs;
        dev->nfss++;
        dev->is_mounted = 1;
+
+       if (mnt_fs_is_swaparea(fs))
+               dev->is_swap = 1;
 }
 
 struct libmnt_fs **lsblk_device_get_filesystems(struct lsblk_device *dev, size_t *n)
@@ -162,8 +165,10 @@ const char *lsblk_device_get_mountpoint(struct lsblk_device *dev)
                        }
                }
        }
-       if (mnt_fs_is_swaparea(fs))
+       if (mnt_fs_is_swaparea(fs)) {
+               dev->is_swap = 1;
                return "[SWAP]";
+       }
        return mnt_fs_get_target(fs);
 }
 
index 308c71e3f628d94922902cc1d2ab8dde99b17259..d92d3fb44c7d9ef03ec58e370dd55f36204d2f67 100644 (file)
@@ -61,6 +61,9 @@ Exclude the devices specified by the comma-separated _list_ of major device numb
 *-f*, *--fs*::
 Output info about filesystems. This option is equivalent to *-o NAME,FSTYPE,FSVER,LABEL,UUID,FSAVAIL,FSUSE%,MOUNTPOINTS*. The authoritative information about filesystems and raids is provided by the *blkid*(8) command.
 
+*--hyperlink*[=_mode_]::
+Print mountpoint paths as terminal hyperlinks. The _mode_ can be set to "always", "never", or "auto". The optional argument _when_ can be set to "auto", "never", or "always". If the _when_ argument is omitted, it will default to "auto". The "auto" setting means that hyperlinks will only be used if the output is on a terminal.
+
 *-I*, *--include* _list_::
 Include devices specified by the comma-separated _list_ of major device numbers. The filter is applied to the top-level devices only. This may be confusing for *--list* output format where hierarchy of the devices is not obvious.
 
index cc1ae98b6cc541a9e2151482cc59b82461893267..6a8dc5011bb8faa7923578a89df8323882fff9b1 100644 (file)
@@ -1274,10 +1274,14 @@ static void device_fill_scols_cell(struct lsblk_device *dev,
        ce = scols_line_get_cell(ln, colnum);
        if (!ce)
                return;
+
        rc = datasiz ? scols_cell_refer_memory(ce, data, datasiz)
                     : scols_cell_refer_data(ce, data);
        if (rc)
                err(EXIT_FAILURE, _("failed to add output data"));
+
+       if (lsblk->uri && (id == COL_TARGETS || id == COL_TARGET) && dev->is_swap)
+               scols_cell_disable_uri(ce, 1);
 }
 
 static int filter_filler_cb(
@@ -2407,7 +2411,8 @@ int main(int argc, char *argv[])
                OPT_COUNTER_FILTER,
                OPT_COUNTER,
                OPT_HIGHLIGHT,
-               OPT_PROPERTIES_BY
+               OPT_PROPERTIES_BY,
+               OPT_HYPERLINK
        };
 
        static const struct option longopts[] = {
@@ -2424,6 +2429,7 @@ int main(int argc, char *argv[])
                { "output-all", no_argument,       NULL, 'O' },
                { "filter",     required_argument, NULL, 'Q' },
                { "highlight",  required_argument, NULL, OPT_HIGHLIGHT },
+               { "hyperlink",  optional_argument, NULL, OPT_HYPERLINK },
                { "merge",      no_argument,       NULL, 'M' },
                { "perms",      no_argument,       NULL, 'm' },
                { "noheadings", no_argument,       NULL, 'n' },
@@ -2667,6 +2673,11 @@ int main(int argc, char *argv[])
                        if (lsblk_set_properties_method(optarg) < 0)
                                errtryhelp(EXIT_FAILURE);
                        break;
+               case OPT_HYPERLINK:
+                       if (hyperlinkwanted_or_err(optarg,
+                                       _("invalid hyperlink argument")))
+                               lsblk->uri = xgethosturi(NULL);
+                       break;
                case 'H':
                        collist = 1;
                        break;
@@ -2793,6 +2804,9 @@ int main(int argc, char *argv[])
                if (fl & SCOLS_FL_WRAP)
                        scols_column_set_wrapfunc(cl, NULL, scols_wrapzero_nextchunk, NULL);
 
+               if (lsblk->uri && (id == COL_TARGET || id == COL_TARGETS))
+                       scols_column_set_uri(cl, lsblk->uri);
+
                set_column_type(ci, cl, fl);
        }
 
index b2b9b40dcd01d4abaf4485ca469b07cfb16bb5b4..90d2df1a135c10072a89caa7172b5518b368ef94 100644 (file)
@@ -60,6 +60,7 @@ struct lsblk {
        size_t ncts;                            /* number of ct filters */
 
        const char *sysroot;
+       char *uri;
        int flags;                      /* LSBLK_* */
 
        int properties_by[__LSBLK_NMETHODS];