diff options
Diffstat (limited to 'config/hammerspoon/init.lua')
| -rw-r--r-- | config/hammerspoon/init.lua | 140 |
1 files changed, 29 insertions, 111 deletions
diff --git a/config/hammerspoon/init.lua b/config/hammerspoon/init.lua index 93950a9..cb1462a 100644 --- a/config/hammerspoon/init.lua +++ b/config/hammerspoon/init.lua @@ -4,40 +4,36 @@ -- defaults write org.hammerspoon.Hammerspoon MJConfigFile "$HOME/.config/hammerspoon/init.lua" -- hs.application.enableSpotlightForNameSearches(true) -local function focus(...) - local appNames = { ... } - local selectedAppName = nil - -- If only one argument is provided, use it directly without searching - if #appNames == 1 then - selectedAppName = appNames[1] - else - -- For multiple arguments, find the first app that exists on disk - local appDirs = { "/Applications/", os.getenv("HOME") .. "/Applications/" } - for _, appName in ipairs(appNames) do - for _, dir in ipairs(appDirs) do - local path = dir .. appName .. ".app" - if hs.fs.attributes(path) then - selectedAppName = appName - break - end - end - if selectedAppName then break end - end - if not selectedAppName then - print("No apps found: " .. table.concat(appNames, ", ")) - end - end - - return function() - if selectedAppName then - print('Opening ' .. selectedAppName) - hs.application.launchOrFocus(selectedAppName) - else - print("No apps found: " .. table.concat(appNames, ", ")) - end - end -end +-- The shared helpers, in dotlib.lua beside this file. A machine's own +-- ~/.config/hammerspoon/local.lua requires the same module, which is the point: +-- as locals of this file they were invisible to it. +-- +-- Hammerspoon already puts the config directory first on package.path; this +-- prepend takes the directory from this file's own path instead, so the module +-- is found however the config was located. +package.path = (debug.getinfo(1, "S").source:match("^@(.*)/[^/]+$") or ".") + .. "/?.lua;" .. package.path +-- A machine that pulled this file but has not re-linked yet has no dotlib.lua +-- beside it, and every hotkey below would be gone with nothing on screen to say +-- why. Name the fix instead. +local ok, lib = pcall(require, "dotlib") +if not ok then + hs.notify.new({ + title = "Hammerspoon", + informativeText = "dotlib.lua is missing -- run link.sh yes in the account repo", + }):send() + error(lib, 0) +end + +local focus = lib.focus +local execute = lib.execute +local hotkey = lib.hotkey +local type_text = lib.type_text +local type_current_date = lib.type_current_date +local type_random_ids = lib.type_random_ids +local cs, ca, csa = lib.mods.cs, lib.mods.ca, lib.mods.csa +local cag, hyp, spc = lib.mods.cag, lib.mods.hyp, lib.mods.spc local function toggle(appName) return function() @@ -62,12 +58,6 @@ local function toggle(appName) end -local function execute(cmd) - return function() - hs.execute(cmd, true) - end -end - local default_browser_bin = '/Users/sz/.bin/default-browser' local function set_default_browser(browserName) return execute(default_browser_bin .. ' "' .. browserName .. '"') @@ -274,11 +264,6 @@ local function toggle_audio_watcher() end -local function type_current_date() - local clock = os.date('%Y-%m-%d') - hs.eventtap.keyStrokes(clock) -end - local function type_uuids(count) local shell = '/bin/bash' local script = '/Volumes/Sense/Work/kafka/kafka/.ignore/tools/gen-uuids.sh' @@ -289,62 +274,6 @@ local function type_uuids(count) hs.eventtap.keyStrokes(out) end end -local function type_random_ids(count) - -- use a login shell - local cmd = string.format('/bin/bash --login -c "random-id.ts %s"', count) - return function() - local out = hs.execute(cmd, true) - if count == 1 then - out = out:gsub("%s+$", "") - end - hs.eventtap.keyStrokes(out) - end -end - -local function type_text(text, options) - return function() - if options and (options.min_delay or options.max_delay) then - -- Type with delays between characters - local min_delay = options.min_delay or 0 - local max_delay = options.max_delay or min_delay - - local chars = {} - for char in text:gmatch(".") do - table.insert(chars, char) - end - - local function typeNextChar(index) - if index > #chars then return end - - -- Handle newlines specially by sending return key - if chars[index] == "\n" then - hs.eventtap.keyStroke({}, "return") - else - hs.eventtap.keyStrokes(chars[index]) - end - - if index < #chars then - local delay = min_delay - if max_delay > min_delay then - -- Use squared random for skewed distribution - -- Most delays will be near min_delay, higher delays are rarer - local rand_squared = math.random() ^ 2 - delay = min_delay + rand_squared * (max_delay - min_delay) - end - hs.timer.doAfter(delay, function() - typeNextChar(index + 1) - end) - end - end - - typeNextChar(1) - else - -- Fast typing without delays - hs.eventtap.keyStrokes(text) - end - end -end - -- Useful to setup a hotkey to dump text for screencasting. e.g. -- hotkey('1', csa, demo_dump('/tmp/screencast1.txt')) -- hotkey('2', csa, demo_dump('/tmp/screencast2.txt')) @@ -493,17 +422,6 @@ end -- return toprint -- end -local function hotkey(key, mods, fn) - hs.hotkey.bind(mods, key, fn) -end - -local cs = { "ctrl", "shift" } -local ca = { "ctrl", "alt" } -local csa = { "ctrl", "shift", "alt" } -local cag = { "ctrl", "alt", "cmd" } -local hyp = { "ctrl", "alt", "cmd", "shift" } -local spc = 'space' - hotkey('0', cag, execute('/Users/sz/.bin/theme --set light')) hotkey('0', hyp, execute('/Users/sz/.bin/theme --set dark')) hotkey('1', cag, focus('Activity Monitor')) |
