summaryrefslogtreecommitdiff
path: root/hammerspoon/init.lua
blob: c130897eb1abd4bf3acb1676888def7bd295b5e9 (plain)
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
159
160
161
-- 这个脚本在 MacBook 接入外接显示屏时
-- 将 Dock 栏移动到右侧
local function updateDockPosition()
    local screens = hs.screen.allScreens()
    local dockPos = "bottom"
    local autoHide = true
    local foundExternal = false

    -- 查找名为 "P5" 的外接屏
    for _, s in ipairs(screens) do
        if s:name() == "P5" then
            foundExternal = true
            break
        end
    end

    if foundExternal then
        dockPos = "right"
        autoHide = true
    else
        dockPos = "bottom"
        autoHide = true
    end

    local cmd = string.format(
        'defaults write com.apple.dock orientation %s; ' ..
        'defaults write com.apple.dock autohide -bool %s; ' ..
        'killall Dock',
        dockPos,
        autoHide and "true" or "false"
    )

    hs.execute(cmd)
end

local screenWatcher = hs.screen.watcher.new(updateDockPosition)
screenWatcher:start()

updateDockPosition()

-- Set DNS servers for the Wi-Fi interface
local function setDNS()
    local success, output = hs.execute("networksetup -setdnsservers 'Wi-Fi' 9.9.9.9 208.67.222.123 208.67.220.123")
    if success then
        hs.alert.show("DNS updated successfully")
    else
        hs.alert.show("Failed to update DNS: " .. output)
    end
end
setDNS()

-- Configure and enable/disable an HTTP/HTTPS proxy
local function setProxy(enable)
    local state = enable and "on" or "off"
    hs.execute("networksetup -setwebproxy 'Wi-Fi' 127.0.0.1 7890")
    hs.execute("networksetup -setsecurewebproxy 'Wi-Fi' 127.0.0.1 7890")
    hs.execute(string.format("networksetup -setwebproxystate 'Wi-Fi' %s", state))
    hs.execute(string.format("networksetup -setsecurewebproxystate 'Wi-Fi' %s", state))
    hs.alert.show("Proxy " .. state)
end
setProxy("on")

-- PaperWM.spoon
PaperWM = hs.loadSpoon("PaperWM")
PaperWM:bindHotkeys({
    -- switch to a new focused window in tiled grid
    focus_left  = {{"alt"}, "left"},
    focus_right = {{"alt"}, "right"},
    focus_up    = {{"alt"}, "up"},
    focus_down  = {{"alt"}, "down"},

    -- switch windows by cycling forward/backward
    -- (forward = down or right, backward = up or left)
    focus_prev = {{"alt"}, "k"},
    focus_next = {{"alt"}, "j"},

    -- move windows around in tiled grid
    swap_left  = {{"alt", "shift"}, "left"},
    swap_right = {{"alt", "shift"}, "right"},
    swap_up    = {{"alt", "shift"}, "up"},
    swap_down  = {{"alt", "shift"}, "down"},

    -- position and resize focused window
    center_window        = {{"alt", "shift"}, "c"},
    anchor_window_left   = {{"alt", "shift"}, ","},
    anchor_window_right  = {{"alt", "shift"}, "."},
    full_width           = {{"alt", "shift"}, "f"},
    cycle_width          = {{"alt", "shift"}, "r"},
    reverse_cycle_width  = {{"ctrl", "alt", "cmd"}, "r"},
    cycle_height         = {{"alt", "cmd", "shift"}, "r"},
    reverse_cycle_height = {{"ctrl", "alt", "cmd", "shift"}, "r"},

    -- increase/decrease width
    increase_width = {{"alt", "shift"}, "l"},
    decrease_width = {{"alt", "shift"}, "h"},

    -- move focused window into / out of a column
    slurp_in = {{"alt", "shift"}, "i"},
    barf_out = {{"alt", "shift"}, "o"},

    -- toggle accordion stacking for the focused column
    toggle_stack = {{"alt", "shift", "shift"}, "s"},

    -- split screen focused window with left window
    split_screen = {{ "alt", "shift" }, "s"},

    -- move the focused window into / out of the tiling layer
    toggle_floating = {{"alt", "shift"}, "escape"},
    -- raise all floating windows on top of tiled windows
    focus_floating  = {{"alt", "shift"}, "f"},

    -- focus the first / second / etc window in the current space
    focus_window_1 = {{"alt"}, "1"},
    focus_window_2 = {{"alt"}, "2"},
    focus_window_3 = {{"alt"}, "3"},
    focus_window_4 = {{"alt"}, "4"},
    focus_window_5 = {{"alt"}, "5"},
    focus_window_6 = {{"alt"}, "6"},
    focus_window_7 = {{"alt"}, "7"},
    focus_window_8 = {{"alt"}, "8"},
    focus_window_9 = {{"alt"}, "9"},

    -- focus the leftmost / rightmost window in the current space
    focus_window_first = {{"cmd", "shift"}, "home"},
    focus_window_last  = {{"cmd", "shift"}, "end"},

    -- switch to a new Mission Control space
    switch_recent_space = {{"ctrl", "alt", "cmd"}, "6"},
    switch_space_l = {{"alt", "cmd"}, ","},
    switch_space_r = {{"alt", "cmd"}, "."},
    switch_space_1 = {{"alt", "cmd"}, "1"},
    switch_space_2 = {{"alt", "cmd"}, "2"},
    switch_space_3 = {{"alt", "cmd"}, "3"},
    switch_space_4 = {{"alt", "cmd"}, "4"},
    switch_space_5 = {{"alt", "cmd"}, "5"},
    switch_space_6 = {{"alt", "cmd"}, "6"},
    switch_space_7 = {{"alt", "cmd"}, "7"},
    switch_space_8 = {{"alt", "cmd"}, "8"},
    switch_space_9 = {{"alt", "cmd"}, "9"},

    -- move focused window to a new space and tile
    move_window_l = {{ "ctrl", "alt", "cmd" }, "left"},
    move_window_r = {{ "ctrl", "alt", "cmd" }, "right"},
    move_window_u = {{ "ctrl", "alt", "cmd" }, "up"},
    move_window_d = {{ "ctrl", "alt", "cmd" }, "down"},
    move_window_1 = {{"alt", "cmd", "shift"}, "1"},
    move_window_2 = {{"alt", "cmd", "shift"}, "2"},
    move_window_3 = {{"alt", "cmd", "shift"}, "3"},
    move_window_4 = {{"alt", "cmd", "shift"}, "4"},
    move_window_5 = {{"alt", "cmd", "shift"}, "5"},
    move_window_6 = {{"alt", "cmd", "shift"}, "6"},
    move_window_7 = {{"alt", "cmd", "shift"}, "7"},
    move_window_8 = {{"alt", "cmd", "shift"}, "8"},
    move_window_9 = {{"alt", "cmd", "shift"}, "9"},

    -- refresh/retile windows (forcedly)
    refresh_windows          = { { "alt", "cmd", "shift" }, "r" },
    refresh_windows_forcedly = { { "alt", "cmd", "shift" }, "t" },

})
PaperWM:start()