Skip to content

Instantly share code, notes, and snippets.

@programus
Last active November 2, 2024 23:31
Show Gist options
  • Save programus/d3bf9c6b8f9b7648891d1f59b665b35e to your computer and use it in GitHub Desktop.
Save programus/d3bf9c6b8f9b7648891d1f59b665b35e to your computer and use it in GitHub Desktop.
pull down from top alacritty (like iTerm2)
window:
# Window dimensions (changes require restart)
#
# Number of lines/columns (not pixels) in the terminal. The number of columns
# must be at least `2`, while using a value of `0` for columns and lines will
# fall back to the window manager's recommended size.
dimensions:
columns: 500
lines: 30
# Window position (changes require restart)
#
# Specified in number of pixels.
# If the position is not set, the window manager will handle the placement.
position:
x: 0
y: 0
# Window padding (changes require restart)
#
# Blank space added around the window in pixels. This padding is scaled
# by DPI and the specified value is always added at both opposing sides.
#padding:
# x: 0
# y: 0
# Spread additional padding evenly around the terminal content.
#dynamic_padding: false
# Window decorations
#
# Values for `decorations`:
# - full: Borders and title bar
# - none: Neither borders nor title bar
#
# Values for `decorations` (macOS only):
# - transparent: Title bar, transparent background and title bar buttons
# - buttonless: Title bar, transparent background and no title bar buttons
decorations: none
key_bindings:
- { key: Return, mods: Command, action: ToggleSimpleFullscreen }
local spaces = require('hs._asm.undocumented.spaces') -- https://github.com/asmagill/hs._asm.undocumented.spaces
-- Switch alacritty
hs.hotkey.bind({'alt'}, 'space', function ()
local APP_NAME = 'Alacritty'
function moveWindow(alacritty, space, mainScreen)
-- move to main space
local win = nil
while win == nil do
win = alacritty:mainWindow()
end
print(win)
print(space)
print(win:screen())
print(mainScreen)
local fullScreen = not win:isStandard()
if fullScreen then
hs.eventtap.keyStroke('cmd', 'return', 0, alacritty)
end
winFrame = win:frame()
scrFrame = mainScreen:fullFrame()
print(winFrame)
print(scrFrame)
winFrame.w = scrFrame.w
winFrame.y = scrFrame.y
winFrame.x = scrFrame.x
print(winFrame)
win:setFrame(winFrame, 0)
print(win:frame())
win:spacesMoveTo(space)
if fullScreen then
hs.eventtap.keyStroke('cmd', 'return', 0, alacritty)
end
win:focus()
end
local alacritty = hs.application.get(APP_NAME)
if alacritty ~= nil and alacritty:isFrontmost() then
alacritty:hide()
else
local space = spaces.activeSpace()
local mainScreen = hs.screen.find(spaces.mainScreenUUID())
if alacritty == nil and hs.application.launchOrFocus(APP_NAME) then
local appWatcher = nil
print('create app watcher')
appWatcher = hs.application.watcher.new(function(name, event, app)
print(name)
print(event)
if event == hs.application.watcher.launched and name == APP_NAME then
app:hide()
moveWindow(app, space, mainScreen)
appWatcher:stop()
end
end)
print('start watcher')
appWatcher:start()
end
if alacritty ~= nil then
moveWindow(alacritty, space, mainScreen)
end
end
end)
@jonashelgemo
Copy link

jonashelgemo commented Apr 14, 2021

Thanks, this works great! I have a small addition to hide Alacritty when other apps get focus or cmd-tab is used. Put this below your code to get it working:

-- Hide alacritty if not in focus
hs.window.filter.default:subscribe(hs.window.filter.windowFocused, function(window, appName)
  local alacritty = hs.application.get('Alacritty')
  if alacritty ~= nil then
     alacritty:hide()
  end
end)

@programus
Copy link
Author

Thanks, this works great! I have a small addition to hide Alacritty when other apps get focus or cmd-tab is used. Put this below your code to get it working:

-- Hide alacritty if not in focus
hs.window.filter.default:subscribe(hs.window.filter.windowFocused, function(window, appName)
  local alacritty = hs.application.get('Alacritty')
  if alacritty ~= nil then
     alacritty:hide()
  end
end)

Great idea. But I will not merge this because I sometimes need to operate in other focused windows while monitoring the display of the terminal. For example, I have a long run job in the terminal, I want to reply a mail while waiting, but if there’s anything wrong, I hope I can deal with it immediately.

So I need the terminal window there even it has lost focus.

And you have given enough instruction that anyone need this feature can add it very easily. I will leave it as a comment of this gist.

Thank you for your code!

@jonashelgemo
Copy link

Sure, no problem, the tweak is just a matter of preference 😀

@Tylerc230
Copy link

Any idea how to make alacritty always show up on the primary screen in a multimonitor setup? It seems that spaces.activeSpace() returns the currently focused space and not the primary one (contrary to what the documents say it should return). Basically I always want my term to show up on my external monitor. Thanks!

@jonashelgemo
Copy link

Any idea how to make alacritty always show up on the primary screen in a multimonitor setup? It seems that spaces.activeSpace() returns the currently focused space and not the primary one (contrary to what the documents say it should return). Basically I always want my term to show up on my external monitor. Thanks!

@Tylerc230 My first hacky suggestion would be something like:

  • Find the UUID of your laptop display (print("mainScreen:", spaces.mainScreenUUID()) when opening terminal on that screen should output the uuid in the Hammerspoon console)
  • Find all screenUUIDs (spaces.layout() or similar)
  • If connected to external-display, show terminal on any UUID not equal to your laptop screen uuid
  • If no connected external display show on laptop display

Good luck and give me a shout if you can't get it working, my lua skills are weak, but I can try to help if you get stuck 😀

@Tylerc230
Copy link

Tylerc230 commented Jul 23, 2021

Here's what I came up with (thanks for the help!):

  local function visiblePrimarySpace()
    local primaryScreen = hs.screen.primaryScreen()
    local primaryScreenSpaces = spaces.layout()[primaryScreen:getUUID()]
    local visibleSpaces = spaces.query(spaces.masks.currentSpaces)
    for _, ps in pairs(primaryScreenSpaces) do
      for _, vs in pairs(visibleSpaces) do  
        if ps == vs then
          return ps
        end
      end
    end
  end

@jonashelgemo
Copy link

Cool, looks good 👍

@zcag
Copy link

zcag commented Nov 7, 2021

Great configuration thanks!
I have one issue though, but i don't know how to fix it.
When have a full-screen app open and press the hotkey, it opens over the full-screen app as expected but immidiately takes me to the desktop (while alacritty is still over that full-screen app).
Any suggestions on fixing this?

@truebit
Copy link

truebit commented Oct 18, 2023

@truebit
Copy link

truebit commented Oct 19, 2023

I have rewrite this script to use latest hs.spaces module, and replace get app_name to get bundle id to avoid mismatching with browser title and I works fine with hammerspoon Version 0.9.100(6815):
check my fork for details: https://gist.github.com/truebit/31396bb2f48c75285d724c9e9e037bcd

And here is the Kitty implementation: https://gist.github.com/truebit/d79b8018666d65e95970f208d8f5d149

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