Skip to content

Commit

Permalink
Merge pull request #25 from agoodshort/main
Browse files Browse the repository at this point in the history
Feat: Only display a notification once #24
  • Loading branch information
m4xshen authored Jul 27, 2023
2 parents c1003d2 + fda8a35 commit 96ae09b
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 4 deletions.
16 changes: 13 additions & 3 deletions lua/hardtime/init.lua
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
local util = require("hardtime.util")

local last_time = util.get_time()
local last_notification = util.get_time()
local last_count = 0
local last_key
local last_keys = ""
Expand Down Expand Up @@ -33,12 +34,21 @@ local function handler(key)
return get_return_key(key)
end

local curr_time = util.get_time()
if curr_time - last_notification > config.max_time then
util.reset_notification()
end

-- key disabled
if config.disabled_keys[key] then
if config.notification then
if
config.notification
and curr_time - last_notification > config.max_time
then
vim.schedule(function()
util.notify("The " .. key .. " key is disabled!")
end)
last_notification = util.get_time()
end
return ""
end
Expand All @@ -54,8 +64,6 @@ local function handler(key)
end

-- restrict
local curr_time = util.get_time()

if
last_count < config.max_count
or curr_time - last_time > config.max_time
Expand All @@ -66,6 +74,7 @@ local function handler(key)
or (config.allow_different_key and key ~= last_key)
then
last_count = 1
util.reset_notification()
else
last_count = last_count + 1
end
Expand All @@ -79,6 +88,7 @@ local function handler(key)
vim.schedule(function()
util.notify("You pressed the " .. key .. " key too soon!")
end)
last_notification = util.get_time()
end
last_key = key
return ""
Expand Down
10 changes: 9 additions & 1 deletion lua/hardtime/util.lua
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
local M = {}
local previous_text

function M.get_time()
return vim.fn.reltimefloat(vim.fn.reltime()) * 1000
Expand All @@ -13,7 +14,14 @@ function M.try_eval(expression)
end

function M.notify(text)
vim.notify(text, vim.log.levels.WARN, { title = "hardtime" })
if text ~= previous_text then
vim.notify(text, vim.log.levels.WARN, { title = "hardtime" })
end
previous_text = text
end

function M.reset_notification()
previous_text = nil
end

return M

0 comments on commit 96ae09b

Please # to comment.