Skip to content

Instantly share code, notes, and snippets.

@saegey
Last active August 29, 2015 14:17
Show Gist options
  • Save saegey/a78614dd22e9ce3c4b8b to your computer and use it in GitHub Desktop.
Save saegey/a78614dd22e9ce3c4b8b to your computer and use it in GitHub Desktop.
local grid = {}
local window = hs.window
function grid.snap(win, x, y, w, h)
local newframe = {
x = x,
y = y,
w = w,
h = h,
}
win:setFrame(newframe, 0)
end
-- |XX|
-- |XX|
function grid.maximize_window()
local win = window.focusedWindow()
local s = win:screen():frame()
grid.snap(win, s.x, 0, s.w, s.h)
end
-- |X |
-- | |
function grid.snap_northwest()
local win = window.focusedWindow()
local s = win:screen():frame()
grid.snap(win, s.x, 0, s.w/2, s.h/2)
end
-- |XX|
-- | |
function grid.snap_north()
local win = window.focusedWindow()
local s = win:screen():frame()
grid.snap(win, s.x, 0, s.w, s.h/2)
end
-- | X|
-- | |
function grid.snap_northeast()
local win = window.focusedWindow()
local s = win:screen():frame()
grid.snap(win, s.x+s.w/2, 0, s.w/2, s.h/2)
end
-- |X |
-- |X |
function grid.snap_west()
local win = window.focusedWindow()
local s = win:screen():frame()
grid.snap(win, s.x, 0, s.w/2, s.h)
end
-- | X|
-- | X|
function grid.snap_east()
local win = window.focusedWindow()
local s = win:screen():frame()
grid.snap(win, s.x+s.w/2, 0, s.w/2, s.h)
end
-- | |
-- |X |
function grid.snap_southwest()
local win = window.focusedWindow()
local s = win:screen():frame()
grid.snap(win, s.x, s.y+(s.h/2), s.w/2, s.h/2)
end
-- | |
-- |XX|
function grid.snap_south()
local win = window.focusedWindow()
local s = win:screen():frame()
grid.snap(win, s.x, s.y+(s.h/2), s.w, s.h/2)
end
-- | |
-- | X|
function grid.snap_southeast()
local win = window.focusedWindow()
local s = win:screen():frame()
grid.snap(win, s.x+s.w/2, s.y+(s.h/2), s.w/2, s.h/2)
end
function grid.pushwindow_nextscreen()
local win = window.focusedWindow()
local new_screen = win:screen():next():frame()
local old_screen = win:screen():frame()
local h_perc = win:size().h / old_screen.h
local w_perc = win:size().w / old_screen.w
local x_perc = math.abs(old_screen.x-win:topLeft().x) / old_screen.w
local y_perc = (win:topLeft().y - old_screen.y) / old_screen.h
grid.snap(win,
new_screen.x + (new_screen.w * x_perc),
new_screen.y + (new_screen.h * y_perc),
new_screen.w * w_perc,
new_screen.h * h_perc
)
end
return grid
local hyper = {"ctrl", "shift", "alt", "cmd"}
local grid = require "grid"
local window = require "hs.window"
local hotkey = require "hs.hotkey"
hs.hints.style = "vimperator"
hs.grid.MARGINX = 0
hs.grid.MARGINY = 0
hs.grid.GRIDHEIGHT = 13
hs.grid.GRIDWIDTH = 13
-- Disable window animations (janky for iTerm)
window.animationDuration = 0
-- Snap Window
hotkey.bind(hyper, 'U', grid.snap_northwest)
hotkey.bind(hyper, 'I', grid.snap_north)
hotkey.bind(hyper, 'O', grid.snap_northeast)
hotkey.bind(hyper, 'J', grid.snap_west)
hotkey.bind(hyper, 'K', grid.maximize_window)
hotkey.bind(hyper, 'L', grid.snap_east)
hotkey.bind(hyper, 'M', grid.snap_southwest)
hotkey.bind(hyper, ',', grid.snap_south)
hotkey.bind(hyper, '.', grid.snap_southeast)
hotkey.bind(hyper, 'LEFT', hs.grid.pushWindowLeft)
hotkey.bind(hyper, 'RIGHT', hs.grid.pushWindowRight)
hotkey.bind(hyper, '+', hs.grid.pushWindowNextScreen)
hotkey.bind(hyper, '-', hs.grid.pushWindowPrevScreen)
-- lock computer
hotkey.bind(hyper, "Escape", function() hs.caffeinate.lockScreen() end)
hotkey.bind(hyper, "tab", function() hs.hints.windowHints() end)
-- application help
local function open_help()
help_str = "1 - Chrome, 2 - Sublime, 2 - iTerm "
hs.alert.show(help_str, 2)
end
-- application shortcuts
hotkey.bind(hyper, '1', function () hs.application.launchOrFocus("Google Chrome") end)
hotkey.bind(hyper, '2', function () hs.application.launchOrFocus("Sublime Text") end)
hotkey.bind(hyper, '3', function () hs.application.launchOrFocus("iTerm") end)
hotkey.bind(hyper, '`', open_help)
-- reload config on save
function reloadConfig(files)
doReload = false
for _,file in pairs(files) do
if file:sub(-4) == ".lua" then
doReload = true
end
end
if doReload then
hs.reload()
end
end
hs.pathwatcher.new(os.getenv("HOME") .. "/.hammerspoon/", reloadConfig):start()
hs.alert.show("Config loaded")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment