summary refs log tree commit diff
path: root/nixos/modules/programs/zsh/zsh-autosuggestions.nix
blob: 2e53e907d547a9cd5abf34a3b7e062f887a4c501 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
{ config, pkgs, lib, ... }:

with lib;

let
  cfg = config.programs.zsh.autosuggestions;
in
{
  imports = [
    (mkRenamedOptionModule [ "programs" "zsh" "enableAutosuggestions" ] [ "programs" "zsh" "autosuggestions" "enable" ])
  ];

  options.programs.zsh.autosuggestions = {

    enable = mkEnableOption "zsh-autosuggestions";

    highlightStyle = mkOption {
      type = types.str;
      default = "fg=8"; # https://github.com/zsh-users/zsh-autosuggestions/tree/v0.4.3#suggestion-highlight-style
      description = "Highlight style for suggestions ({fore,back}ground color)";
      example = "fg=cyan";
    };

    strategy = mkOption {
      type = types.listOf (types.enum [ "history" "completion" "match_prev_cmd" ]);
      default = [ "history" ];
      description = ''
        `ZSH_AUTOSUGGEST_STRATEGY` is an array that specifies how suggestions should be generated.
        The strategies in the array are tried successively until a suggestion is found.
        There are currently three built-in strategies to choose from:

        - `history`: Chooses the most recent match from history.
        - `completion`: Chooses a suggestion based on what tab-completion would suggest. (requires `zpty` module)
        - `match_prev_cmd`: Like `history`, but chooses the most recent match whose preceding history item matches
            the most recently executed command. Note that this strategy won't work as expected with ZSH options that
            don't preserve the history order such as `HIST_IGNORE_ALL_DUPS` or `HIST_EXPIRE_DUPS_FIRST`.
      '';
    };

    async = mkOption {
      type = types.bool;
      default = true;
      description = "Whether to fetch suggestions asynchronously";
      example = false;
    };

    extraConfig = mkOption {
      type = with types; attrsOf str;
      default = {};
      description = "Attribute set with additional configuration values";
      example = literalExpression ''
        {
          "ZSH_AUTOSUGGEST_BUFFER_MAX_SIZE" = "20";
        }
      '';
    };

  };

  config = mkIf cfg.enable {

    programs.zsh.interactiveShellInit = ''
      source ${pkgs.zsh-autosuggestions}/share/zsh-autosuggestions/zsh-autosuggestions.zsh

      export ZSH_AUTOSUGGEST_HIGHLIGHT_STYLE="${cfg.highlightStyle}"
      export ZSH_AUTOSUGGEST_STRATEGY=(${concatStringsSep " " cfg.strategy})
      ${optionalString (!cfg.async) "unset ZSH_AUTOSUGGEST_USE_ASYNC"}

      ${concatStringsSep "\n" (mapAttrsToList (key: value: ''export ${key}="${value}"'') cfg.extraConfig)}
    '';

  };
}