plugins/alpha: refactor

This commit is contained in:
Gaetan Lepage 2024-01-12 23:13:04 +01:00 committed by Gaétan Lepage
parent c29a33d38a
commit 9e04eb3c3c
2 changed files with 124 additions and 135 deletions

View file

@ -8,30 +8,33 @@
with lib; let
cfg = config.plugins.alpha;
sectionType = types.enum ["group" "padding" "text"];
mkAlphaSectionOption = type:
types.submodule {
sectionType = types.submodule {
freeformType = with types; attrsOf anything;
options = {
type = mkOption {
inherit type;
type = types.enum [
"button"
"group"
"padding"
"text"
"terminal"
];
description = "Type of section";
};
val = mkOption {
type = with types;
type = with helpers.nixvimTypes;
oneOf [
(either str int)
# "button", "text"
str
# "padding"
int
(listOf (
either
# "text" (list of strings)
str
(submodule {
options = {
shortcut = helpers.mkNullOrOption str "Shortcut for keymap";
desc = helpers.mkNullOrOption str "Description to display for keymap";
command = helpers.mkNullOrOption str "Command to run for keymap";
};
})
# "group"
(attrsOf anything)
))
];
default = null;
@ -39,33 +42,7 @@ with lib; let
};
opts = mkOption {
type = types.submodule {
options = {
spacing = mkOption {
type = types.int;
default = 0;
description = "Spacing between grouped components";
};
hl = mkOption {
type = types.str;
default = "Keyword";
description = "HighlightGroup to apply";
};
position = mkOption {
type = types.str;
default = "center";
description = "How to align section";
};
margin = mkOption {
type = types.int;
default = 0;
description = "Margin for section";
};
};
};
type = with types; attrsOf anything;
default = {};
description = "Additional options for the section";
};
@ -74,9 +51,9 @@ with lib; let
in {
options = {
plugins.alpha = {
enable = mkEnableOption "alpha";
enable = mkEnableOption "alpha-nvim";
package = helpers.mkPackageOption "alpha" pkgs.vimPlugins.alpha-nvim;
package = helpers.mkPackageOption "alpha-nvim" pkgs.vimPlugins.alpha-nvim;
iconsEnabled = mkOption {
type = types.bool;
@ -84,8 +61,22 @@ in {
default = true;
};
theme = mkOption {
type = with helpers.nixvimTypes; nullOr (maybeRaw str);
apply = v:
if isString v
then helpers.mkRaw "require'alpha.themes.${v}'.config"
else v;
default = null;
example = "dashboard";
description = "You can directly use a pre-defined theme.";
};
layout = mkOption {
default = [
type = types.listOf sectionType;
default = [];
description = "List of sections to layout for the dashboard";
example = [
{
type = "padding";
val = 2;
@ -137,27 +128,17 @@ in {
};
}
];
description = "List of sections to layout for the dashboard";
type = types.listOf (mkAlphaSectionOption sectionType);
};
opts = helpers.mkNullOrOption (with types; attrsOf anything) ''
Optional global options.
'';
};
};
config = let
processButton = button: let
stringifyButton = button: ''button("${button.shortcut}", "${button.desc}", "${button.command}")'';
in
helpers.mkRaw (stringifyButton button);
processButtons = attrset:
if attrset.type == "group"
then attrset // {val = builtins.map processButton attrset.val;}
else attrset;
options = {
inherit (cfg) theme iconsEnabled;
layout = builtins.map processButtons cfg.layout;
};
layoutDefined = cfg.layout != [];
themeDefined = cfg.theme != null;
in
mkIf cfg.enable {
extraPlugins =
@ -165,48 +146,36 @@ in {
cfg.package
]
++ (optional cfg.iconsEnabled pkgs.vimPlugins.nvim-web-devicons);
extraConfigLua = ''
local leader = "SPC"
--- @param sc string
--- @param txt string
--- @param keybind string? optional
--- @param keybind_opts table? optional
local function button(sc, txt, keybind, keybind_opts)
local sc_ = sc:gsub("%s", ""):gsub(leader, "<leader>")
local opts = {
position = "center",
shortcut = sc,
cursor = 3,
width = 50,
align_shortcut = "right",
hl_shortcut = "Keyword",
assertions = [
{
assertion = themeDefined || layoutDefined;
message = ''
Nixvim (plugins.alpha): You have to either set a `theme` or define some sections in `layout`.
'';
}
if keybind then
keybind_opts = vim.F.if_nil(keybind_opts, { noremap = true, silent = true, nowait = true })
opts.keymap = { "n", sc_, keybind, keybind_opts }
end
local function on_press()
local key = vim.api.nvim_replace_termcodes(keybind or sc_ .. "<Ignore>", true, false, true)
vim.api.nvim_feedkeys(key, "t", false)
end
return {
type = "button",
val = txt,
on_press = on_press,
opts = opts,
{
assertion = !(themeDefined && layoutDefined);
message = ''
Nixvim (plugins.alpha): You can't define both a `theme` and custom options.
Set `plugins.alpha.theme = null` if you want to configure alpha manually using the `layout` option.
'';
}
end
];
local config = {
layout = ${helpers.toLuaObject options.layout},
opts = {
margin = 5,
},
}
require('alpha').setup(config)
extraConfigLua = let
setupOptions =
if themeDefined
then cfg.theme
else
(with cfg; {
inherit
layout
opts
;
});
in ''
require('alpha').setup(${helpers.toLuaObject setupOptions})
'';
};
}

View file

@ -1,11 +1,22 @@
{
empty = {
plugins.alpha.enable = true;
};
default = {
theme = {
plugins.alpha = {
enable = true;
theme = "dashboard";
};
};
theme-lua = {
plugins.alpha = {
enable = true;
theme.__raw = "require'alpha.themes.startify'.config";
};
};
custom-layout = {
plugins.alpha = {
enable = true;
iconsEnabled = true;
layout = [
{
@ -59,6 +70,15 @@
};
}
];
opts = {
margin = 0;
noautocmd = true;
keymap = {
press = "<CR>";
press_queue = "<M-CR>";
};
};
};
};
}