Skip to content

Commit 1292d22

Browse files
authored
feat: Show popup message as reminder, clears after x seconds (#5)
1 parent 0710e76 commit 1292d22

File tree

5 files changed

+55
-4
lines changed

5 files changed

+55
-4
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ require("stopinsert").setup()
3535
| Items | Type | Default Value | Description |
3636
| --------------------- | --------- | ------------------ | -------------- |
3737
| `idle_time_ms` | number | `5000` (5 seconds) | Maximum time (in milliseconds) before you are forced out of Insert mode back to Normal mode. |
38+
| `show_popup_msg` | boolean | `true` | Enable/disable popup message" |
39+
| `clear_popup_ms` | number | `5000` | Maximum time (in milliseconds) for which the popup message hangs around |
3840
| `disabled_filetypes` | list | `{ "TelescopePrompt", "checkhealth", "help", "lspinfo", "mason", "neo%-tree*", }` | List of filetypes to exclude the effect of this plugin. |
3941

4042
**NOTE:**

lua/stopinsert/config.lua

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ local M = {}
22

33
M.config = {
44
idle_time_ms = 5000,
5+
show_popup_msg = true,
6+
clear_popup_ms = 5000,
57
disabled_filetypes = {
68
"TelescopePrompt",
79
"checkhealth",

lua/stopinsert/init.lua

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
local M = {}
2-
local util = require("stopinsert.util")
3-
M.enable = true
42

3+
M.enable = true
54
local user_cmds = {
65
enable = function()
76
M.enable = true
@@ -21,14 +20,18 @@ local user_cmds = {
2120
end,
2221
}
2322

23+
local config = require("stopinsert.config")
24+
local util = require("stopinsert.util")
25+
2426
---@param opts table
2527
---@return nil
2628
function M.setup(opts)
2729
opts = opts or {}
28-
require("stopinsert.config").set(opts)
30+
config.set(opts)
2931

32+
local plugin_autocmd_group = "StopInsertAutoCmd"
3033
vim.api.nvim_create_autocmd("InsertEnter", {
31-
group = vim.api.nvim_create_augroup("InsertEnterListener", { clear = true }),
34+
group = vim.api.nvim_create_augroup(plugin_autocmd_group, { clear = true }),
3235
callback = function()
3336
if not M.enable then
3437
return

lua/stopinsert/popup.lua

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
local M = {}
2+
3+
--- Create a simple popup message, positioned in the bottom right corner of the buffer
4+
--- Automatically close this popup after 5 seconds
5+
--- Courtersy of encourage.nvim
6+
---@param message string
7+
---@return nil
8+
function M.show(message)
9+
local width = #message
10+
local height = 1
11+
local buf = vim.api.nvim_create_buf(false, true)
12+
local current_win = vim.api.nvim_get_current_win()
13+
local win_config = vim.api.nvim_win_get_config(current_win)
14+
local win_width = win_config.width
15+
local win_height = win_config.height
16+
17+
local opts = {
18+
style = "minimal",
19+
relative = "win",
20+
win = current_win,
21+
width = width,
22+
height = height,
23+
row = win_height - height - 2,
24+
col = win_width - width - 2,
25+
border = "rounded",
26+
}
27+
28+
local win = vim.api.nvim_open_win(buf, false, opts)
29+
vim.api.nvim_buf_set_lines(buf, 0, -1, false, { message })
30+
vim.api.nvim_win_set_option(win, "winhl", "Normal:NormalFloat,FloatBorder:FloatBorder")
31+
32+
vim.defer_fn(function()
33+
if vim.api.nvim_win_is_valid(win) then
34+
vim.api.nvim_win_close(win, true)
35+
end
36+
end, 5000)
37+
end
38+
39+
return M

lua/stopinsert/util.lua

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,16 @@
11
local M = {}
22
local timer = nil
33
local config = require("stopinsert.config").config
4+
local popup = require("stopinsert.popup")
45

56
---@return nil
67
function M.force_exit_insert_mode()
78
if vim.fn.mode() == "i" then
89
vim.cmd("stopinsert")
10+
11+
if config.show_popup_msg then
12+
popup.show("StopInsertPlug: You were idling in Insert mode. Remeber to <Esc> when you finish editing.")
13+
end
914
end
1015
end
1116

0 commit comments

Comments
 (0)