moar icon indicating copy to clipboard operation
moar copied to clipboard

Syntax highlighting for extension-less files

Open Shayan-To opened this issue 9 months ago • 14 comments

If a file has no extension (like nginx config files), then no syntax highlighting will be given.

My suggestion is to add support for picking the language that Chroma is using for syntax highlighting. This has the added benefit that if a file extension is not correct (like a .log file with json content), then the user can pick the correct language and have syntax highlighting.

BTW, thanks for the awesome tool! I found it to be the best pager out there!

Shayan-To avatar Mar 13 '25 13:03 Shayan-To

Is this what you're after?

cat build.sh | moar --lang=shell

If so, it's already in there :)

walles avatar Mar 15 '25 06:03 walles

What about relying on (in order of importance):

  1. vi modeline
  2. shebang

Lockszmith-GH avatar Apr 04 '25 01:04 Lockszmith-GH

I assume you ran into this when viewing some particular file.

What was the name of that file, and what kind of file was it?

walles avatar Apr 04 '25 04:04 walles

OP stated it was an nginx-config file. Mine is a bash shell file I wrote without the .sh extension. (The name of the file is load-zellij shebang is #! /usr/bin/env bash)

Lockszmith-GH avatar Apr 04 '25 11:04 Lockszmith-GH

@Shayan-To, what command are you using to have Chroma pick the language to use for syntax highlighting?

moar currently uses Chroma's identify-by-file-name functionality, but not Chroma's identify-by-contents functionality.

walles avatar Apr 05 '25 10:04 walles

Sorry to reply so late.

I think it's not really practical to use the --lang argument, as one needs to memorize or check the list of supported languages every time. An interactive option would be more practical, like pressing a key from within moar, and selecting the language from a list.

Detecting the shebang (or other ways of detection) would also be very very helpful, as it will solve a lot of the cases without any need for manual input. (Although it wouldn't work for nginx config files, that I think would require manual input anyway.)

Shayan-To avatar Apr 07 '25 23:04 Shayan-To

@Shayan-To what's the full path to that nginx config file on your machine? I want to see if I can do better regarding figuring out the file type based on its path.

walles avatar Apr 09 '25 04:04 walles

On Linux, the standard directory for Nginx config files is /etc/nginx. The config files have different contexts, but the syntax seems like to be the same.

On Windows though, there is no standard path (as it is portable and can be placed anywhere), but it usually ends with nginx-VERSION/conf/*.

Shayan-To avatar Apr 09 '25 04:04 Shayan-To

Is the interactive language selection a difficult feature to implement? I think it would be a very useful feature. I use the language selection feature of VSCode all the time (so much I have assigned a hot key for it), and have wished for a similar feature in moar many times when syntax highlighting was not available.

Shayan-To avatar Apr 09 '25 18:04 Shayan-To

Is the interactive language selection a difficult feature to implement?

Somewhat, yes.

Something that would be a lot easier might be to ensure your nginx config files get detected properly when you open them in moar on your system.

So on your system, where you sometimes view nginx config files in moar, what is the exact path to one of these nginx config files?

walles avatar Apr 09 '25 19:04 walles

These are the usual nginx config files:

/etc/nginx/conf.d/*.conf
/etc/nginx/modules-available/*.conf
/etc/nginx/modules-enabled/*.conf
/etc/nginx/nginx.conf
/etc/nginx/sites-available/*
/etc/nginx/sites-enabled/*
/etc/nginx/snippets/*.conf

Although I have other files inside /etc/nginx (so for me, /etc/nginx/* is also on the list).

I think all the files inside /etc/nginx have the same general syntax, so /etc/nginx/** might be good as well.

Shayan-To avatar Apr 12 '25 21:04 Shayan-To

Given this information I would say that these two patterns should cover the files on your system:

  • nginx.conf
  • nginx/*/*.conf

The current pattern is just nginx.conf: https://github.com/alecthomas/chroma/blob/b3104143efa8799cb956dda26c1bb637d695fc97/lexers/embedded/nginx_configuration_file.xml#L2-L7

Wanna make a PR for Chroma? I believe the XML format accepts multiple <filename> entries. If you do, post a link here!

walles avatar Apr 13 '25 16:04 walles

  • nginx.conf

  • nginx/*/*.conf

This is not totally correct. Config files inside /etc/nginx/sites-available and /etc/nginx/sites-enabled do not usually have .conf extension (as they are not config files). And for my own setup, I have other files inside /etc/nginx that are not "config" files and so do not have .conf extension.

Shayan-To avatar Apr 14 '25 17:04 Shayan-To

When you say that certain files are not "config" files, what does that mean?

Are those files not in nginx config file format?

Way back, on Apache, sites-enabled and sites-available contained symlinks to config files. I don't remember their extensions, but opening one of those gave you a text that should have been highlighted.

walles avatar May 27 '25 17:05 walles