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 with lib; let
cfg = config.plugins.alpha; cfg = config.plugins.alpha;
sectionType = types.enum ["group" "padding" "text"]; sectionType = types.submodule {
freeformType = with types; attrsOf anything;
mkAlphaSectionOption = type:
types.submodule {
options = { options = {
type = mkOption { type = mkOption {
inherit type; type = types.enum [
"button"
"group"
"padding"
"text"
"terminal"
];
description = "Type of section"; description = "Type of section";
}; };
val = mkOption { val = mkOption {
type = with types; type = with helpers.nixvimTypes;
oneOf [ oneOf [
(either str int) # "button", "text"
str
# "padding"
int
(listOf ( (listOf (
either either
# "text" (list of strings)
str str
(submodule { # "group"
options = { (attrsOf anything)
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";
};
})
)) ))
]; ];
default = null; default = null;
@ -39,33 +42,7 @@ with lib; let
}; };
opts = mkOption { opts = mkOption {
type = types.submodule { type = with types; attrsOf anything;
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";
};
};
};
default = {}; default = {};
description = "Additional options for the section"; description = "Additional options for the section";
}; };
@ -74,9 +51,9 @@ with lib; let
in { in {
options = { options = {
plugins.alpha = { 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 { iconsEnabled = mkOption {
type = types.bool; type = types.bool;
@ -84,8 +61,22 @@ in {
default = true; 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 { layout = mkOption {
default = [ type = types.listOf sectionType;
default = [];
description = "List of sections to layout for the dashboard";
example = [
{ {
type = "padding"; type = "padding";
val = 2; val = 2;
@ -93,12 +84,12 @@ in {
{ {
type = "text"; type = "text";
val = [ val = [
" " " "
" " " "
" " " "
" " " "
" " " "
" " " "
]; ];
opts = { opts = {
position = "center"; position = "center";
@ -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 config = let
processButton = button: let layoutDefined = cfg.layout != [];
stringifyButton = button: ''button("${button.shortcut}", "${button.desc}", "${button.command}")''; themeDefined = cfg.theme != null;
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;
};
in in
mkIf cfg.enable { mkIf cfg.enable {
extraPlugins = extraPlugins =
@ -165,48 +146,36 @@ in {
cfg.package cfg.package
] ]
++ (optional cfg.iconsEnabled pkgs.vimPlugins.nvim-web-devicons); ++ (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 = { assertions = [
position = "center", {
shortcut = sc, assertion = themeDefined || layoutDefined;
cursor = 3, message = ''
width = 50, Nixvim (plugins.alpha): You have to either set a `theme` or define some sections in `layout`.
align_shortcut = "right", '';
hl_shortcut = "Keyword",
} }
if keybind then {
keybind_opts = vim.F.if_nil(keybind_opts, { noremap = true, silent = true, nowait = true }) assertion = !(themeDefined && layoutDefined);
opts.keymap = { "n", sc_, keybind, keybind_opts } message = ''
end 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.
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,
} }
end ];
local config = { extraConfigLua = let
layout = ${helpers.toLuaObject options.layout}, setupOptions =
opts = { if themeDefined
margin = 5, then cfg.theme
}, else
} (with cfg; {
require('alpha').setup(config) inherit
layout
opts
;
});
in ''
require('alpha').setup(${helpers.toLuaObject setupOptions})
''; '';
}; };
} }

View file

@ -1,11 +1,22 @@
{ {
empty = { theme = {
plugins.alpha.enable = true;
};
default = {
plugins.alpha = { plugins.alpha = {
enable = true; 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; iconsEnabled = true;
layout = [ layout = [
{ {
@ -15,12 +26,12 @@
{ {
type = "text"; type = "text";
val = [ val = [
" " " "
" " " "
" " " "
" " " "
" " " "
" " " "
]; ];
opts = { opts = {
position = "center"; position = "center";
@ -59,6 +70,15 @@
}; };
} }
]; ];
opts = {
margin = 0;
noautocmd = true;
keymap = {
press = "<CR>";
press_queue = "<M-CR>";
};
};
}; };
}; };
} }