Skip to content

Instantly share code, notes, and snippets.

@mgkennard
Created December 13, 2019 10:05
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save mgkennard/e60ea3b354963293035a315d4b9d69f0 to your computer and use it in GitHub Desktop.
Save mgkennard/e60ea3b354963293035a315d4b9d69f0 to your computer and use it in GitHub Desktop.
local clockingLog = hs.logger.new("clocking")
local clockingMenu = hs.menubar.new()
local currentTask = nil
local function trim(s)
return (s:gsub("^%s*(.-)%s*$", "%1"))
end
local function eval(sexp, callback)
hs.task.new(
"/usr/local/bin/emacsclient",
function(exitCode, stdOut, stdErr)
if exitCode == 0 then
callback(trim(stdOut))
end
end,
{ "--eval", sexp }
):start()
end
local function updateClockingMenu()
eval(
"(org-clock-is-active)",
function(value)
if value == "nil" then
clockingMenu:setTitle("No Task")
else
eval(
"(org-clock-get-clock-string)",
function(value)
clockingMenu:setTitle(string.match(value, '"(.+)"'))
end
)
end
end
)
end
local function startUpdatingClockingMenu()
hs.timer.doEvery(10, updateClockingMenu)
end
local mod = {}
function mod.init()
updateClockingMenu()
startUpdatingClockingMenu()
end
return mod
@iT-Boyer
Copy link

iT-Boyer commented Apr 1, 2021

Hello, I really like the work you have done for this, but when I use it, the timing refresh is always interrupted unexpectedly. I don’t know the reason. Can you help me solve it?

local function startUpdatingClockingMenu()
   hs.timer.doEvery(10, updateClockingMenu)
end

@mgkennard
Copy link
Author

I need to revisit this. For me the refresh seems to have broken completely recently. I did notice that the refresh broke when macOS came back from being suspended. I probably need to take a look at some of the recent updates to Hammerspoon and see if they've changed anything that this script uses.

@iT-Boyer
Copy link

iT-Boyer commented Apr 7, 2021

Continue to pay attention, after optimization, hope to be notified

@isamert
Copy link

isamert commented Feb 7, 2022

I just added return to the lines 41 and 48:

41c41
<    hs.timer.doEvery(5, updateClockingMenu)
---
>    return hs.timer.doEvery(5, updateClockingMenu)
48c48
<     startUpdatingClockingMenu()
---
>     return startUpdatingClockingMenu()

And binded the timer into a global variable in my init.lua:

local clocking = require "clocking"
dateTimeGarbageCollectorPreventer = clocking.init()

Now it works without any problems. Here is the issue that I found the solution: Hammerspoon/hammerspoon#1942 TLDR; timer gets garbage collected for some reason, when we bind it to a some global value, it doesn't get garbage collected.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment