Skip to content

Commit

Permalink
Feat: Only display a notification once #24
Browse files Browse the repository at this point in the history
  • Loading branch information
agoodshort committed Jul 27, 2023
1 parent c1003d2 commit a492b43
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 11 deletions.
30 changes: 20 additions & 10 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,16 +34,6 @@ local function handler(key)
return get_return_key(key)
end

-- key disabled
if config.disabled_keys[key] then
if config.notification then
vim.schedule(function()
util.notify("The " .. key .. " key is disabled!")
end)
end
return ""
end

-- reset
if config.resetting_keys[key] then
last_count = 0
Expand All @@ -55,6 +46,23 @@ local function handler(key)

-- restrict
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
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

if
last_count < config.max_count
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 a492b43

Please # to comment.