Skip to content

Commit

Permalink
feat: Show popup message as reminder, clears after x seconds (#5)
Browse files Browse the repository at this point in the history
  • Loading branch information
csessh authored Oct 7, 2024
1 parent 0710e76 commit 1292d22
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 4 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ require("stopinsert").setup()
| Items | Type | Default Value | Description |
| --------------------- | --------- | ------------------ | -------------- |
| `idle_time_ms` | number | `5000` (5 seconds) | Maximum time (in milliseconds) before you are forced out of Insert mode back to Normal mode. |
| `show_popup_msg` | boolean | `true` | Enable/disable popup message" |
| `clear_popup_ms` | number | `5000` | Maximum time (in milliseconds) for which the popup message hangs around |
| `disabled_filetypes` | list | `{ "TelescopePrompt", "checkhealth", "help", "lspinfo", "mason", "neo%-tree*", }` | List of filetypes to exclude the effect of this plugin. |

**NOTE:**
Expand Down
2 changes: 2 additions & 0 deletions lua/stopinsert/config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ local M = {}

M.config = {
idle_time_ms = 5000,
show_popup_msg = true,
clear_popup_ms = 5000,
disabled_filetypes = {
"TelescopePrompt",
"checkhealth",
Expand Down
11 changes: 7 additions & 4 deletions lua/stopinsert/init.lua
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
local M = {}
local util = require("stopinsert.util")
M.enable = true

M.enable = true
local user_cmds = {
enable = function()
M.enable = true
Expand All @@ -21,14 +20,18 @@ local user_cmds = {
end,
}

local config = require("stopinsert.config")
local util = require("stopinsert.util")

---@param opts table
---@return nil
function M.setup(opts)
opts = opts or {}
require("stopinsert.config").set(opts)
config.set(opts)

local plugin_autocmd_group = "StopInsertAutoCmd"
vim.api.nvim_create_autocmd("InsertEnter", {
group = vim.api.nvim_create_augroup("InsertEnterListener", { clear = true }),
group = vim.api.nvim_create_augroup(plugin_autocmd_group, { clear = true }),
callback = function()
if not M.enable then
return
Expand Down
39 changes: 39 additions & 0 deletions lua/stopinsert/popup.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
local M = {}

--- Create a simple popup message, positioned in the bottom right corner of the buffer
--- Automatically close this popup after 5 seconds
--- Courtersy of encourage.nvim
---@param message string
---@return nil
function M.show(message)
local width = #message
local height = 1
local buf = vim.api.nvim_create_buf(false, true)
local current_win = vim.api.nvim_get_current_win()
local win_config = vim.api.nvim_win_get_config(current_win)
local win_width = win_config.width
local win_height = win_config.height

local opts = {
style = "minimal",
relative = "win",
win = current_win,
width = width,
height = height,
row = win_height - height - 2,
col = win_width - width - 2,
border = "rounded",
}

local win = vim.api.nvim_open_win(buf, false, opts)
vim.api.nvim_buf_set_lines(buf, 0, -1, false, { message })
vim.api.nvim_win_set_option(win, "winhl", "Normal:NormalFloat,FloatBorder:FloatBorder")

vim.defer_fn(function()
if vim.api.nvim_win_is_valid(win) then
vim.api.nvim_win_close(win, true)
end
end, 5000)
end

return M
5 changes: 5 additions & 0 deletions lua/stopinsert/util.lua
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
local M = {}
local timer = nil
local config = require("stopinsert.config").config
local popup = require("stopinsert.popup")

---@return nil
function M.force_exit_insert_mode()
if vim.fn.mode() == "i" then
vim.cmd("stopinsert")

if config.show_popup_msg then
popup.show("StopInsertPlug: You were idling in Insert mode. Remeber to <Esc> when you finish editing.")
end
end
end

Expand Down

0 comments on commit 1292d22

Please sign in to comment.