Welcome to Vifm Q&A, where you can ask questions about using Vifm. Registration is optional, anonymous posts are moderated. E-mail and GitHub logins are enabled.
0 votes
in vifm by
edited by

Considering this question and its answer, one may be tempted to write something like this to configure a viewer for all text files:

fileviewer <text/*> /my/text/viewer %c

However, at least when vifm is built with GLib, there is a number of specific MIME types for particular kinds of plain text outside of text/ category:

┌── File Information ───────────────────┐
│                                       │
│ Path:        /home/intelfx            │
│ Name:        file.json                │
│ Size:        3.9 K (3974 bytes)       │
│ Type:        JSON text data           │
│ Mime Type:   application/json         │
│ Hard Links:  1                        │
│ Modified:    Вт, 06 фев 2024 11:10:38 │
│ Accessed:    Пт, 14 июн 2024 13:56:49 │
│ Changed:     Пт, 14 июн 2024 14:10:17 │
│ Permissions: -rw-r--r-- (644)         │
│ Owner:       intelfx (1000)           │
│ Group:       intelfx (1000)           │
└───────────────────────────────────────┘

There is quite a number of such MIME types (the second filtering is here to count non-<text/> types) inheriting from <text/>:

$ rg 'sub-class-of type="text/.+"' /usr/share/mime | rg -v '/usr/share/mime/text' | wc -l
338

There are also inheritance chains of length > 1:

$ rg 'sub-class-of type="text/.+"' /usr/share/mime -l | rg '/usr/share/mime/(.+)\.xml' -or '$1' | xargs -I{} rg "-e=sub-class-of type=\"{}\"" /usr/share/mime | wc -l
242

Is there a reliable way to identify all such files within :fileviewer/:filetype?

1 Answer

0 votes
by

You'd have to enumerate all of them as a comma-separated list as a workaround, but there is no generic reliable way of doing it. And I'm not sure there can be until mimetypes start to indicate text vs. binary formats and there will be a way to query that information.

by

Technically, there is a way: for a given MIME type, you need to check if there is a (potentially transitive) sub-class-of relationship between the given MIME type and text/plain, or (better) any text/* type.

I'm just wondering if something like that exists in Vifm or can be reasonably added (I only looked at the GLib API and very hastily, so I don't know if any other libraries you use can do this).

by

Technically, there is a way:

I now see there is /usr/share/mime/subclasses and the whole /usr/share/mime isn't something specific to GLib, but comes from freedesktop.org as *.desktop files do. So Vifm could parse that file (or mime.cache if it's convenient) and check the chain of classes against a mimetype matcher.

I'm just wondering if something like that exists in Vifm or can be reasonably added (I only looked at the GLib API and very hastily, so I don't know if any other libraries you use can do this).

The other ones are libmagic or file application and I don't think they deal with classes (file can print charset, so I might be wrong).

...