at master 2.7 kB view raw
1{ 2 config, 3 pkgs, 4 lib, 5 ... 6}: 7 8let 9 cfg = config.programs.zsh.autosuggestions; 10in 11{ 12 imports = [ 13 (lib.mkRenamedOptionModule 14 [ "programs" "zsh" "enableAutosuggestions" ] 15 [ "programs" "zsh" "autosuggestions" "enable" ] 16 ) 17 ]; 18 19 options.programs.zsh.autosuggestions = { 20 21 enable = lib.mkEnableOption "zsh-autosuggestions"; 22 23 highlightStyle = lib.mkOption { 24 type = lib.types.str; 25 default = "fg=8"; # https://github.com/zsh-users/zsh-autosuggestions/tree/v0.4.3#suggestion-highlight-style 26 description = "Highlight style for suggestions ({fore,back}ground color)"; 27 example = "fg=cyan"; 28 }; 29 30 strategy = lib.mkOption { 31 type = lib.types.listOf ( 32 lib.types.enum [ 33 "history" 34 "completion" 35 "match_prev_cmd" 36 ] 37 ); 38 default = [ "history" ]; 39 description = '' 40 `ZSH_AUTOSUGGEST_STRATEGY` is an array that specifies how suggestions should be generated. 41 The strategies in the array are tried successively until a suggestion is found. 42 There are currently three built-in strategies to choose from: 43 44 - `history`: Chooses the most recent match from history. 45 - `completion`: Chooses a suggestion based on what tab-completion would suggest. (requires `zpty` module) 46 - `match_prev_cmd`: Like `history`, but chooses the most recent match whose preceding history item matches 47 the most recently executed command. Note that this strategy won't work as expected with ZSH options that 48 don't preserve the history order such as `HIST_IGNORE_ALL_DUPS` or `HIST_EXPIRE_DUPS_FIRST`. 49 ''; 50 }; 51 52 async = lib.mkOption { 53 type = lib.types.bool; 54 default = true; 55 description = "Whether to fetch suggestions asynchronously"; 56 example = false; 57 }; 58 59 extraConfig = lib.mkOption { 60 type = lib.types.attrsOf lib.types.str; 61 default = { }; 62 description = "Attribute set with additional configuration values"; 63 example = lib.literalExpression '' 64 { 65 "ZSH_AUTOSUGGEST_BUFFER_MAX_SIZE" = "20"; 66 } 67 ''; 68 }; 69 70 }; 71 72 config = lib.mkIf cfg.enable { 73 74 programs.zsh.interactiveShellInit = '' 75 source ${pkgs.zsh-autosuggestions}/share/zsh-autosuggestions/zsh-autosuggestions.zsh 76 77 export ZSH_AUTOSUGGEST_HIGHLIGHT_STYLE="${cfg.highlightStyle}" 78 export ZSH_AUTOSUGGEST_STRATEGY=(${builtins.concatStringsSep " " cfg.strategy}) 79 ${lib.optionalString (!cfg.async) "unset ZSH_AUTOSUGGEST_USE_ASYNC"} 80 81 ${builtins.concatStringsSep "\n" ( 82 lib.mapAttrsToList (key: value: ''export ${key}="${value}"'') cfg.extraConfig 83 )} 84 ''; 85 86 }; 87}