zsh-syntax-highlighting icon indicating copy to clipboard operation
zsh-syntax-highlighting copied to clipboard

different colors for < and > redirection

Open 400thecat opened this issue 3 years ago • 4 comments

I feel there is huge difference between < and <. One is harmless and reads from file, the other is dangerous and overwrites file.

I would like to have different colors for each to distinguish them. Unfortunately, zsh-syntax-highlighting treats them as same:

164:  [[ $1 == (<0-9>|)(\<|\>)* ]] && [[ $1 != (\<|\>)$'\x28'* ]]

Can you please implement this, or at least suggest what changes to make?

400thecat avatar Aug 18 '21 14:08 400thecat

Changes to make: change the places that set redirection to set some other string. For inclusion, update docs and add fallback mappings (as for argv0). See also the NO_CLOBBER and APPEND_CREATE zshoptions(1). Excuse brevity.

danielshahaf avatar Aug 18 '21 21:08 danielshahaf

I did patch main-highlighter.zsh in version 0.6, and it worked fine. Basically I just added:

   elif (( in_redirection == 2 )); then
        if [[ "$arg" =~ '.*>' ]] ; then
            style=redirection
        else
            style=redirection2
        fi

and defined new color for redirection2. Now I have upgraded to version 0.7, and the code in main-highlighter.zsh looks unrecognizable.

How can I have different colors for < and > ?

400thecat avatar Apr 13 '23 12:04 400thecat

Something like

diff --git a/highlighters/main/main-highlighter.zsh b/highlighters/main/main-highlighter.zsh
index 7ec6249..9728066 100644
--- a/highlighters/main/main-highlighter.zsh
+++ b/highlighters/main/main-highlighter.zsh
@@ -117,6 +117,9 @@ _zsh_highlight_main_calculate_fallback() {
       autodirectory arg0
       arg0_\* arg0
 
+      redirection-in redirection
+      redirection-out redirection
+
       # TODO: Maybe these? —
       #   named-fd file-descriptor
       #   numeric-fd file-descriptor
@@ -747,7 +750,12 @@ _zsh_highlight_main_highlighter_highlight_list()
         _zsh_highlight_main_add_region_highlight $start_pos $end_pos unknown-token
       else
         in_redirection=2
-        _zsh_highlight_main_add_region_highlight $start_pos $end_pos redirection
+        if [[ $arg = *\>* ]]; then
+          style=redirection-out
+        else
+          style=redirection-in
+        fi
+        _zsh_highlight_main_add_region_highlight $start_pos $end_pos $style
       fi
       continue
     elif [[ $arg == '{'${~parameter_name_pattern}'}' ]] && _zsh_highlight_main__is_redirection $args[1]; then

(Docs and test changes are an exercise for the reader.)

phy1729 avatar Apr 15 '23 03:04 phy1729

that works beautifully. thank you

400thecat avatar Apr 15 '23 14:04 400thecat