about summary refs log tree commit diff
path: root/modules
diff options
context:
space:
mode:
authoraszlig <aszlig@nix.build>2022-03-24 20:04:05 +0100
committeraszlig <aszlig@nix.build>2022-03-24 20:11:43 +0100
commit25a077f90a0005b519db071a6b5b4d20bd6d2d45 (patch)
tree41816fe039f9bc41cd50e14fbf3a69dc31578710 /modules
parente7c7e67a4692357de28405afe9ec8319780bc36f (diff)
programs/zsh: Add syntax highlighting
I'm already using zsh-fast-syntax-highlighting since weeks via the
configuration.nix and I actually forgot why I used this implementation
rather than one of the others out there.

However, since I'm also using Nushell[1] on a regular basis, I got quite
used to syntax highlighting so that's why I added it to zsh as well.

[1]: https://www.nushell.sh/

Signed-off-by: aszlig <aszlig@nix.build>
Diffstat (limited to 'modules')
-rw-r--r--modules/user/aszlig/programs/zsh/default.nix47
1 files changed, 46 insertions, 1 deletions
diff --git a/modules/user/aszlig/programs/zsh/default.nix b/modules/user/aszlig/programs/zsh/default.nix
index 178fe09a..b0e4f459 100644
--- a/modules/user/aszlig/programs/zsh/default.nix
+++ b/modules/user/aszlig/programs/zsh/default.nix
@@ -56,7 +56,48 @@ in {
       "interactive_comments"
     ];
 
-    programs.zsh.interactiveShellInit = mkAfter ''
+    programs.zsh.interactiveShellInit = let
+      # This is to make sure that all themes use either color names or RGB
+      # colors, otherwise they're not displayed correctly on 24bit color terms.
+      highlighter = pkgs.zsh-fast-syntax-highlighting.overrideAttrs (drv: {
+        fixThemeColors = pkgs.writers.writePython3 "fix-theme-colors" {
+          libraries = [ pkgs.python3Packages.plumbum ];
+          flakeIgnore = [ "E111" "E121" "E302" "E305" ];
+        } ''
+          import re
+          from glob import glob
+          from plumbum.colorlib.styles import Color
+          from configparser import RawConfigParser
+
+          def fix_color(color: str) -> str:
+            if color[:2] in ('fg', 'bg') and color[2:3] in ':=':
+              return color[:3] + fix_color(color[3:])
+            return Color(int(color)).hex_code if color.isdigit() else color
+
+          mainfile = re.sub(
+            r'^(:\s+\''${FAST_HIGHLIGHT_STYLES\[[^]]+\]:=)([^}]+)',
+            lambda m: m.group(1) + fix_color(m.group(2)),
+            open('fast-highlight').read(),
+            flags=re.MULTILINE
+          )
+          open('fast-highlight', 'w').write(mainfile)
+
+          for themefile in glob('themes/*.ini'):
+            parser = RawConfigParser(strict=False)
+            parser.read(themefile)
+
+            for name, section in parser.items():
+              for key, color in section.items():
+                section[key] = ','.join(fix_color(c) for c in color.split(','))
+
+            with open(themefile, 'w') as fp:
+              parser.write(fp)
+        '';
+        postPatch = (drv.postPatch or "") + ''
+          "$fixThemeColors"
+        '';
+      });
+    in mkAfter ''
       export HISTFILE=~/.histfile
       export HISTSIZE=100000
       export SAVEHIST=100000
@@ -113,6 +154,10 @@ in {
       zstyle ':completion:*' verbose true
 
       autoload -Uz zmv
+
+      source ${lib.escapeShellArg "${highlighter}/${
+        "share/zsh/site-functions/fast-syntax-highlighting.plugin.zsh"
+      }"}
     '';
 
     programs.zsh.promptInit = ''