From ada99e57635b9ec60fab30d361927820fa0f890d Mon Sep 17 00:00:00 2001 From: Gaetan Lepage Date: Tue, 19 Sep 2023 11:37:33 +0200 Subject: [PATCH] plugins/notify: add missing options + test --- plugins/utils/notify.nix | 185 ++++++++++++++------ tests/test-sources/plugins/utils/notify.nix | 31 ++++ 2 files changed, 165 insertions(+), 51 deletions(-) create mode 100644 tests/test-sources/plugins/utils/notify.nix diff --git a/plugins/utils/notify.nix b/plugins/utils/notify.nix index 41f1a4d1..037d8b2f 100644 --- a/plugins/utils/notify.nix +++ b/plugins/utils/notify.nix @@ -7,67 +7,150 @@ with lib; let cfg = config.plugins.notify; helpers = import ../helpers.nix {inherit lib;}; - icon = mkOption { - type = types.nullOr types.str; - default = null; - }; in { - options.plugins.notify = { - enable = mkEnableOption "notify"; + options.plugins.notify = + helpers.extraOptionsOptions + // { + enable = mkEnableOption "nvim-notify"; - package = helpers.mkPackageOption "notify" pkgs.vimPlugins.nvim-notify; + package = helpers.mkPackageOption "nvim-notify" pkgs.vimPlugins.nvim-notify; - stages = mkOption { - type = types.nullOr (types.enum ["fade_in_slide_out" "fade" "slide" "static"]); - description = "Animation style"; - default = null; - }; - timeout = mkOption { - type = types.nullOr types.int; - description = "Default timeout for notifications"; - default = null; - }; - backgroundColour = mkOption { - type = types.nullOr types.str; - description = "For stages that change opacity this is treated as the highlight between the window"; - default = null; - }; - minimumWidth = mkOption { - type = types.nullOr types.int; - description = "Minimum width for notification windows"; - default = null; - }; - icons = mkOption { - type = types.nullOr (types.submodule { - options = { - error = icon; - warn = icon; - info = icon; - debug = icon; - trace = icon; + level = + helpers.defaultNullOpts.mkNullable + ( + with types; + oneOf [ + ints.unsigned + helpers.rawType + str + ] + ) + ''{__raw = "vim.log.levels.INFO";}'' + "Minimum log level to display. See `vim.log.levels`."; + + timeout = helpers.defaultNullOpts.mkUnsignedInt 5000 "Default timeout for notification."; + + maxWidth = helpers.mkNullOrOption (with types; either ints.unsigned helpers.rawType) '' + Max number of columns for messages. + ''; + + maxHeight = helpers.mkNullOrOption (with types; either ints.unsigned helpers.rawType) '' + Max number of lines for a message. + ''; + + stages = + helpers.defaultNullOpts.mkNullable + ( + with types; + either + ( + enum + [ + "fade" + "slide" + "fade_in_slide_out" + "static" + ] + ) + (listOf str) + ) + "fade_in_slide_out" + '' + Animation stages. + Can be either one of the builtin stages or an array of lua functions. + ''; + + backgroundColour = helpers.defaultNullOpts.mkStr "NotifyBackground" '' + For stages that change opacity this is treated as the highlight behind the window. + Set this to either a highlight group, an RGB hex value e.g. "#000000" or a function + returning an RGB code for dynamic values. + ''; + + icons = + mapAttrs + ( + name: default: + helpers.defaultNullOpts.mkStr default "Icon for the ${name} level." + ) + { + error = ""; + warn = ""; + info = ""; + debug = ""; + trace = "✎"; }; - }); - description = "Icons for the different levels"; - default = {}; + + onOpen = helpers.mkNullOrOption types.str '' + Function called when a new window is opened, use for changing win settings/config. + ''; + + onClose = helpers.mkNullOrOption types.str '' + Function called when a new window is closed. + ''; + + render = + helpers.defaultNullOpts.mkNullable + ( + with types; + either + (enum ["default" "minimal"]) + helpers.rawType + ) + "default" + "Function to render a notification buffer or a built-in renderer name."; + + minimumWidth = helpers.defaultNullOpts.mkUnsignedInt 50 '' + Minimum width for notification windows. + ''; + + fps = helpers.defaultNullOpts.mkPositiveInt 30 '' + Frames per second for animation stages, higher value means smoother animations but more CPU + usage. + ''; + + topDown = helpers.defaultNullOpts.mkBool true '' + Whether or not to position the notifications at the top or not. + ''; }; - }; config = let - setupOptions = with cfg; { - inherit stages timeout; - background_colour = backgroundColour; - minimum_width = minimumWidth; - icons = with icons; { - ERROR = error; - WARN = warn; - INFO = info; - DEBUG = debug; - TRACE = trace; - }; - }; + setupOptions = with cfg; + { + inherit + level + timeout + ; + max_width = maxWidth; + max_height = maxHeight; + stages = + helpers.ifNonNull' stages + ( + if isString stages + then stages + else map helpers.mkRaw stages + ); + background_colour = backgroundColour; + icons = + mapAttrs' + ( + name: value: { + name = strings.toUpper name; + inherit value; + } + ) + icons; + on_open = helpers.ifNonNull' onOpen (helpers.mkRaw onOpen); + on_close = helpers.ifNonNull' onClose (helpers.mkRaw onClose); + inherit render; + minimum_width = minimumWidth; + inherit fps; + top_down = topDown; + } + // cfg.extraOptions; in mkIf cfg.enable { extraPlugins = [cfg.package]; + extraConfigLua = '' vim.notify = require('notify'); require('notify').setup(${helpers.toLuaObject setupOptions}) diff --git a/tests/test-sources/plugins/utils/notify.nix b/tests/test-sources/plugins/utils/notify.nix new file mode 100644 index 00000000..3f8b9037 --- /dev/null +++ b/tests/test-sources/plugins/utils/notify.nix @@ -0,0 +1,31 @@ +{ + empty = { + plugins.notify.enable = true; + }; + + defaults = { + plugins.notify = { + enable = true; + + level.__raw = "vim.log.levels.INFO"; + timeout = 5000; + maxWidth = null; + maxHeight = null; + stages = "fade_in_slide_out"; + backgroundColour = "NotifyBackground"; + icons = { + error = ""; + warn = ""; + info = ""; + debug = ""; + trace = "✎"; + }; + onOpen = null; + onClose = null; + render = "default"; + minimumWidth = 50; + fps = 30; + topDown = true; + }; + }; +}