doronbehar / pistol

General purpose file previewer designed for Ranger, Lf to make scope.sh redundant

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Feature Request: Preview Rule Based on Extension

smhmd opened this issue · comments

I wish to preview markdown files with mdcat, but their mimetype is text/plain, same as .txt files (mdcat ruins these). Is there a way to be more specific on this front?

Interesting idea @alivefromupstairs . How would you expect it to be implemented ideally? I've had 2.5 ideas so far:

  1. Using a separate configuration file for preview programs selection based on file name extensions (I'm not sure it'll be easy to implement - need to think about precedence between regexes in both configs...)
  2. Match a regex of a file name extension in the same pistol.conf - need to think how will we guarantee there won't be or rather, handle collisions between filenames and mime types...
  3. Perhaps to use the same approach as in idea # 2, but use some sort of prefix to tell pistol that this is a filename regex and not a mime type regex.

The way I've seen it done is fairly straightforward and possibly incompatible with pistol and its goals, but worth taking a look at. for instance: mimi, an xdg-open replacement, has this:

inode/directory: $TERMINAL -e lf
image/: sxiv
image/gif: sxiv -a # this overrides the previous line
md: $TERMINAL -e mdcat # this matches to .md files directly

pistol tries to be as efficient as possible and bails out on any match, so typing rules would be better and collisions/priority should be left to the user:

ext md mdcat %s
text/html w3m -dump %s
text/* cat %s

so, I guess you will have to acquire files' mimetype and extension and ream through rules in the config, matching against the mimetype unless the ext keyword is used, you match against the extension.

I don't think a second config file is necessary. And I think this gokcehan/lf#277 could nullify the need for micro optimizations.

so typing rules would be better and collisions/priority should be left to the user [...]

I agree - this will be most reasonable in the manner of consistency: The first mime-type regex / file-name regex is used.

Your example config line:

ext md mdcat %s

Suggests that you would ideally use the ext keyword to indicate that the following word is an extension regex and not a mime type regex. Cool 👍. I have one small implementation note: I think it'd be better to stick to a file name regex matching so even files in certain directories could be used and naturally just extensions could be used as well. Do you agree?

I think it'd be better to stick to a file name regex matching so even files in certain directories could be used.

If I understand that correctly, the rule would include file name and potentially directory name?

That would be great if one wants to use syntax highlighted previewing inside coding directories but resort to cat on others, or even disable previewing all together.

It's a good idea, but if you, however, decided against it, I think .ext ext2txt %s would be a nicer, cleaner syntax with the . or * being the keyword.

BTW for the record, pistol's native text files previewer, supports syntax highlighting for markdown files, although they are detected as text/plain by libmagic.

I didn't know that, what does it use? It's kinda fast, albeit ugly (uses dark red for strings 😕)