nixos/zsh-syntax-highlighting: refactor
This commit is contained in:
parent
423d96e4f2
commit
7002ca7e1c
|
@ -5,74 +5,74 @@ with lib;
|
||||||
let
|
let
|
||||||
cfg = config.programs.zsh.syntaxHighlighting;
|
cfg = config.programs.zsh.syntaxHighlighting;
|
||||||
in
|
in
|
||||||
{
|
{
|
||||||
options = {
|
options = {
|
||||||
programs.zsh.syntaxHighlighting = {
|
programs.zsh.syntaxHighlighting = {
|
||||||
enable = mkEnableOption "zsh-syntax-highlighting";
|
enable = mkEnableOption "zsh-syntax-highlighting";
|
||||||
|
|
||||||
highlighters = mkOption {
|
highlighters = mkOption {
|
||||||
default = [ "main" ];
|
default = [ "main" ];
|
||||||
|
|
||||||
# https://github.com/zsh-users/zsh-syntax-highlighting/blob/master/docs/highlighters.md
|
# https://github.com/zsh-users/zsh-syntax-highlighting/blob/master/docs/highlighters.md
|
||||||
type = types.listOf(types.enum([
|
type = types.listOf(types.enum([
|
||||||
"main"
|
"main"
|
||||||
"brackets"
|
"brackets"
|
||||||
"pattern"
|
"pattern"
|
||||||
"cursor"
|
"cursor"
|
||||||
"root"
|
"root"
|
||||||
"line"
|
"line"
|
||||||
]));
|
]));
|
||||||
|
|
||||||
description = ''
|
description = ''
|
||||||
Specifies the highlighters to be used by zsh-syntax-highlighting.
|
Specifies the highlighters to be used by zsh-syntax-highlighting.
|
||||||
|
|
||||||
The following defined options can be found here:
|
The following defined options can be found here:
|
||||||
https://github.com/zsh-users/zsh-syntax-highlighting/blob/master/docs/highlighters.md
|
https://github.com/zsh-users/zsh-syntax-highlighting/blob/master/docs/highlighters.md
|
||||||
'';
|
'';
|
||||||
};
|
};
|
||||||
|
|
||||||
patterns = mkOption {
|
patterns = mkOption {
|
||||||
default = {};
|
default = {};
|
||||||
type = types.attrsOf types.string;
|
type = types.attrsOf types.string;
|
||||||
|
|
||||||
example = literalExample ''
|
example = literalExample ''
|
||||||
{
|
{
|
||||||
"rm -rf *" = "fg=white,bold,bg=red";
|
"rm -rf *" = "fg=white,bold,bg=red";
|
||||||
}
|
}
|
||||||
'';
|
'';
|
||||||
|
|
||||||
description = ''
|
description = ''
|
||||||
Specifies custom patterns to be highlighted by zsh-syntax-highlighting.
|
Specifies custom patterns to be highlighted by zsh-syntax-highlighting.
|
||||||
|
|
||||||
Please refer to the docs for more information about the usage:
|
Please refer to the docs for more information about the usage:
|
||||||
https://github.com/zsh-users/zsh-syntax-highlighting/blob/master/docs/highlighters/pattern.md
|
https://github.com/zsh-users/zsh-syntax-highlighting/blob/master/docs/highlighters/pattern.md
|
||||||
'';
|
'';
|
||||||
};
|
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
};
|
||||||
|
|
||||||
config = mkIf cfg.enable {
|
config = mkIf cfg.enable {
|
||||||
environment.systemPackages = with pkgs; [ zsh-syntax-highlighting ];
|
environment.systemPackages = with pkgs; [ zsh-syntax-highlighting ];
|
||||||
|
|
||||||
programs.zsh.interactiveShellInit = with pkgs; with builtins; ''
|
assertions = [
|
||||||
source ${zsh-syntax-highlighting}/share/zsh-syntax-highlighting/zsh-syntax-highlighting.zsh
|
{
|
||||||
|
assertion = length(attrNames cfg.patterns) > 0 -> elem "pattern" cfg.highlighters;
|
||||||
|
message = ''
|
||||||
|
When highlighting patterns, "pattern" needs to be included in the list of highlighters.
|
||||||
|
'';
|
||||||
|
}
|
||||||
|
];
|
||||||
|
|
||||||
${optionalString (length(cfg.highlighters) > 0)
|
programs.zsh.interactiveShellInit = with pkgs;
|
||||||
"ZSH_HIGHLIGHT_HIGHLIGHTERS=(${concatStringsSep " " cfg.highlighters})"
|
lib.concatStringsSep "\n" ([
|
||||||
}
|
"source ${zsh-syntax-highlighting}/share/zsh-syntax-highlighting/zsh-syntax-highlighting.zsh"
|
||||||
|
] ++ optional (length(cfg.highlighters) > 0)
|
||||||
${let
|
"ZSH_HIGHLIGHT_HIGHLIGHTERS=(${concatStringsSep " " cfg.highlighters})"
|
||||||
n = attrNames cfg.patterns;
|
++ optionals (length(attrNames cfg.patterns) > 0)
|
||||||
in
|
(mapAttrsToList (
|
||||||
optionalString (length(n) > 0)
|
pattern: design:
|
||||||
(assert(elem "pattern" cfg.highlighters); (foldl (
|
"ZSH_HIGHLIGHT_PATTERNS+=('${pattern}' '${design}')"
|
||||||
a: b:
|
) cfg.patterns)
|
||||||
''
|
);
|
||||||
${a}
|
};
|
||||||
ZSH_HIGHLIGHT_PATTERNS+=('${b}' '${attrByPath [b] "" cfg.patterns}')
|
}
|
||||||
''
|
|
||||||
) "") n)
|
|
||||||
}
|
|
||||||
'';
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
Loading…
Reference in New Issue