Add support for setting filetypes

This commit is contained in:
2025-02-09 14:37:07 +03:00
parent 274d7ef476
commit f65f5ed530
3 changed files with 43 additions and 5 deletions

View File

@@ -36,6 +36,7 @@ To use PicVim, simply open an image file in Neovim and the image will be display
- Rotate the image with configurable keybindings. - Rotate the image with configurable keybindings.
- Pan the image using arrow keys or specific keybindings. - Pan the image using arrow keys or specific keybindings.
- Automatically scale and adjust images for optimal viewing. - Automatically scale and adjust images for optimal viewing.
- Configure keybindings and image types to work with your liking.
## ⚙️ Installation ## ⚙️ Installation
@@ -64,6 +65,13 @@ You can set custom keymaps using the `keymap` option.
```lua ```lua
require'picvim'.setup({ require'picvim'.setup({
filetypes = { -- Default filetypes
"png", -- For now only these are supported:
"jpg", -- > PNG, JPG, JPEG, GIF, BMP
"jpeg",
"gif", -- No need to set these if you want to
"bmp", -- support all of these image formats.
}
keymap = { -- Default keymaps keymap = { -- Default keymaps
move_left = { "<Left>", "h" }, -- Pan left move_left = { "<Left>", "h" }, -- Pan left
move_right = { "<Right>", "l" }, -- Pan right move_right = { "<Right>", "l" }, -- Pan right
@@ -81,9 +89,9 @@ require'picvim'.setup({
## 🛠️ Autocommands ## 🛠️ Autocommands
The plugin automatically activates for image files (.png, .jpg, .jpeg, .gif, .bmp) upon opening. It sets the buffer to a "non-file" type to display the image correctly. The plugin automatically activates for image files (.png, .jpg, .jpeg, .gif, .bmp) (configured in the `filetypes` option) upon opening. It sets the buffer to a "non-file" type to display the image correctly.
## ⌨️ Default Keybindings ## ⌨️ Default Keymaps
| Options within `keymap` | Default Value | Description | | Options within `keymap` | Default Value | Description |
|-----------------------------|-----------------------|--------------------------| |-----------------------------|-----------------------|--------------------------|
@@ -98,6 +106,14 @@ The plugin automatically activates for image files (.png, .jpg, .jpeg, .gif, .bm
| `reset` | `"o"` | Resets the image | | `reset` | `"o"` | Resets the image |
| `rerender` | `"r"` | Rerenders the image | | `rerender` | `"r"` | Rerenders the image |
## Filetypes
Defaults to:
- `{ "png", "jpg", "jpeg", "gif", "bmp" }`
- You can set custom filetypes using the `filetypes` option.
- The value must be a table of strings that includes the file extension for the image.
- Filetypes not in the default list will not work (They'll be ignored).
## 📦 Dependencies ## 📦 Dependencies
@@ -112,9 +128,9 @@ The plugin automatically activates for image files (.png, .jpg, .jpeg, .gif, .bm
- [x] Support image rotation. - [x] Support image rotation.
- [x] Add support for non-png raster images. - [x] Add support for non-png raster images.
- [x] Make Keybinds configurable. - [x] Make Keybinds configurable.
- [x] Add config for filetypes to work with.
- [ ] Add support for svg files. - [ ] Add support for svg files.
- [ ] Add PDF viewer. - [ ] Add PDF viewer.
- [ ] Add support for gif animations. - [ ] Add support for gif animations.
- [ ] Add config for filetypes to work with.
- [ ] Expose functions to handle the images. - [ ] Expose functions to handle the images.

View File

@@ -40,6 +40,13 @@ Use your preferred plugin manager, e.g., with lazy.nvim:
The plugin can be configured using: The plugin can be configured using:
>lua >lua
require'picvim'.setup({ require'picvim'.setup({
filetypes = { -- Default filetypes
"png", -- For now only these are supported:
"jpg", -- > PNG, JPG, JPEG, GIF, BMP
"jpeg",
"gif", -- No need to set these if you want to
"bmp", -- support all of these image formats.
}
keymap = { keymap = {
move_left = { "<Left>", "h" }, -- Pan left move_left = { "<Left>", "h" }, -- Pan left
move_right = { "<Right>", "l" }, -- Pan right move_right = { "<Right>", "l" }, -- Pan right
@@ -69,6 +76,13 @@ The plugin can be configured using:
│ `rerender` │ `"r"` │ Rerenders the image │ │ `rerender` │ `"r"` │ Rerenders the image │
└─────────────────────────┴──────────────────┴──────────────────────────┘ └─────────────────────────┴──────────────────┴──────────────────────────┘
Filetypes defaults to:
- `{ "png", "jpg", "jpeg", "gif", "bmp" }`
- You can set custom filetypes using the `filetypes` option.
- The value must be a table of strings.
- Filetypes not in the default list will not work (They'll be ignored).
============================================================================== ==============================================================================
4. *USAGE* 4. *USAGE*

View File

@@ -133,8 +133,16 @@ end
function M.setup(config) function M.setup(config)
local opts = config or {} local opts = config or {}
local allowed_types = { png = true, jpg = true, jpeg = true, gif = true, bmp = true }
local filetypes = {}
for _, ft in ipairs(opts.filetypes or { "png", "jpg", "jpeg", "gif", "bmp" }) do
if allowed_types[ft] then
table.insert(filetypes, ft)
end
end
local formatted = "*." .. table.concat(filetypes, ",*.")
vim.api.nvim_create_autocmd("BufEnter", { vim.api.nvim_create_autocmd("BufEnter", {
pattern = "*.png,*.jpg,*.jpeg,*.gif,*.bmp", pattern = formatted,
callback = function() callback = function()
if not vim.b.img then if not vim.b.img then
vim.b.img = Image:new(vim.fn.expand("%:p")) vim.b.img = Image:new(vim.fn.expand("%:p"))
@@ -257,7 +265,7 @@ function M.setup(config)
end, end,
}) })
vim.api.nvim_create_autocmd("VimResized", { vim.api.nvim_create_autocmd("VimResized", {
pattern = "*.png,*.jpg,*.jpeg,*.gif,*.bmp", pattern = formatted,
callback = function() callback = function()
if not vim.b.img then if not vim.b.img then
vim.b.img = Image:new(vim.fn.expand("%:p")) vim.b.img = Image:new(vim.fn.expand("%:p"))