nix-community.nixvim/plugins/ui/image.nix

160 lines
4.8 KiB
Nix
Raw Normal View History

2023-12-29 11:29:26 +01:00
{
lib,
helpers,
config,
pkgs,
...
}:
2024-05-05 19:39:35 +02:00
with lib;
let
2023-12-29 11:29:26 +01:00
cfg = config.plugins.image;
2024-05-05 19:39:35 +02:00
in
{
meta.maintainers = [ maintainers.GaetanLepage ];
2023-12-29 11:29:26 +01:00
2024-05-05 19:39:35 +02:00
options.plugins.image = helpers.neovim-plugin.extraOptionsOptions // {
enable = mkEnableOption "image.nvim";
2023-12-29 11:29:26 +01:00
package = helpers.mkPluginPackageOption "image.nvim" pkgs.vimPlugins.image-nvim;
2023-12-29 11:29:26 +01:00
2024-05-05 19:39:35 +02:00
backend =
helpers.defaultNullOpts.mkEnumFirstDefault
[
"kitty"
"ueberzug"
]
''
All the backends support rendering inside Tmux.
2023-12-29 11:29:26 +01:00
2024-05-05 19:39:35 +02:00
- kitty - best in class, works great and is very snappy
- ueberzug - backed by ueberzugpp, supports any terminal, but has lower performance
- Supports multiple images thanks to @jstkdng.
'';
2023-12-29 11:29:26 +01:00
2024-05-05 19:39:35 +02:00
integrations =
let
2023-12-29 11:29:26 +01:00
mkIntegrationOptions = integrationName: filetypesDefault: {
enabled = helpers.defaultNullOpts.mkBool true ''
Whether to enable the markdown integration.
'';
clearInInsertMode = helpers.defaultNullOpts.mkBool false ''
Clears the image when entering insert mode.
'';
downloadRemoteImages = helpers.defaultNullOpts.mkBool true ''
Whether to download remote images.
'';
onlyRenderImageAtCursor = helpers.defaultNullOpts.mkBool false ''
Whether to limit rendering to the image at the current cursor position.
'';
filetypes = helpers.defaultNullOpts.mkListOf types.str filetypesDefault ''
Markdown extensions (ie. quarto) can go here.
'';
};
in
2024-05-05 19:39:35 +02:00
mapAttrs mkIntegrationOptions {
2024-06-11 16:54:41 +01:00
markdown = [
"markdown"
"vimwiki"
];
neorg = [ "norg" ];
syslang = [ "syslang" ];
2024-05-05 19:39:35 +02:00
};
2023-12-29 11:29:26 +01:00
2024-05-05 19:39:35 +02:00
maxWidth = helpers.mkNullOrOption types.ints.unsigned "Image maximum width.";
2023-12-29 11:29:26 +01:00
2024-05-05 19:39:35 +02:00
maxHeight = helpers.mkNullOrOption types.ints.unsigned "Image maximum height.";
2023-12-29 11:29:26 +01:00
2024-05-05 19:39:35 +02:00
maxWidthWindowPercentage = helpers.mkNullOrOption types.ints.unsigned ''
Image maximum width as a percentage of the window width.
'';
2023-12-29 11:29:26 +01:00
2024-05-05 19:39:35 +02:00
maxHeightWindowPercentage = helpers.defaultNullOpts.mkUnsignedInt 50 ''
Image maximum height as a percentage of the window height.
'';
2023-12-29 11:29:26 +01:00
2024-05-05 19:39:35 +02:00
windowOverlapClearEnabled = helpers.defaultNullOpts.mkBool false ''
Toggles images when windows are overlapped.
'';
2023-12-29 11:29:26 +01:00
2024-05-05 19:39:35 +02:00
windowOverlapClearFtIgnore =
2024-06-11 16:54:41 +01:00
helpers.defaultNullOpts.mkListOf types.str
[
"cmp_menu"
"cmp_docs"
""
]
2023-12-29 11:29:26 +01:00
''
Toggles images when windows are overlapped.
'';
2024-05-05 19:39:35 +02:00
editorOnlyRenderWhenFocused = helpers.defaultNullOpts.mkBool false ''
Auto show/hide images when the editor gains/looses focus.
'';
2023-12-29 11:29:26 +01:00
2024-05-05 19:39:35 +02:00
tmuxShowOnlyInActiveWindow = helpers.defaultNullOpts.mkBool false ''
Auto show/hide images in the correct Tmux window (needs visual-activity off).
'';
2023-12-29 11:29:26 +01:00
2024-05-05 19:39:35 +02:00
hijackFilePatterns =
2024-06-11 16:54:41 +01:00
helpers.defaultNullOpts.mkListOf types.str
[
"*.png"
"*.jpg"
"*.jpeg"
"*.gif"
"*.webp"
]
2023-12-29 11:29:26 +01:00
''
Render image files as images when opened.
'';
2024-05-05 19:39:35 +02:00
};
2023-12-29 11:29:26 +01:00
config = mkIf cfg.enable {
2024-05-05 19:39:35 +02:00
extraPlugins = [ cfg.package ];
extraLuaPackages = ps: [ ps.magick ];
extraPackages = [
# In theory, we could remove that if the user explicitly disables `downloadRemoteImages` for
# all integrations but shipping `curl` is not too heavy.
pkgs.curl
] ++ optional (cfg.backend == "ueberzug") pkgs.ueberzugpp;
extraConfigLua =
let
setupOptions =
with cfg;
{
inherit backend;
integrations =
let
processIntegrationOptions = v: {
inherit (v) enabled;
clear_in_insert_mode = v.clearInInsertMode;
download_remote_images = v.downloadRemoteImages;
only_render_image_at_cursor = v.onlyRenderImageAtCursor;
inherit (v) filetypes;
};
in
mapAttrs (_: processIntegrationOptions) integrations;
max_width = maxWidth;
max_height = maxHeight;
max_width_window_percentage = maxWidthWindowPercentage;
max_height_window_percentage = maxHeightWindowPercentage;
window_overlap_clear_enabled = windowOverlapClearEnabled;
window_overlap_clear_ft_ignore = windowOverlapClearFtIgnore;
editor_only_render_when_focused = editorOnlyRenderWhenFocused;
tmux_show_only_in_active_window = tmuxShowOnlyInActiveWindow;
hijack_file_patterns = hijackFilePatterns;
}
// cfg.extraOptions;
in
''
require('image').setup(${helpers.toLuaObject setupOptions})
'';
2023-12-29 11:29:26 +01:00
};
}