1{ config, lib, pkgs, ... }:
2
3with lib;
4
5let
6 cfg = config.programs.zsh.syntaxHighlighting;
7in
8{
9 imports = [
10 (mkRenamedOptionModule [ "programs" "zsh" "enableSyntaxHighlighting" ] [ "programs" "zsh" "syntaxHighlighting" "enable" ])
11 (mkRenamedOptionModule [ "programs" "zsh" "syntax-highlighting" "enable" ] [ "programs" "zsh" "syntaxHighlighting" "enable" ])
12 (mkRenamedOptionModule [ "programs" "zsh" "syntax-highlighting" "highlighters" ] [ "programs" "zsh" "syntaxHighlighting" "highlighters" ])
13 (mkRenamedOptionModule [ "programs" "zsh" "syntax-highlighting" "patterns" ] [ "programs" "zsh" "syntaxHighlighting" "patterns" ])
14 ];
15
16 options = {
17 programs.zsh.syntaxHighlighting = {
18 enable = mkEnableOption (lib.mdDoc "zsh-syntax-highlighting");
19
20 highlighters = mkOption {
21 default = [ "main" ];
22
23 # https://github.com/zsh-users/zsh-syntax-highlighting/blob/master/docs/highlighters.md
24 type = types.listOf(types.enum([
25 "main"
26 "brackets"
27 "pattern"
28 "cursor"
29 "root"
30 "line"
31 ]));
32
33 description = lib.mdDoc ''
34 Specifies the highlighters to be used by zsh-syntax-highlighting.
35
36 The following defined options can be found here:
37 https://github.com/zsh-users/zsh-syntax-highlighting/blob/master/docs/highlighters.md
38 '';
39 };
40
41 patterns = mkOption {
42 default = {};
43 type = types.attrsOf types.str;
44
45 example = literalExpression ''
46 {
47 "rm -rf *" = "fg=white,bold,bg=red";
48 }
49 '';
50
51 description = lib.mdDoc ''
52 Specifies custom patterns to be highlighted by zsh-syntax-highlighting.
53
54 Please refer to the docs for more information about the usage:
55 https://github.com/zsh-users/zsh-syntax-highlighting/blob/master/docs/highlighters/pattern.md
56 '';
57 };
58 styles = mkOption {
59 default = {};
60 type = types.attrsOf types.str;
61
62 example = literalExpression ''
63 {
64 "alias" = "fg=magenta,bold";
65 }
66 '';
67
68 description = lib.mdDoc ''
69 Specifies custom styles to be highlighted by zsh-syntax-highlighting.
70
71 Please refer to the docs for more information about the usage:
72 https://github.com/zsh-users/zsh-syntax-highlighting/blob/master/docs/highlighters/main.md
73 '';
74 };
75 };
76 };
77
78 config = mkIf cfg.enable {
79 environment.systemPackages = with pkgs; [ zsh-syntax-highlighting ];
80
81 assertions = [
82 {
83 assertion = length(attrNames cfg.patterns) > 0 -> elem "pattern" cfg.highlighters;
84 message = ''
85 When highlighting patterns, "pattern" needs to be included in the list of highlighters.
86 '';
87 }
88 ];
89
90 programs.zsh.interactiveShellInit = with pkgs;
91 lib.mkAfter (lib.concatStringsSep "\n" ([
92 "source ${zsh-syntax-highlighting}/share/zsh-syntax-highlighting/zsh-syntax-highlighting.zsh"
93 ] ++ optional (length(cfg.highlighters) > 0)
94 "ZSH_HIGHLIGHT_HIGHLIGHTERS=(${concatStringsSep " " cfg.highlighters})"
95 ++ optionals (length(attrNames cfg.patterns) > 0)
96 (mapAttrsToList (
97 pattern: design:
98 "ZSH_HIGHLIGHT_PATTERNS+=('${pattern}' '${design}')"
99 ) cfg.patterns)
100 ++ optionals (length(attrNames cfg.styles) > 0)
101 (mapAttrsToList (
102 styles: design:
103 "ZSH_HIGHLIGHT_STYLES[${styles}]='${design}'"
104 ) cfg.styles)
105 ));
106 };
107}