chroma icon indicating copy to clipboard operation
chroma copied to clipboard

Inconsistent syntax highlighting in bash

Open esoreq opened this issue 5 years ago • 8 comments

Hi I get weird results for specific commands in bash rendering

For example the following code when rendered using (https://swapoff.org/chroma/playground/) vim style will identify some commands but not all.

echo Some text to print out 
cat file
a=10
b=$a
let a+=10
ls
mkdir dir  
mkdir -p dir_root/{dir_b}/{dir_l1,dir_l2} 
cd dir_root
cd "dir name"
cd ~
cp file $output

image

esoreq avatar May 08 '20 07:05 esoreq

Have you tried the different themes with chroma --json?

alecthomas avatar May 08 '20 09:05 alecthomas

So I think this is a problem in how you parse specific tags internally - vim was just used as an example here is dracula

image

esoreq avatar May 08 '20 09:05 esoreq

I'm honestly not sure what the actual complaint is? If you're saying some themes highlight different things, that's correct. It depends on if the theme has assignments for those classes of tokens.

alecthomas avatar May 08 '20 09:05 alecthomas

If you're referring to eg. "mkdir" and "cp" not being highlighted, that's because they are not counted as builtins. See this line for details.

alecthomas avatar May 08 '20 09:05 alecthomas

That is exactly what I was referring to - is there a workaround - i.e. explicitly adding them ?

esoreq avatar May 08 '20 09:05 esoreq

@alecthomas

  • In addition to "mkdir" and "cp", we could also highlight commands like "sudo" & "ls".
  • We could also highlight configuration variables(the one that start with --) in different colour. Example: "dotnet --version".

For reference, you can look into Rogue used in Jekyll.

Implementation of Rogue: https://github.com/rouge-ruby/rouge/blob/442c8f430277889afc6ce0ccbbdbdaec687abc0f/lib/rouge/lexers/shell.rb

Ref: Screenshot Screenshot 2021-06-08 184210

kolappannathan avatar Jun 08 '21 13:06 kolappannathan

@alecthomas * In addition to "mkdir" and "cp", we could also highlight commands like "sudo" & "ls". * We could also highlight configuration variables(the one that start with --) in different colour. Example: "dotnet --version".

It could be interesting, for bash, to separate :

  • keywords
$ k=$(compgen -k); echo $k
if then else elif fi case esac for select while until do done in function time { } ! [[ ]] coproc
  • builtins
$ b=$(compgen -b); echo $b
. : [ alias bg bind break builtin caller cd command compgen complete compopt continue declare dirs disown echo enable eval exec exit export false fc fg getopts hash help history jobs kill let local logout mapfile popd printf pushd pwd read readarray readonly return set shift shopt source suspend test times trap true type typeset ulimit umask unalias unset wait

Maybe builtins and keywords could be considered as the same "thing", for highlighting purpose.

  • regular commands. This is the first word in a line (and after a |, in $(command) constructions, etc..., when not understood as known cases (assignation, builtin, keyword, case patterns, continuation lines, etc...). It is impossible to have a full list of commands, as it is done in Rogue example, by definition.
  • commands options (starting with - or --)

braoult avatar Feb 22 '22 12:02 braoult

Hi everyone, i'm bumping this thread in the hope of a future modification!

dedounet avatar Feb 02 '24 20:02 dedounet