diff --git a/README.md b/README.md index 291e573..8ec450d 100644 --- a/README.md +++ b/README.md @@ -2,7 +2,7 @@ Simple neovim plugin for keeping your cursor centered while reading through code/text/whatever -![readermode.gif](readermode.gif) +https://github.com/user-attachments/assets/d9bd8a00-54cd-4264-83a8-2ad4d82d64dc ## Requirements @@ -12,25 +12,25 @@ Simple neovim plugin for keeping your cursor centered while reading through code ### lazy.nvim - ```lua - { - 'sarrisv/readermode.nvim', - opts = { --[[ Configurations Options ]] } - } - ``` +```lua +{ + 'sarrisv/readermode.nvim', + opts = { --[[ Configurations Options ]] } +} +``` ### Configuration - ```lua - { - enabled = false, -- Turned off by default - keymap = "R" -- Keymap used to toggle ReaderMode on/off - } - ``` +```lua + { + enabled = false, -- Turned off by default + keymap = "R" -- Keymap used to toggle ReaderMode on/off + } +``` ## Usage -Toggle **ReaderMode** using `:ReaderMode` or the provided *normal mode* keymapping +Toggle **ReaderMode** using `:ReaderMode` or the provided _normal mode_ keymapping ## Credits diff --git a/lua/readermode/init.lua b/lua/readermode/init.lua index 6545e9f..3272322 100644 --- a/lua/readermode/init.lua +++ b/lua/readermode/init.lua @@ -18,17 +18,29 @@ function M.setup(opts) vim.api.nvim_create_user_command("ReaderMode", require("readermode").toggle, {}) vim.keymap.set({ "n" }, M.opts.keymap, M.toggle, { desc = M.opts.desc }) + local last_line = M.getCurrentLineNumber() + -- Define Autocommand w/ new Autogroup vim.api.nvim_create_autocmd({ "CursorMoved", "CursorMovedI" }, { group = vim.api.nvim_create_augroup("ReaderMode", { clear = true }), callback = function() if M.opts.enabled then - M.center() + local current_line = M.getCurrentLineNumber() + if current_line ~= last_line then + M.center() + else + end + last_line = current_line end end, }) end +---@return number +function M.getCurrentLineNumber() + return vim.api.nvim_win_get_cursor(0)[1] +end + ---@return nil function M.toggle() M.opts.enabled = not M.opts.enabled diff --git a/readermode.gif b/readermode.gif deleted file mode 100644 index 6ed3e4a..0000000 Binary files a/readermode.gif and /dev/null differ