summaryrefslogtreecommitdiffstats
path: root/hammerspoon/init.lua
blob: 1a06600bfa56c68290af1973eff830937604f3f2 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
hs.loadSpoon("ReloadConfiguration")
spoon.ReloadConfiguration:start()

hs.loadSpoon("SpeedMenu")

hs.hotkey.bind({"alt"}, "h", function()
    hs.window.focusedWindow():focusWindowWest(nil, false, true)
end)
hs.hotkey.bind({"alt"}, "j", function()
    hs.window.focusedWindow():focusWindowSouth(nil, false, true)
end)
hs.hotkey.bind({"alt"}, "k", function()
    hs.window.focusedWindow():focusWindowNorth(nil, false, true)
end)
hs.hotkey.bind({"alt"}, "l", function()
    hs.window.focusedWindow():focusWindowEast(nil, false, true)
end)

hs.hotkey.bind({"alt"}, "b", function()
    hs.execute("open /Applications/Google\\ Chrome.app --new --args --new-window about:home")
end)

hs.hotkey.bind({"alt"}, "d", function()
    hs.caffeinate.lockScreen()
end)

hs.hotkey.bind({"alt"}, "f", function()
    hs.window.focusedWindow():toggleFullScreen()
end)

hs.hotkey.bind({"alt"}, "o", function()
    hs.execute("open -n /Applications/Alacritty.app/")
end)

hs.hotkey.bind({"alt"}, "q", function()
    hs.window.focusedWindow():close()
end)

hs.hotkey.bind({"alt"}, "r", function()
    hs.eventtap.keyStroke({"cmd"}, "space")
end)

hs.hotkey.bind({"alt"}, "[", function()
    hs.eventtap.keyStroke({"ctrl"}, "left")
end)

hs.hotkey.bind({"alt"}, "]", function()
    hs.eventtap.keyStroke({"ctrl"}, "right")
end)

hs.hotkey.bind({"cmd"}, "h", function()
    -- nothing
end)

hs.hotkey.bind({"cmd"}, "u", function()
    hs.eventtap.keyStroke({"cmd"}, "delete")
end)

hs.hotkey.bind({"cmd"}, "a", function()
    hs.eventtap.keyStroke({"cmd"}, "left")
end)

hs.hotkey.bind({"cmd"}, "e", function()
    hs.eventtap.keyStroke({"cmd"}, "right")
end)

for i = 1, 6 do
    hs.hotkey.bind({"alt"}, tostring(i), function()
        hs.eventtap.keyStroke({"ctrl"}, tostring(i))
    end)
end

-- bindings for window movement are handled by amethyst, since hammerspoon
-- doesn't support that well

extra_bindings = {
    ["Alacritty"] = hs.hotkey.modal.new(),
    ["Google Chrome"] = hs.hotkey.modal.new(),
}

slack_cmd_k_watcher = nil
extra_bindings["Alacritty"]:bind({"ctrl"}, "k", function()
    slack_cmd_k_watcher = hs.application.watcher.new(function(name, event_type, app)
        if event_type == hs.application.watcher.activated then
            if app:name() == "Slack" then
                hs.timer.doAfter(0.001, function()
                    hs.eventtap.keyStroke({"cmd"}, "k")
                end)
                slack_cmd_k_watcher:stop()
                slack_cmd_k_watcher = nil
            end
        end
    end)
    slack_cmd_k_watcher:start()
    hs.application.get("Slack"):mainWindow():focus()
end)

extra_bindings["Google Chrome"]:bind({"cmd"}, "h", function()
    hs.eventtap.keyStroke({"cmd"}, "left")
end)

extra_bindings["Google Chrome"]:bind({"cmd"}, "l", function()
    hs.eventtap.keyStroke({"cmd"}, "right")
end)

-- doesn't seem to work?
-- extra_bindings["Google Chrome"]:bind({"cmd", "shift"}, "i", function()
--     hs.eventtap.keyStroke({"cmd", "option"}, "i")
-- end)

current_app_name = nil

function enter_bindings(name)
    if extra_bindings[name] then
        extra_bindings[name]:enter()
        current_app_name = name
    end
end

function exit_bindings(name)
    if extra_bindings[name] then
        extra_bindings[name]:exit()
        current_app_name = nil
    end
end

-- the application watcher receives notifications about new apps being
-- activated before the old apps are deactivated, so we can't rely on
-- deactivated events. this should be fine because there should always be an
-- active app (at the very least, Finder)
current_application_watcher = hs.application.watcher.new(function(name, event_type, app)
    if event_type == hs.application.watcher.activated then
        if current_app_name ~= app:name() then
            exit_bindings(current_app_name)
            enter_bindings(app:name())
        end
    end
end)
enter_bindings(hs.application.frontmostApplication():name())
current_application_watcher:start()

cpu_usage_bar = hs.menubar.new()
timer = hs.timer.doEvery(1, function()
    hs.host.cpuUsage(function(cpu)
        local cpuUsage = cpu["overall"]["active"]
        cpu_usage_bar:setTitle(math.floor(cpuUsage + 0.5) .. "%")
    end)
end)

power_usage_bar = hs.menubar.new()
timer = hs.timer.doEvery(5, function()
    local watts = hs.execute("/Users/doy/.bin/st-doy2/power-usage")
    power_usage_bar:setTitle(string.gsub(watts, "\n", "") .. "W")
end)

hs.caffeinate.set("systemIdle", true, true)

hs.alert("Hammerspoon config reloaded")