From bb3fdb1ede178b5350eccb2246f6da28dd213935 Mon Sep 17 00:00:00 2001 From: Syed Daanish Date: Fri, 24 Jan 2025 01:03:15 +0300 Subject: [PATCH] Add README and LISENCE --- LICENSE | 21 ++++++++++++++++++ README.md | 64 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ init.lua | 61 +++++++++++++++++++++++++++++++++++++++++----------- 3 files changed, 133 insertions(+), 13 deletions(-) create mode 100644 LICENSE create mode 100644 README.md diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..1976382 --- /dev/null +++ b/LICENSE @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) [year] [Your Name] + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/README.md b/README.md new file mode 100644 index 0000000..38902d3 --- /dev/null +++ b/README.md @@ -0,0 +1,64 @@ +# PicVim + +PicVim is a Neovim plugin that allows you to view and interact with images directly in Neovim. It supports various image formats such as PNG, JPG, GIF, BMP, and more, offering basic functionality like zooming, rotating, and panning. + +## Features + +- View images in Neovim. +- Zoom in and out using keybindings. +- Rotate the image with configurable keybindings. +- Pan the image using arrow keys or specific keybindings. +- Automatically scale and adjust images for optimal viewing. + +## Installation + +Use your prefferred package manager to install PicVim. + +### Eg. Using lazy.nvim + +If you use [lazy.nvim](https://github.com/folke/lazy.nvim), you can install PicVim by adding the following to your configuration: + +```lua +{ + 'your-username/picvim', +} +``` + +## Setup + +To activate the plugin, add the following to your init.lua configuration file (not needed if using lazy.nvim): + +```lua +require'picvim'.setup() +``` + +## Auto-commands + +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. + +## Keybindings + + h, Left Arrow – Pan left. + l, Right Arrow – Pan right. + j, Down Arrow – Pan down. + k, Up Arrow – Pan up. + =, + – Zoom in. + - – Zoom out. + t – Rotate clockwise (30 degrees). + T – Rotate counterclockwise (30 degrees). + o – Reset image position and rotation. + r – Redraw the image. + +## Dependencies + +`ImageMagick` for image manipulation. +`Neovim` 0.5.0 or higher. + +## Troubleshooting + +Ensure you have `ImageMagick` installed for image scaling and conversion functionality. +If the image fails to load, check that the file path is correct. + +## License + +This project is licensed under the MIT License – see the LICENSE file for details. diff --git a/init.lua b/init.lua index 3f5fefe..d8fc682 100644 --- a/init.lua +++ b/init.lua @@ -139,18 +139,31 @@ function M.setup() if not vim.b.img then vim.b.img = Image:new(vim.fn.expand "%:p") end - local win = vim.api.nvim_get_current_win() - local window_height = vim.api.nvim_win_get_height(win) - local window_width = vim.api.nvim_win_get_width(win) - local MAX_OFFSET_X = (window_width * 10) - 150 - local MIN_OFFSET_X = (-window_width * 10) + 150 - local MAX_OFFSET_Y = (window_height * 23) - 150 - local MIN_OFFSET_Y = (-window_height * 23) + 150 vim.cmd "setlocal buftype=nofile" vim.cmd "setlocal nonumber" vim.cmd "setlocal norelativenumber" vim.cmd "setlocal modifiable" local buf = vim.api.nvim_get_current_buf() + -- local win = vim.api.nvim_get_current_win() + -- local window_height = vim.api.nvim_win_get_height(win) + -- local window_width = vim.api.nvim_win_get_width(win) + -- local border_top_bottom = string.rep("-", window_width - 3) + -- local border_sides = "|" .. string.rep(" ", window_width - 5) .. "|" + -- local message = " Welcome to PicVim! Displaying image: " .. vim.fn.expand "%:p" + -- local lines = {} + -- table.insert(lines, message) + -- table.insert(lines, border_top_bottom) + -- for _ = 1, window_height - 4 do + -- table.insert(lines, border_sides) + -- end + -- table.insert(lines, border_sides) + -- table.insert(lines, border_top_bottom) + -- vim.api.nvim_buf_set_lines(buf, 0, -1, false, lines) + -- vim.api.nvim_win_set_cursor(win, { 1, 0 }) + vim.api.nvim_buf_set_lines(buf, 0, -1, false, {}) + vim.cmd "setlocal nomodifiable" + vim.cmd "setlocal nowrap" + vim.cmd "setlocal nolist" local debounce_timer = nil local debounce_interval = 50 local keypress_state = { o_x = 0, o_y = 0, zoom = 0, rotation = 0 } @@ -160,6 +173,13 @@ function M.setup() end local image = vim.b.img setmetatable(image, Image) + local win = vim.api.nvim_get_current_win() + local window_height = vim.api.nvim_win_get_height(win) + local window_width = vim.api.nvim_win_get_width(win) + local MAX_OFFSET_X = (window_width * 10) - 150 + local MIN_OFFSET_X = (-window_width * 10) + 150 + local MAX_OFFSET_Y = (window_height * 23) - 150 + local MIN_OFFSET_Y = (-window_height * 23) + 150 image.properties.o_x = math.min(math.max(image.properties.o_x + keypress_state.o_x, MIN_OFFSET_X), MAX_OFFSET_X) image.properties.o_y = math.min(math.max(image.properties.o_y + keypress_state.o_y, MIN_OFFSET_Y), MAX_OFFSET_Y) image.properties.zoom = math.min(math.max(image.properties.zoom + keypress_state.zoom, 0.1), 5) @@ -170,7 +190,6 @@ function M.setup() image:draw(x, y, window_width - 6, window_height - 1) vim.b.img = image end - local function schedule_redraw() if debounce_timer then debounce_timer:stop() @@ -182,21 +201,37 @@ function M.setup() end, debounce_interval) end vim.keymap.set("n", "", function() - keypress_state.o_x = keypress_state.o_x + 30 - schedule_redraw() - end, { buffer = buf, noremap = true, silent = true }) - vim.keymap.set("n", "", function() keypress_state.o_x = keypress_state.o_x - 30 schedule_redraw() end, { buffer = buf, noremap = true, silent = true }) + vim.keymap.set("n", "", function() + keypress_state.o_x = keypress_state.o_x + 30 + schedule_redraw() + end, { buffer = buf, noremap = true, silent = true }) vim.keymap.set("n", "", function() - keypress_state.o_y = keypress_state.o_y - 30 + keypress_state.o_y = keypress_state.o_y + 30 schedule_redraw() end, { buffer = buf, noremap = true, silent = true }) vim.keymap.set("n", "", function() + keypress_state.o_y = keypress_state.o_y - 30 + schedule_redraw() + end, { buffer = buf, noremap = true, silent = true }) + vim.keymap.set("n", "h", function() + keypress_state.o_x = keypress_state.o_x - 30 + schedule_redraw() + end, { buffer = buf, noremap = true, silent = true }) + vim.keymap.set("n", "l", function() + keypress_state.o_x = keypress_state.o_x + 30 + schedule_redraw() + end, { buffer = buf, noremap = true, silent = true }) + vim.keymap.set("n", "j", function() keypress_state.o_y = keypress_state.o_y + 30 schedule_redraw() end, { buffer = buf, noremap = true, silent = true }) + vim.keymap.set("n", "k", function() + keypress_state.o_y = keypress_state.o_y - 30 + schedule_redraw() + end, { buffer = buf, noremap = true, silent = true }) vim.keymap.set("n", "=", function() keypress_state.zoom = keypress_state.zoom + 0.2 schedule_redraw()