From ccd0582d09dbbda4cf81c003794921d4ca61efe2 Mon Sep 17 00:00:00 2001 From: NotAShelf Date: Mon, 7 Oct 2024 02:10:20 +0300 Subject: [PATCH] visuals: move cellular-automaton to its own module Scuffed impl. --- .../cellular-automaton/cellular-automaton.nix | 60 +++++++++++++++++++ .../visuals/cellular-automaton/config.nix | 39 ++++++++++++ .../visuals/cellular-automaton/default.nix | 6 ++ modules/plugins/visuals/config.nix | 31 ---------- modules/plugins/visuals/default.nix | 1 + modules/plugins/visuals/visuals.nix | 9 --- 6 files changed, 106 insertions(+), 40 deletions(-) create mode 100644 modules/plugins/visuals/cellular-automaton/cellular-automaton.nix create mode 100644 modules/plugins/visuals/cellular-automaton/config.nix create mode 100644 modules/plugins/visuals/cellular-automaton/default.nix diff --git a/modules/plugins/visuals/cellular-automaton/cellular-automaton.nix b/modules/plugins/visuals/cellular-automaton/cellular-automaton.nix new file mode 100644 index 0000000..6d432e7 --- /dev/null +++ b/modules/plugins/visuals/cellular-automaton/cellular-automaton.nix @@ -0,0 +1,60 @@ +{lib, ...}: let + inherit (lib.modules) mkRenamedOptionModule; + inherit (lib.options) mkOption mkEnableOption; + inherit (lib.nvim.types) luaInline; + inherit (lib.nvim.binds) mkMappingOption; + inherit (lib.generators) mkLuaInline; +in { + imports = [ + (mkRenamedOptionModule ["vim" "visuals" "cellularAutomaton"] ["vim" "visuals" "cellular-automaton"]) + ]; + + options.vim.visuals.cellular-automaton = { + enable = mkEnableOption "cellular-automaton to help you cope with stubborn code [cellular-automaton]"; + + mappings = { + makeItRain = mkMappingOption "Make it rain [cellular-automaton]" "fml"; + }; + + animation = { + register = mkEnableOption "registering configured animation(s) automatically" // {default = true;}; + setup = mkOption { + type = luaInline; + default = mkLuaInline '' + local ca_config = { + fps = 50, + name = 'slide', + } + + -- init function is invoked only once at the start + -- config.init = function (grid) + -- + -- end + + -- update function + ca_config.update = function (grid) + for i = 1, #grid do + local prev = grid[i][#(grid[i])] + for j = 1, #(grid[i]) do + grid[i][j], prev = prev, grid[i][j] + end + end + return true + end + ''; + description = '' + Configuration used to generate an animation to be registered. + + The final value for `ca_config` will be used to register a new + animation using `require("cellular-automaton").register_animation(ca_config)` + + ::: {.warning} + `ca_config` **must** eval to a valid Lua table. nvf does not and cannot + perform any kind of validation on your Lua code, so bogus values will + result in errors when the animation is registered. + ::: + ''; + }; + }; + }; +} diff --git a/modules/plugins/visuals/cellular-automaton/config.nix b/modules/plugins/visuals/cellular-automaton/config.nix new file mode 100644 index 0000000..2c0c466 --- /dev/null +++ b/modules/plugins/visuals/cellular-automaton/config.nix @@ -0,0 +1,39 @@ +{ + config, + lib, + ... +}: let + inherit (lib.modules) mkIf; + inherit (lib.strings) optionalString; + inherit (lib.nvim.lua) toLuaObject; + inherit (lib.nvim.dag) entryAnywhere entryAfter; + inherit (lib.nvim.binds) mkBinding; + + cfg = config.vim.visuals.cellular-automaton; +in { + config = mkIf cfg.enable { + vim = { + startPlugins = ["cellular-automaton"]; + + maps.normal = mkBinding cfg.mappings.makeItRain "CellularAutomaton make_it_rain" "Make it rain"; + + pluginRC = { + # XXX: This has no error handling. User can set + # `animation.setup` to a bogus value, and we would + # have an error in our hands. I don't think there + # is a good way to check for errors, so I'm leaving + # it like this under the assumption that the user + # will not mess it up for no reason. + cellular-automaton-anim = entryAnywhere (optionalString cfg.animation.register '' + -- Coerce user animation config into pluginRC + ${toLuaObject cfg.animation.setup} + ''); + + cellular-automaton = entryAfter ["cellular-automaton-anim"] '' + -- Register the animation + require("cellular-automaton").register_animation(ca_config) + ''; + }; + }; + }; +} diff --git a/modules/plugins/visuals/cellular-automaton/default.nix b/modules/plugins/visuals/cellular-automaton/default.nix new file mode 100644 index 0000000..4bb43af --- /dev/null +++ b/modules/plugins/visuals/cellular-automaton/default.nix @@ -0,0 +1,6 @@ +{ + imports = [ + ./config.nix + ./cellular-automaton.nix + ]; +} diff --git a/modules/plugins/visuals/config.nix b/modules/plugins/visuals/config.nix index 7fca240..79222fb 100644 --- a/modules/plugins/visuals/config.nix +++ b/modules/plugins/visuals/config.nix @@ -19,37 +19,6 @@ in { ''; }) - (mkIf cfg.cellularAutomaton.enable { - vim.startPlugins = ["cellular-automaton"]; - - vim.maps.normal = mkBinding cfg.cellularAutomaton.mappings.makeItRain "CellularAutomaton make_it_rain" "Make it rain"; - - vim.pluginRC.cellularAUtomaton = entryAnywhere '' - local config = { - fps = 50, - name = 'slide', - } - - -- init function is invoked only once at the start - -- config.init = function (grid) - -- - -- end - - -- update function - config.update = function (grid) - for i = 1, #grid do - local prev = grid[i][#(grid[i])] - for j = 1, #(grid[i]) do - grid[i][j], prev = prev, grid[i][j] - end - end - return true - end - - require("cellular-automaton").register_animation(config) - ''; - }) - (mkIf cfg.highlight-undo.enable { vim.startPlugins = ["highlight-undo"]; vim.pluginRC.highlight-undo = entryAnywhere '' diff --git a/modules/plugins/visuals/default.nix b/modules/plugins/visuals/default.nix index 57eb329..2fecef2 100644 --- a/modules/plugins/visuals/default.nix +++ b/modules/plugins/visuals/default.nix @@ -1,5 +1,6 @@ { imports = [ + ./cellular-automaton ./cinnamon-nvim ./fidget-nvim ./indent-blankline diff --git a/modules/plugins/visuals/visuals.nix b/modules/plugins/visuals/visuals.nix index 8ce416d..90558ac 100644 --- a/modules/plugins/visuals/visuals.nix +++ b/modules/plugins/visuals/visuals.nix @@ -5,21 +5,12 @@ }: let inherit (lib.options) mkEnableOption mkOption; inherit (lib.types) int bool str; - inherit (lib.nvim.binds) mkMappingOption; cfg = config.vim.visuals; in { options.vim.visuals = { enable = mkEnableOption "Visual enhancements."; - cellularAutomaton = { - enable = mkEnableOption "cellular automaton [cellular-automaton]"; - - mappings = { - makeItRain = mkMappingOption "Make it rain [cellular-automaton]" "fml"; - }; - }; - highlight-undo = { enable = mkEnableOption "highlight undo [highlight-undo]";