marleyos/modules/home/options/shellAbbrs/default.nix

100 lines
2.7 KiB
Nix
Raw Normal View History

2024-11-15 17:59:32 -08:00
{ config, lib, ... }:
let
2024-11-15 17:59:32 -08:00
inherit (lib)
types
mkOption
attrsets
literalExpression
mkIf
;
# https://github.com/nix-community/home-manager/blob/master/modules/programs/fish.nix
abbrModule = types.submodule {
options = {
expansion = mkOption {
type = with types; nullOr str;
default = null;
2024-11-15 17:59:32 -08:00
description = "The command expanded by an abbreviation.";
};
position = mkOption {
type = with types; nullOr str;
default = null;
example = "anywhere";
description = ''
2024-11-15 17:59:32 -08:00
If the position is "command", the abbreviation expands only if the
position is a command. If it is "anywhere", the abbreviation expands
anywhere.
'';
};
regex = mkOption {
type = with types; nullOr str;
default = null;
description = ''
The regular expression pattern matched instead of the literal name.
'';
};
setCursor = mkOption {
type = with types; (either bool str);
default = false;
description = ''
The marker indicates the position of the cursor when the abbreviation
is expanded. When setCursor is true, the marker is set with a default
value of "%".
'';
};
function = mkOption {
type = with types; nullOr str;
default = null;
2024-11-15 17:59:32 -08:00
description = "The fish function expanded instead of a literal string.";
};
};
};
removeFishOnly = attrsets.filterAttrs (
_: v: if (builtins.isAttrs v) then !((v ? regex) || (v ? setCursor) || (v ? function)) else true
);
abbrToAlias = attrsets.mapAttrs (
_: v: if (builtins.isAttrs v) then v.expansion else v
) removeFishOnly;
in
{
options = {
# https://github.com/nix-community/home-manager/blob/master/modules/programs/fish.nix
home.shellAbbrs = mkOption {
type = with types; attrsOf (either str abbrModule);
default = { };
example = literalExpression ''
{
l = "less";
gco = "git checkout";
"-C" = {
position = "anywhere";
expansion = "--color";
};
}
'';
description = ''
2024-11-15 17:59:32 -08:00
An attribute set that maps aliases (the top level attribute names in
this option) to abbreviations. Abbreviations are expanded with the
longer phrase after they are entered.
'';
};
};
config =
let
fishCfg = config.programs.fish;
2024-11-15 17:59:32 -08:00
inherit (config.home) shellAbbrs;
in
{
programs.fish.shellAbbrs = mkIf fishCfg.enable shellAbbrs;
home.shellAliases = mkIf (!fishCfg.enable) (abbrToAlias shellAbbrs);
};
}