From b69bde941a1ae54e6106cbd8de878e6ebf70258d Mon Sep 17 00:00:00 2001 From: CaptainEureka Date: Thu, 25 May 2023 14:16:05 +0200 Subject: [PATCH] plugins/alpha-nvim: add alpha-nvim plugin module + tests (#378) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * plugins/alpha-nvim: add alpha-nvim plugin module + tests * fix: remove telescope from defaults * Update plugins/utils/alpha.nix Co-authored-by: Gaétan Lepage <33058747+GaetanLepage@users.noreply.github.com> * update: remove theme option * update: add default configuration --------- Co-authored-by: Gaétan Lepage <33058747+GaetanLepage@users.noreply.github.com> --- plugins/default.nix | 1 + plugins/utils/alpha.nix | 215 +++++++++++++++++++++ tests/test-sources/plugins/utils/alpha.nix | 64 ++++++ 3 files changed, 280 insertions(+) create mode 100644 plugins/utils/alpha.nix create mode 100644 tests/test-sources/plugins/utils/alpha.nix diff --git a/plugins/default.nix b/plugins/default.nix index 255daf8c..376f4b81 100644 --- a/plugins/default.nix +++ b/plugins/default.nix @@ -68,6 +68,7 @@ ./ui/noice.nix + ./utils/alpha.nix ./utils/comment-nvim.nix ./utils/commentary.nix ./utils/conjure.nix diff --git a/plugins/utils/alpha.nix b/plugins/utils/alpha.nix new file mode 100644 index 00000000..962928bf --- /dev/null +++ b/plugins/utils/alpha.nix @@ -0,0 +1,215 @@ +{ + config, + lib, + pkgs, + ... +}: +with lib; let + cfg = config.plugins.alpha; + + helpers = import ../helpers.nix {inherit lib;}; + + sectionType = types.enum ["group" "padding" "text"]; + + mkAlphaSectionOption = type: + types.submodule { + options = { + type = mkOption { + inherit type; + description = "Type of section"; + }; + + val = mkOption { + type = with types; + oneOf [ + (either str int) + (listOf ( + either + 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"; + }; + }) + )) + ]; + default = null; + description = "Value for section"; + }; + + 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"; + }; + }; + }; + default = {}; + description = "Additional options for the section"; + }; + }; + }; +in { + options = { + plugins.alpha = { + enable = mkEnableOption "alpha"; + + package = helpers.mkPackageOption "alpha" pkgs.vimPlugins.alpha-nvim; + + iconsEnabled = mkOption { + type = types.bool; + description = "Toggle icon support. Installs nvim-web-devicons."; + default = true; + }; + + layout = with helpers; + mkOption { + default = [ + { + type = "padding"; + val = 2; + } + { + type = "text"; + val = [ + " ███╗ ██╗██╗██╗ ██╗██╗ ██╗██╗███╗ ███╗ " + " ████╗ ██║██║╚██╗██╔╝██║ ██║██║████╗ ████║ " + " ██╔██╗ ██║██║ ╚███╔╝ ██║ ██║██║██╔████╔██║ " + " ██║╚██╗██║██║ ██╔██╗ ╚██╗ ██╔╝██║██║╚██╔╝██║ " + " ██║ ╚████║██║██╔╝ ██╗ ╚████╔╝ ██║██║ ╚═╝ ██║ " + " ╚═╝ ╚═══╝╚═╝╚═╝ ╚═╝ ╚═══╝ ╚═╝╚═╝ ╚═╝ " + ]; + opts = { + position = "center"; + hl = "Type"; + }; + } + { + type = "padding"; + val = 2; + } + { + type = "group"; + val = [ + { + shortcut = "e"; + desc = " New file"; + command = "ene "; + } + { + shortcut = "SPC q"; + desc = " Quit Neovim"; + command = ":qa"; + } + ]; + } + { + type = "padding"; + val = 2; + } + { + type = "text"; + val = "Inspiring quote here."; + opts = { + position = "center"; + hl = "Keyword"; + }; + } + ]; + description = "List of sections to layout for the dashboard"; + type = types.listOf (mkAlphaSectionOption sectionType); + }; + }; + }; + + config = with helpers; let + processButton = button: let + stringifyButton = button: ''button("${button.shortcut}", "${button.desc}", "${button.command}")''; + in + mkRaw (stringifyButton button); + + processButtons = attrset: + if attrset.type == "group" + then attrset // {val = builtins.map processButton attrset.val;} + else attrset; + + options = { + theme = cfg.theme; + iconsEnabled = cfg.iconsEnabled; + layout = builtins.map processButtons cfg.layout; + }; + in + mkIf cfg.enable { + extraPlugins = + [ + 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, "") + + local opts = { + position = "center", + shortcut = sc, + cursor = 3, + width = 50, + align_shortcut = "right", + hl_shortcut = "Keyword", + } + 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_ .. "", 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 = { + layout = ${toLuaObject options.layout}, + opts = { + margin = 5, + }, + } + require('alpha').setup(config) + ''; + }; +} diff --git a/tests/test-sources/plugins/utils/alpha.nix b/tests/test-sources/plugins/utils/alpha.nix new file mode 100644 index 00000000..0bb49ce5 --- /dev/null +++ b/tests/test-sources/plugins/utils/alpha.nix @@ -0,0 +1,64 @@ +{pkgs}: { + empty = { + plugins.alpha.enable = true; + }; + + default = { + plugins.alpha = { + enable = true; + iconsEnabled = true; + layout = [ + { + type = "padding"; + val = 2; + } + { + type = "text"; + val = [ + " ███╗ ██╗██╗██╗ ██╗██╗ ██╗██╗███╗ ███╗ " + " ████╗ ██║██║╚██╗██╔╝██║ ██║██║████╗ ████║ " + " ██╔██╗ ██║██║ ╚███╔╝ ██║ ██║██║██╔████╔██║ " + " ██║╚██╗██║██║ ██╔██╗ ╚██╗ ██╔╝██║██║╚██╔╝██║ " + " ██║ ╚████║██║██╔╝ ██╗ ╚████╔╝ ██║██║ ╚═╝ ██║ " + " ╚═╝ ╚═══╝╚═╝╚═╝ ╚═╝ ╚═══╝ ╚═╝╚═╝ ╚═╝ " + ]; + opts = { + position = "center"; + hl = "Type"; + }; + } + { + type = "padding"; + val = 2; + } + { + type = "group"; + val = [ + { + shortcut = "e"; + desc = " New file"; + command = "ene "; + } + { + shortcut = "SPC q"; + desc = " Quit Neovim"; + command = ":qa"; + } + ]; + } + { + type = "padding"; + val = 2; + } + { + type = "text"; + val = "Inspiring quote here."; + opts = { + position = "center"; + hl = "Keyword"; + }; + } + ]; + }; + }; +}