Skip to content

Instantly share code, notes, and snippets.

@mgreensmith
Created September 13, 2017 20:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mgreensmith/09355dc9862d109df889dc54ec0b32a5 to your computer and use it in GitHub Desktop.
Save mgreensmith/09355dc9862d109df889dc54ec0b32a5 to your computer and use it in GitHub Desktop.
-- Great stuff at https://github.com/cmsj/hammerspoon-config
local hyper = {"ctrl", "alt", "cmd"}
local laptopScreen = "Color LCD" -- 1440 x 900
local thunderboltScreen = "DELL P2715Q"
-- Defines for screen watcher
local screenWatcher = nil
local lastNumberOfScreens = #hs.screen.allScreens()
--local leftOffsetRect = hs.geometry.rect(280, 0, 1160, 900)
local leftOffsetRect = hs.geometry.rect(280, 0, 1400, 1050)
local singleScreenLayout = {
{"Slack", nil, laptopScreen, hs.layout.maximized, nil, nil},
{"iTerm", nil, laptopScreen, nil, nil, leftOffsetRect},
{"iTerm2", nil, laptopScreen, nil, nil, leftOffsetRect},
{"Sublime Text", nil, laptopScreen, nil, nil, leftOffsetRect},
{"Notes", nil, laptopScreen, nil, nil, leftOffsetRect},
{"Google Chrome", nil, laptopScreen, nil, nil, leftOffsetRect},
{"Spotify", nil, laptopScreen, nil, nil, leftOffsetRect}
}
local multiScreenLayout = {
{"Slack", nil, laptopScreen, hs.layout.maximized, nil, nil},
{"iTerm", nil, thunderboltScreen, hs.layout.right50, nil, nil},
{"iTerm2", nil, thunderboltScreen, hs.layout.right50, nil, nil},
{"Sublime Text", nil, thunderboltScreen, hs.layout.left50, nil, nil},
{"Notes", nil, laptopScreen, nil, nil, leftOffsetRect},
{"Google Chrome", nil, laptopScreen, nil, nil, leftOffsetRect},
{"Spotify", nil, laptopScreen, nil, nil, leftOffsetRect}
}
-- Callback function for changes in screen layout
function screensChangedCallback()
newNumberOfScreens = #hs.screen.allScreens()
if lastNumberOfScreens ~= newNumberOfScreens then
if newNumberOfScreens == 1 then
hs.alert.show("Switching to single-screen layout")
hs.layout.apply(singleScreenLayout)
else
hs.alert.show("Switching to multi-screen layout")
hs.layout.apply(multiScreenLayout)
end
end
lastNumberOfScreens = newNumberOfScreens
end
-- Replace Caffeine.app with 18 lines of Lua :D
-- NOTE: If you reload your config on a hotkey or a pathwatcher, you should call caffeine:delete() there
local caffeine = hs.menubar.new()
function setCaffeineDisplay(state)
local result
if state then
result = caffeine:setIcon("caffeine-on.pdf")
else
result = caffeine:setIcon("caffeine-off.pdf")
end
end
function caffeineClicked()
setCaffeineDisplay(hs.caffeinate.toggle("displayIdle"))
end
if caffeine then
caffeine:setClickCallback(caffeineClicked)
setCaffeineDisplay(hs.caffeinate.get("displayIdle"))
end
function reloadConfig()
screenWatcher:stop()
screenWatcher = nil
caffeine:delete()
hs.reload()
end
hs.hotkey.bind(hyper , "L", function()
hs.caffeinate.lockScreen()
end)
hs.hotkey.bind(hyper , "X", function()
hs.alert.show(hs.screen.allScreens())
end)
hs.hotkey.bind(hyper , "R", function()
reloadConfig()
hs.alert.show("Config loaded")
end)
hs.hotkey.bind(hyper , "Z", function()
if #hs.screen.allScreens() == 1 then
hs.alert.show("Applying single-screen layout")
hs.layout.apply(singleScreenLayout)
else
hs.alert.show("Applying multi-screen layout")
hs.layout.apply(multiScreenLayout)
end
end)
screenWatcher = hs.screen.watcher.new(screensChangedCallback)
screenWatcher:start()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment