1{ config, pkgs, lib, ... }:
2
3with lib;
4
5let
6 cfg = config.programs.zsh.autosuggestions;
7in
8{
9 imports = [
10 (mkRenamedOptionModule [ "programs" "zsh" "enableAutosuggestions" ] [ "programs" "zsh" "autosuggestions" "enable" ])
11 ];
12
13 options.programs.zsh.autosuggestions = {
14
15 enable = mkEnableOption "zsh-autosuggestions";
16
17 highlightStyle = mkOption {
18 type = types.str;
19 default = "fg=8"; # https://github.com/zsh-users/zsh-autosuggestions/tree/v0.4.3#suggestion-highlight-style
20 description = "Highlight style for suggestions ({fore,back}ground color)";
21 example = "fg=cyan";
22 };
23
24 strategy = mkOption {
25 type = types.enum [ "history" "match_prev_cmd" ];
26 default = "history";
27 description = ''
28 Set ZSH_AUTOSUGGEST_STRATEGY to choose the strategy for generating suggestions.
29 There are currently two to choose from:
30
31 * history: Chooses the most recent match.
32 * match_prev_cmd: Chooses the most recent match whose preceding history item matches
33 the most recently executed command (more info). Note that this strategy won't work as
34 expected with ZSH options that don't preserve the history order such as
35 HIST_IGNORE_ALL_DUPS or HIST_EXPIRE_DUPS_FIRST.
36 '';
37 };
38
39 extraConfig = mkOption {
40 type = with types; attrsOf str;
41 default = {};
42 description = "Attribute set with additional configuration values";
43 example = literalExample ''
44 {
45 "ZSH_AUTOSUGGEST_BUFFER_MAX_SIZE" = "20";
46 }
47 '';
48 };
49
50 };
51
52 config = mkIf cfg.enable {
53
54 programs.zsh.interactiveShellInit = ''
55 source ${pkgs.zsh-autosuggestions}/share/zsh-autosuggestions/zsh-autosuggestions.zsh
56
57 export ZSH_AUTOSUGGEST_HIGHLIGHT_STYLE="${cfg.highlightStyle}"
58 export ZSH_AUTOSUGGEST_STRATEGY=("${cfg.strategy}")
59
60 ${concatStringsSep "\n" (mapAttrsToList (key: value: ''export ${key}="${value}"'') cfg.extraConfig)}
61 '';
62
63 };
64}