1
0
Fork 0
mirror of https://github.com/nix-community/home-manager synced 2024-11-20 10:09:45 +01:00

zsh: allow setting custom syntax highlighting styles (#4122)

* 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
This commit is contained in:
ThinkChaos 2023-06-28 05:12:58 -04:00 committed by GitHub
parent 9dd107a1d5
commit f5f64ac022
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 58 additions and 6 deletions

View file

@ -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 in
{ {
imports = [
(mkRenamedOptionModule [ "programs" "zsh" "enableSyntaxHighlighting" ] [ "programs" "zsh" "syntaxHighlighting" "enable" ])
];
options = { options = {
programs.zsh = { programs.zsh = {
enable = mkEnableOption "Z shell (Zsh)"; enable = mkEnableOption "Z shell (Zsh)";
@ -312,9 +333,10 @@ in
description = "Enable zsh autosuggestions"; description = "Enable zsh autosuggestions";
}; };
enableSyntaxHighlighting = mkOption { syntaxHighlighting = mkOption {
default = false; type = syntaxHighlightingModule;
description = "Enable zsh syntax highlighting"; default = {};
description = "Options related to zsh-syntax-highlighting.";
}; };
historySubstringSearch = mkOption { historySubstringSearch = mkOption {
@ -584,11 +606,17 @@ in
${dirHashesStr} ${dirHashesStr}
'') '')
(optionalString cfg.enableSyntaxHighlighting (optionalString cfg.syntaxHighlighting.enable
# Load zsh-syntax-highlighting after all custom widgets have been created # Load zsh-syntax-highlighting after all custom widgets have been created
# https://github.com/zsh-users/zsh-syntax-highlighting#faq # 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) (optionalString (cfg.historySubstringSearch.enable or false)
# Load zsh-history-substring-search after zsh-syntax-highlighting # Load zsh-history-substring-search after zsh-syntax-highlighting

View file

@ -7,4 +7,5 @@
zsh-history-ignore-pattern = ./history-ignore-pattern.nix; zsh-history-ignore-pattern = ./history-ignore-pattern.nix;
zsh-history-substring-search = ./history-substring-search.nix; zsh-history-substring-search = ./history-substring-search.nix;
zsh-prezto = ./prezto.nix; zsh-prezto = ./prezto.nix;
zsh-syntax-highlighting = ./syntax-highlighting.nix;
} }

View file

@ -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'"
'';
};
}