plugins/toggleterm: switch to mkNeovimPlugin

This commit is contained in:
Gaetan Lepage 2024-03-31 00:21:25 +01:00 committed by Gaétan Lepage
parent 776cc84ad1
commit c068f78dcd
2 changed files with 356 additions and 225 deletions

View file

@ -5,199 +5,253 @@
pkgs, pkgs,
... ...
}: }:
with lib; let with lib;
cfg = config.plugins.toggleterm; helpers.neovim-plugin.mkNeovimPlugin config {
in { name = "toggleterm";
options.plugins.toggleterm = { originalName = "toggleterm.nvim";
enable = mkEnableOption "toggleterm"; defaultPackage = pkgs.vimPlugins.toggleterm-nvim;
package = helpers.mkPackageOption "toggleterm" pkgs.vimPlugins.toggleterm-nvim; maintainers = [maintainers.GaetanLepage];
# TODO: introduced 2024-04-07, remove on 2024-06-07
deprecateExtraOptions = true;
optionsRenamedToSettings = [
"size"
"onCreate"
"onOpen"
"onClose"
"onStdout"
"onStderr"
"onExit"
"hideNumbers"
"shadeFiletypes"
"autochdir"
"highlights"
"shadeTerminals"
"shadingFactor"
"startInInsert"
"insertMappings"
"terminalMappings"
"persistSize"
"persistMode"
"direction"
"closeOnExit"
"shell"
"autoScroll"
["floatOpts" "border"]
["floatOpts" "width"]
["floatOpts" "height"]
["floatOpts" "winblend"]
["floatOpts" "zindex"]
["winbar" "enabled"]
["winbar" "nameFormatter"]
];
imports = [
(
mkRemovedOptionModule
["plugins" "toggleterm" "openMapping"]
''
Please use `plugins.toggleterm.settings.open_mapping` instead but beware, you have to provide the value in this form: `"[[<c-\>]]"`.
''
)
];
settingsOptions = {
size = helpers.defaultNullOpts.mkStrLuaFnOr types.number "12" '' size = helpers.defaultNullOpts.mkStrLuaFnOr types.number "12" ''
Size of the terminal. Size of the terminal.
`size` can be a number or function `size` can be a number or a function.
Example: Example:
```nix ```nix
size = 20 size = 20
``` ```
OR OR
```
size = function(term) ```nix
size = \'\'
function(term)
if term.direction == "horizontal" then if term.direction == "horizontal" then
return 15 return 15
elseif term.direction == "vertical" then elseif term.direction == "vertical" then
return vim.o.columns * 0.4 return vim.o.columns * 0.4
end end
end end
\'\';
``` ```
''; '';
openMapping = helpers.mkNullOrOption types.str '' open_mapping = helpers.mkNullOrLua ''
Setting the open_mapping key to use for toggling the terminal(s) will set up mappings for Setting the `open_mapping` key to use for toggling the terminal(s) will set up mappings for
normal mode. normal mode.
''; '';
onCreate = helpers.defaultNullOpts.mkLuaFn "nil" '' on_create = helpers.mkNullOrLuaFn ''
Function to run when the terminal is first created. Function to run when the terminal is first created.
`fun(t: Terminal)`
''; '';
onOpen = helpers.defaultNullOpts.mkLuaFn "nil" '' on_open = helpers.mkNullOrLuaFn ''
Function to run when the terminal opens. Function to run when the terminal opens.
`fun(t: Terminal)`
''; '';
onClose = helpers.defaultNullOpts.mkLuaFn "nil" '' on_close = helpers.mkNullOrLuaFn ''
Function to run when the terminal closes. Function to run when the terminal closes.
`fun(t: Terminal)`
''; '';
onStdout = helpers.defaultNullOpts.mkLuaFn "nil" '' on_stdout = helpers.mkNullOrLuaFn ''
Callback for processing output on stdout. Callback for processing output on stdout.
`fun(t: Terminal, job: number, data: string[], name: string)`
''; '';
onStderr = helpers.defaultNullOpts.mkLuaFn "nil" '' on_stderr = helpers.mkNullOrLuaFn ''
Callback for processing output on stderr. Callback for processing output on stderr.
`fun(t: Terminal, job: number, data: string[], name: string)`
''; '';
onExit = helpers.defaultNullOpts.mkLuaFn "nil" '' on_exit = helpers.mkNullOrLuaFn ''
Function to run when terminal process exits. Function to run when terminal process exits.
`fun(t: Terminal, job: number, exit_code: number, name: string)`
''; '';
hideNumbers = helpers.defaultNullOpts.mkBool true '' hide_numbers = helpers.defaultNullOpts.mkBool true ''
Hide the number column in toggleterm buffers. Hide the number column in toggleterm buffers.
''; '';
shadeFiletypes = helpers.defaultNullOpts.mkNullable (types.listOf types.str) "[]" ""; shade_filetypes = helpers.defaultNullOpts.mkListOf types.str "[]" ''
Shade filetypes.
'';
autochdir = helpers.defaultNullOpts.mkBool false '' autochdir = helpers.defaultNullOpts.mkBool false ''
When neovim changes it current directory the terminal will change it's own when next it's When neovim changes it current directory the terminal will change it's own when next it's
opened. opened.
''; '';
highlights = helpers.mkNullOrOption (with types; (attrsOf (attrsOf str))) '' highlights =
Highlights which map to a highlight group name and a table of it's values. helpers.defaultNullOpts.mkAttrsOf helpers.nixvimTypes.highlight
''
{
NormalFloat.link = "Normal";
FloatBorder.link = "Normal";
StatusLine.gui = "NONE";
StatusLineNC = {
cterm = "italic";
gui = "NONE";
};
}
''
"Highlights which map a highlight group name to an attrs of it's values.";
Example: shade_terminals = helpers.defaultNullOpts.mkBool true ''
```nix NOTE: This option takes priority over highlights specified so if you specify Normal
highlights = { highlights you should set this to `false`.
Normal = {
guibg = "<VALUE-HERE>";
};
NormalFloat = {
link = "Normal";
},
FloatBorder = {
guifg = "<VALUE-HERE>";
guibg = "<VALUE-HERE>";
};
};
```
''; '';
shadeTerminals = helpers.defaultNullOpts.mkBool false '' shading_factor = helpers.mkNullOrOption types.int ''
NOTE: This option takes priority over highlights specified so if you specify Normal highlights
you should set this to false.
'';
shadingFactor = helpers.defaultNullOpts.mkInt (-30) ''
The percentage by which to lighten terminal background. The percentage by which to lighten terminal background.
Default: -30 (gets multiplied by -3 if background is light). default: -30 (gets multiplied by -3 if background is light).
''; '';
startInInsert = helpers.defaultNullOpts.mkBool true ""; start_in_insert = helpers.defaultNullOpts.mkBool true ''
Whether to start toggleterm in insert mode.
'';
insertMappings = helpers.defaultNullOpts.mkBool true '' insert_mappings = helpers.defaultNullOpts.mkBool true ''
Whether or not the open mapping applies in insert mode. Whether or not the open mapping applies in insert mode.
''; '';
terminalMappings = helpers.defaultNullOpts.mkBool true '' terminal_mappings = helpers.defaultNullOpts.mkBool true ''
Whether or not the open mapping applies in the opened terminals. Whether or not the open mapping applies in the opened terminals.
''; '';
persistSize = helpers.defaultNullOpts.mkBool true ""; persist_size = helpers.defaultNullOpts.mkBool true ''
Whether the terminal size should persist.
'';
persistMode = helpers.defaultNullOpts.mkBool true '' persist_mode = helpers.defaultNullOpts.mkBool true ''
If set to true (default) the previous terminal mode will be remembered. If set to true (default) the previous terminal mode will be remembered.
''; '';
direction = direction =
helpers.defaultNullOpts.mkEnumFirstDefault helpers.defaultNullOpts.mkEnum
["vertical" "horizontal" "tab" "float"] ["vertical" "horizontal" "tab" "float"] "horizontal"
""; "The direction the terminal should be opened in.";
closeOnExit = helpers.defaultNullOpts.mkBool true '' close_on_exit = helpers.defaultNullOpts.mkBool true ''
Close the terminal window when the process exits. Close the terminal window when the process exits.
''; '';
shell = helpers.defaultNullOpts.mkStr "`vim.o.shell`" '' shell = helpers.defaultNullOpts.mkStr ''{__raw = "vim.o.shell";}'' ''
Change the default shell. Change the default shell.
''; '';
autoScroll = helpers.defaultNullOpts.mkBool true '' auto_scroll = helpers.defaultNullOpts.mkBool true ''
Automatically scroll to the bottom on terminal output. Automatically scroll to the bottom on terminal output.
''; '';
floatOpts = { float_opts = {
border = border = helpers.mkNullOrOption helpers.nixvimTypes.border ''
helpers.defaultNullOpts.mkBorder "single" "toggleterm" `border` = "single" | "double" | "shadow" | "curved" | ... other options supported by
''
`border` = 'single' | 'double' | 'shadow' | 'curved' | ... other options supported by
`win open`. `win open`.
The border key is *almost* the same as 'nvim_open_win'. The border key is *almost* the same as 'nvim_open_win'.
The 'curved' border is a custom border type not natively supported but implemented in this plugin. The 'curved' border is a custom border type not natively supported but implemented in this plugin.
''; '';
width = helpers.defaultNullOpts.mkInt 50 ""; width = helpers.mkNullOrOption types.ints.unsigned "";
height = helpers.defaultNullOpts.mkInt 50 ""; height = helpers.mkNullOrOption types.ints.unsigned "";
winblend = helpers.defaultNullOpts.mkInt 3 ""; row = helpers.mkNullOrOption types.ints.unsigned "";
zindex = helpers.defaultNullOpts.mkInt 50 ""; col = helpers.mkNullOrOption types.ints.unsigned "";
winblend = helpers.defaultNullOpts.mkUnsignedInt 0 "";
zindex = helpers.mkNullOrOption types.ints.unsigned "";
title_pos = helpers.defaultNullOpts.mkStr "left" "";
}; };
winbar = {
enabled = helpers.defaultNullOpts.mkBool false "";
nameFormatter = winbar = {
enabled = helpers.defaultNullOpts.mkBool false ''
Whether to enable winbar.
'';
name_formatter =
helpers.defaultNullOpts.mkLuaFn helpers.defaultNullOpts.mkLuaFn
'' ''
function(term) function(term)
return term.name return term.name
end end
'' ""; ''
}; ''
}; `func(term: Terminal):string`
config = let
setupOptions = with cfg; { Example:
inherit autochdir highlights direction shell size; ```lua
open_mapping = helpers.ifNonNull' openMapping (helpers.mkRaw "[[${openMapping}]]"); function(term)
on_create = onCreate; return fmt("%d:%s", term.id, term:_display_name())
on_open = onOpen; end
on_close = onClose; ```
on_stdout = onStdout;
on_stderr = onStderr;
on_exit = onExit;
hide_numbers = hideNumbers;
shade_filetypes = shadeFiletypes;
shade_terminals = shadeTerminals;
shading_factor = shadingFactor;
start_in_insert = startInInsert;
insert_mappings = insertMappings;
terminal_mappings = terminalMappings;
persist_size = persistSize;
persist_mode = persistMode;
close_on_exit = closeOnExit;
auto_scroll = autoScroll;
float_opts = floatOpts;
winbar = with winbar; {
inherit enabled;
name_formatter = nameFormatter;
};
};
in
mkIf cfg.enable {
extraPlugins = [cfg.package];
extraConfigLua = ''
require("toggleterm").setup(${helpers.toLuaObject setupOptions})
''; '';
}; };
} };
settingsExample = {
open_mapping = "[[<c-\>]]";
direction = "float";
float_opts = {
border = "curved";
width = 130;
height = 30;
};
};
}

View file

@ -3,10 +3,27 @@
plugins.toggleterm.enable = true; plugins.toggleterm.enable = true;
}; };
test = { simple-example = {
plugins.toggleterm = { plugins.toggleterm = {
enable = true; enable = true;
settings = {
open_mapping = "[[<c-\>]]";
direction = "float";
float_opts = {
border = "curved";
width = 130;
height = 30;
};
};
};
};
example = {
plugins.toggleterm = {
enable = true;
settings = {
size = '' size = ''
function(term) function(term)
if term.direction == "horizontal" then if term.direction == "horizontal" then
@ -16,31 +33,32 @@
end end
end end
''; '';
openMapping = "<c-\\>"; open_mapping = "[[<c-\>]]";
onCreate = "function() end"; on_create = "function() end";
onOpen = "function() end"; on_open = "function() end";
onClose = "function() end"; on_close = "function() end";
onStdout = "function() end"; on_stdout = "function() end";
onStderr = "function() end"; on_stderr = "function() end";
onExit = "function() end"; on_exit = "function() end";
hideNumbers = false; hide_numbers = false;
shadeFiletypes = [""]; shade_filetypes = [""];
autochdir = true; autochdir = true;
highlights = { highlights = {
Normal.guibg = "#000000"; Normal.guibg = "#000000";
NormalFloat.link = "#FFFFFF"; NormalFloat.link = "#FFFFFF";
}; };
shadeTerminals = true; shade_terminals = true;
shadingFactor = -40; shading_factor = -40;
startInInsert = false; start_in_insert = false;
insertMappings = false; insert_mappings = false;
terminalMappings = true; terminal_mappings = true;
persistSize = false; persist_size = false;
persist_mode = false;
direction = "tab"; direction = "tab";
closeOnExit = false; close_on_exit = false;
shell = "bash"; shell = "bash";
autoScroll = false; auto_scroll = false;
floatOpts = { float_opts = {
border = "double"; border = "double";
width = 30; width = 30;
height = 30; height = 30;
@ -49,7 +67,7 @@
}; };
winbar = { winbar = {
enabled = true; enabled = true;
nameFormatter = '' name_formatter = ''
function(term) function(term)
return term.name + "Test" return term.name + "Test"
end end
@ -57,4 +75,63 @@
}; };
}; };
}; };
};
defaults = {
plugins.toggleterm = {
enable = true;
settings = {
size = 12;
open_mapping = null;
on_create = null;
on_open = null;
on_close = null;
on_stdout = null;
on_stderr = null;
on_exit = null;
hide_numbers = true;
shade_filetypes = [];
autochdir = false;
highlights = {
NormalFloat.link = "Normal";
FloatBorder.link = "Normal";
StatusLine.gui = "NONE";
StatusLineNC = {
cterm = "italic";
gui = "NONE";
};
};
shade_terminals = true;
shading_factor = -30;
start_in_insert = true;
insert_mappings = true;
terminal_mappings = true;
persist_size = true;
persist_mode = true;
direction = "horizontal";
close_on_exit = true;
shell.__raw = "vim.o.shell";
auto_scroll = true;
float_opts = {
border = null;
width = null;
height = null;
row = null;
col = null;
winblend = 0;
zindex = null;
title_pos = "left";
};
winbar = {
enabled = false;
name_formatter = ''
function(term)
return term.name
end
'';
};
};
};
};
} }