Last active
December 21, 2024 16:23
-
-
Save SilverEzhik/f4626851965fa7a1be0b27bfb49280d1 to your computer and use it in GitHub Desktop.
A Hammerspoon script that will nag you to go to sleep once a minute from 00:30 to 06:00.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
--[[ | |
A little script by Ezhik (https://ezhik.me) to get you to go to sleep already. | |
Import it in your Hammerspoon config like this: | |
```lua | |
sleepNag = require("sleepNag") | |
``` | |
Then it'll kick in between 00:30 and 06:00 once a minute, reminding you to GO TO SLEEP. | |
You can set `sleepNag.disabled` to true to disable it for 10 minutes. | |
You can also just disable it completely by stopping the timer or deleting the script. I'm a comment, not a cop. | |
]] | |
local sleepNag = { | |
disabled = false | |
} | |
function sleepNag.hide() | |
if sleepNag.window then | |
sleepNag.window:delete() | |
sleepNag.window = nil | |
end | |
end | |
function sleepNag.show() | |
if sleepNag.disabled then return end | |
if sleepNag.window ~= nil then return end | |
local contentController = hs.webview.usercontent.new("closeHandler"):setCallback(function(message) | |
if message.body == "close" then | |
sleepNag.hide() | |
end | |
end) | |
local frame = hs.screen.primaryScreen():fullFrame() | |
sleepNag.window = hs.webview.new(frame, {}, contentController) | |
sleepNag.window:html([[ | |
<body> | |
<p> | |
It's <x-clock></x-clock>! Go to sleep!!! | |
</p> | |
<button>Close</button> | |
</body> | |
<style> | |
@keyframes blink { | |
50% { opacity: 0; } | |
} | |
body { | |
background-color: rgba(0, 0, 0, 0.9); | |
color: white; | |
display: flex; | |
justify-content: center; | |
align-items: center; | |
flex-direction: column; | |
} | |
* { | |
font-family: Iosevka SS04, Iosevka, monospace; | |
font-variant-numeric: tabular-nums; | |
} | |
p { | |
font-size: 48px; | |
} | |
button { | |
font-size: 24px; | |
padding: 10px 20px; | |
margin-top: 20px; | |
} | |
</style> | |
<script> | |
const clockElement = document.querySelector("x-clock"); | |
const close = () => window.webkit.messageHandlers.closeHandler.postMessage("close"); | |
function updateClock() { | |
const now = new Date(); | |
clockElement.textContent = now.toLocaleTimeString("en-US", { hour12: false }); | |
const time = now.toLocaleTimeString("en-US", { hour: "2-digit", minute: "2-digit", hour12: false }); | |
if (time >= "06:00") { | |
close(); | |
} else if (time >= "02:00") { | |
clockElement.style.color = "red"; | |
clockElement.style.animation = "blink 1s infinite"; | |
} else if (time >= "01:30") { | |
clockElement.style.color = "red"; | |
clockElement.style.animation = "none"; | |
} else { | |
clockElement.style.color = "white"; | |
clockElement.style.animation = "none"; | |
} | |
} | |
updateClock(); | |
setInterval(updateClock, 500); | |
document.querySelector("button").addEventListener("click", close); | |
</script> | |
]]) | |
sleepNag.window:windowStyle(0):level(hs.drawing.windowLevels.screenSaver):transparent(true) | |
sleepNag.window:show() | |
end | |
function sleepNag.handle() | |
local time = os.date("%H:%M") | |
if time >= "00:30" and time <= "06:00" then | |
if time:sub(-1) == "0" then | |
sleepNag.disabled = false | |
end | |
sleepNag.show() | |
else | |
sleepNag.disabled = false | |
sleepNag.hide() | |
end | |
end | |
sleepNag.timer = hs.timer.doAt(os.date("%H:%M", os.time() + 60), "1m", sleepNag.handle, true) | |
return sleepNag |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment