Skip to content

Commit

Permalink
zsh: allow setting custom syntax highlighting styles (nix-community#4122
Browse files Browse the repository at this point in the history
)

* zsh: allow setting custom syntax highlighting styles

Custom styles allow overriding the default colors.
Example:
```nix
zsh.syntaxHighlighting.styles.comment = "fg=#6c6c6c";
```

See https://github.com/zsh-users/zsh-syntax-highlighting/blob/master/docs/highlighters.md

* zsh: allow configuring syntax-highlighting package
  • Loading branch information
ThinkChaos authored and Anthony Oleinik committed Jul 24, 2023
1 parent a017794 commit bdf4706
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 6 deletions.
40 changes: 34 additions & 6 deletions modules/programs/zsh.nix
Original file line number Diff line number Diff line change
Expand Up @@ -208,9 +208,30 @@ let
};
};

syntaxHighlightingModule = types.submodule {
options = {
enable = mkEnableOption "zsh syntax highlighting";

package = mkPackageOption pkgs "zsh-syntax-highlighting" { };

styles = mkOption {
type = types.attrsOf types.str;
default = {};
description = ''
Custom styles for syntax highlighting.
See each highlighter's options: <link xlink:href="https://github.com/zsh-users/zsh-syntax-highlighting/blob/master/docs/highlighters.md"/>
'';
};
};
};

in

{
imports = [
(mkRenamedOptionModule [ "programs" "zsh" "enableSyntaxHighlighting" ] [ "programs" "zsh" "syntaxHighlighting" "enable" ])
];

options = {
programs.zsh = {
enable = mkEnableOption "Z shell (Zsh)";
Expand Down Expand Up @@ -312,9 +333,10 @@ in
description = "Enable zsh autosuggestions";
};

enableSyntaxHighlighting = mkOption {
default = false;
description = "Enable zsh syntax highlighting";
syntaxHighlighting = mkOption {
type = syntaxHighlightingModule;
default = {};
description = "Options related to zsh-syntax-highlighting.";
};

historySubstringSearch = mkOption {
Expand Down Expand Up @@ -584,11 +606,17 @@ in
${dirHashesStr}
'')

(optionalString cfg.enableSyntaxHighlighting
(optionalString cfg.syntaxHighlighting.enable
# Load zsh-syntax-highlighting after all custom widgets have been created
# https://github.com/zsh-users/zsh-syntax-highlighting#faq
"source ${pkgs.zsh-syntax-highlighting}/share/zsh-syntax-highlighting/zsh-syntax-highlighting.zsh"
)
''
source ${cfg.syntaxHighlighting.package}/share/zsh-syntax-highlighting/zsh-syntax-highlighting.zsh
${lib.concatStringsSep "\n" (
lib.mapAttrsToList
(name: value: "ZSH_HIGHLIGHT_STYLES[${lib.escapeShellArg name}]=${lib.escapeShellArg value}")
cfg.syntaxHighlighting.styles
)}
'')

(optionalString (cfg.historySubstringSearch.enable or false)
# Load zsh-history-substring-search after zsh-syntax-highlighting
Expand Down
1 change: 1 addition & 0 deletions tests/modules/programs/zsh/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@
zsh-history-ignore-pattern = ./history-ignore-pattern.nix;
zsh-history-substring-search = ./history-substring-search.nix;
zsh-prezto = ./prezto.nix;
zsh-syntax-highlighting = ./syntax-highlighting.nix;
}
23 changes: 23 additions & 0 deletions tests/modules/programs/zsh/syntax-highlighting.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{ config, lib, pkgs, ... }:

with lib;

{
config = {
programs.zsh = {
enable = true;
syntaxHighlighting = {
enable = true;
package = pkgs.hello;
styles.comment = "fg=#6c6c6c";
};
};

test.stubs.zsh = { };

nmt.script = ''
assertFileContains home-files/.zshrc "source ${pkgs.hello}/share/zsh-syntax-highlighting/zsh-syntax-highlighting.zsh"
assertFileContains home-files/.zshrc "ZSH_HIGHLIGHT_STYLES['comment']='fg=#6c6c6c'"
'';
};
}

0 comments on commit bdf4706

Please sign in to comment.