Skip to content

Instantly share code, notes, and snippets.

@tynes
Created December 29, 2016 22:29
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 tynes/a89024f12381643030e152fa8fb701e2 to your computer and use it in GitHub Desktop.
Save tynes/a89024f12381643030e152fa8fb701e2 to your computer and use it in GitHub Desktop.
Hammerspoon config file v0.1
--------------------------------------------------------------------------------
-- CONSTANTS
--------------------------------------------------------------------------------
local cmd_alt_shift = {"cmd", "alt", "shift"}
local cmd_alt_ctrl = {"cmd", "alt", "ctrl"}
local cmd_alt_ctrl_shift = {"cmd", "alt", "ctrl", "shift"}
--------------------------------------------------------------------------------
-- CONFIGURATIONS
--------------------------------------------------------------------------------
hs.window.animationDuration = 0
--------------------------------------------------------------------------------
function config()
hs.hotkey.bind(cmd_alt_ctrl, "right", function()
local win = hs.window.focusedWindow()
win:right()
end)
hs.hotkey.bind(cmd_alt_ctrl, "left", function()
local win = hs.window.focusedWindow()
win:left()
end)
hs.hotkey.bind(cmd_alt_ctrl, "up", function()
local win = hs.window.focusedWindow()
win:up()
end)
hs.hotkey.bind(cmd_alt_ctrl, "down", function()
local win = hs.window.focusedWindow()
win:down()
end)
hs.hotkey.bind(cmd_alt_ctrl_shift, "left", function()
local win = hs.window.focusedWindow()
win:upLeft()
end)
hs.hotkey.bind(cmd_alt_ctrl_shift, "down", function()
local win = hs.window.focusedWindow()
win:downLeft()
end)
hs.hotkey.bind(cmd_alt_ctrl_shift, "right", function()
local win = hs.window.focusedWindow()
win:downRight()
end)
hs.hotkey.bind(cmd_alt_ctrl_shift, "up", function()
local win = hs.window.focusedWindow()
win:upRight()
end)
hs.hotkey.bind(cmd_alt_ctrl, "/", function()
local win = hs.window.focusedWindow()
win:maximize()
end)
hs.hotkey.bind(cmd_alt_ctrl, "h", function()
hs.hints.windowHints()
end)
hs.hotkey.bind(cmd_alt_ctrl, "R", function()
hs.reload()
hs.alert.show("Config loaded")
end)
hs.hotkey.bind(cmd_alt_shift, "left", function()
local win = hs.window.filter.new():setCurrentSpace(true)
if win == nil then
return
end
win:focusWindowWest(nil, false, false)
end)
hs.hotkey.bind(cmd_alt_shift, "right", function()
local win = hs.window.filter.new():setCurrentSpace(true)
if win == nil then
return
end
win:focusWindowEast(nil, false, false)
end)
hs.hotkey.bind(cmd_alt_shift, "up", function()
local win = hs.window.filter.new():setCurrentSpace(true)
if win == nil then
return
end
win:focusWindowNorth(nil, false, false)
end)
hs.hotkey.bind(cmd_alt_shift, "down", function()
local win = hs.window.filter.new():setCurrentSpace(true)
if win == nil then
return
end
win:focusWindowSouth(nil, false, false)
end)
--------------------------------------------------------------------------------
-- METHODS - BECAREFUL :)
--------------------------------------------------------------------------------
-- Returns the width of the smaller screen size
-- isFullscreen = false removes the toolbar
-- and dock sizes
function hs.screen.minWidth(isFullscreen)
local min_width = math.maxinteger
local allScreens = hs.screen.allScreens()
for i, screen in ipairs(allScreens) do
local screen_frame = screen:frame()
if (isFullscreen) then
screen_frame = screen:fullFrame()
end
min_width = math.min(min_width, screen_frame.w)
end
return min_width
end
-- isFullscreen = false removes the toolbar
-- and dock sizes
-- Returns the height of the smaller screen size
function hs.screen.minHeight(isFullscreen)
local min_height = math.maxinteger
local allScreens = hs.screen.allScreens()
for i, screen in ipairs(allScreens) do
local screen_frame = screen:frame()
if (isFullscreen) then
screen_frame = screen:fullFrame()
end
min_height = math.min(min_height, screen_frame.h)
end
return min_height
end
-- If you are using more than one monitor, returns X
-- considering the reference screen minus smaller screen
-- = (MAX_REFSCREEN_WIDTH - MIN_AVAILABLE_WIDTH) / 2
-- If using only one monitor, returns the X of ref screen
function hs.screen.minX(refScreen)
local min_x = refScreen:frame().x
local allScreens = hs.screen.allScreens()
if (#allScreens > 1) then
min_x = refScreen:frame().x + ((refScreen:frame().w - hs.screen.minWidth()) / 2)
end
return min_x
end
-- If you are using more than one monitor, returns Y
-- considering the focused screen minus smaller screen
-- = (MAX_REFSCREEN_HEIGHT - MIN_AVAILABLE_HEIGHT) / 2
-- If using only one monitor, returns the Y of focused screen
function hs.screen.minY(refScreen)
local min_y = refScreen:frame().y
local allScreens = hs.screen.allScreens()
if (#allScreens > 1) then
min_y = refScreen:frame().y + ((refScreen:frame().h - hs.screen.minHeight()) / 2)
end
return min_y
end
-- If you are using more than one monitor, returns the
-- half of minX and 0
-- = ((MAX_REFSCREEN_WIDTH - MIN_AVAILABLE_WIDTH) / 2) / 2
-- If using only one monitor, returns the X of ref screen
function hs.screen.almostMinX(refScreen)
local min_x = refScreen:frame().x
local allScreens = hs.screen.allScreens()
if (#allScreens > 1) then
min_x = refScreen:frame().x + (((refScreen:frame().w - hs.screen.minWidth()) / 2) - ((refScreen:frame().w - hs.screen.minWidth()) / 4))
end
return min_x
end
-- If you are using more than one monitor, returns the
-- half of minY and 0
-- = ((MAX_REFSCREEN_HEIGHT - MIN_AVAILABLE_HEIGHT) / 2) / 2
-- If using only one monitor, returns the Y of ref screen
function hs.screen.almostMinY(refScreen)
local min_y = refScreen:frame().y
local allScreens = hs.screen.allScreens()
if (#allScreens > 1) then
min_y = refScreen:frame().y + (((refScreen:frame().h - hs.screen.minHeight()) / 2) - ((refScreen:frame().h - hs.screen.minHeight()) / 4))
end
return min_y
end
-- Returns the frame of the smaller available screen
-- considering the context of refScreen
-- isFullscreen = false removes the toolbar
-- and dock sizes
function hs.screen.minFrame(refScreen, isFullscreen)
return {
x = hs.screen.minX(refScreen),
y = hs.screen.minY(refScreen),
w = hs.screen.minWidth(isFullscreen),
h = hs.screen.minHeight(isFullscreen)
}
end
-- +-----------------+
-- | | |
-- | | HERE |
-- | | |
-- +-----------------+
function hs.window.right(win)
local minFrame = hs.screen.minFrame(win:screen(), false)
minFrame.x = minFrame.x + (minFrame.w/2)
minFrame.w = minFrame.w/2
win:setFrame(minFrame)
end
-- +-----------------+
-- | | |
-- | HERE | |
-- | | |
-- +-----------------+
function hs.window.left(win)
local minFrame = hs.screen.minFrame(win:screen(), false)
minFrame.w = minFrame.w/2
win:setFrame(minFrame)
end
-- +-----------------+
-- | HERE |
-- +-----------------+
-- | |
-- +-----------------+
function hs.window.up(win)
local minFrame = hs.screen.minFrame(win:screen(), false)
minFrame.h = minFrame.h/2
win:setFrame(minFrame)
end
-- +-----------------+
-- | |
-- +-----------------+
-- | HERE |
-- +-----------------+
function hs.window.down(win)
local minFrame = hs.screen.minFrame(win:screen(), false)
minFrame.y = minFrame.y + minFrame.h/2
minFrame.h = minFrame.h/2
win:setFrame(minFrame)
end
-- +-----------------+
-- | HERE | |
-- +--------+ |
-- | |
-- +-----------------+
function hs.window.upLeft(win)
local minFrame = hs.screen.minFrame(win:screen(), false)
minFrame.w = minFrame.w/2
minFrame.h = minFrame.h/2
win:setFrame(minFrame)
end
-- +-----------------+
-- | |
-- +--------+ |
-- | HERE | |
-- +-----------------+
function hs.window.downLeft(win)
local minFrame = hs.screen.minFrame(win:screen(), false)
win:setFrame({
x = minFrame.x,
y = minFrame.y + minFrame.h/2,
w = minFrame.w/2,
h = minFrame.h/2
})
end
-- +-----------------+
-- | |
-- | +--------|
-- | | HERE |
-- +-----------------+
function hs.window.downRight(win)
local minFrame = hs.screen.minFrame(win:screen(), false)
win:setFrame({
x = minFrame.x + minFrame.w/2,
y = minFrame.y + minFrame.h/2,
w = minFrame.w/2,
h = minFrame.h/2
})
end
-- +-----------------+
-- | | HERE |
-- | +--------|
-- | |
-- +-----------------+
function hs.window.upRight(win)
local minFrame = hs.screen.minFrame(win:screen(), false)
win:setFrame({
x = minFrame.x + minFrame.w/2,
y = minFrame.y,
w = minFrame.w/2,
h = minFrame.h/2
})
end
end
config()
@tynes
Copy link
Author

tynes commented Dec 29, 2016

Currently no support for multiple screens in this config

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment