Skip to content

Commit

Permalink
location aware resize
Browse files Browse the repository at this point in the history
  • Loading branch information
gzagatti committed May 12, 2023
1 parent 03ad63d commit f4957a2
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 10 deletions.
8 changes: 4 additions & 4 deletions kitty/kitty.conf
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,10 @@ map kitty_mod+] next_layout
map kitty_mod+- launch --location=hsplit --cwd=current
map kitty_mod+\ launch --location=vsplit --cwd=current

map kitty_mod+left resize_window narrower
map kitty_mod+right resize_window wider
map kitty_mod+up resize_window taller
map kitty_mod+down resize_window shorter 3
map alt+j kitten pass_keys.py relative_resize down 3 alt+j
map alt+k kitten pass_keys.py relative_resize up 3 alt+k
map alt+h kitten pass_keys.py relative_resize left 3 alt+h
map alt+l kitten pass_keys.py relative_resize right 3 alt+l

map kitty_mod+d detach_window ask
map kitty_mod+w focus_visible_window
Expand Down
21 changes: 15 additions & 6 deletions kitty/pass_keys.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
# Based on MIT licensed code at https://github.com/mrjones2014/smart-splits.nvim/blob/master/kitty/pass_keys.py
import re

from kittens.tui.handler import result_handler
from kitty.key_encoding import KeyEvent, parse_shortcut
from kittens.ssh.utils import is_kitten_cmdline

from relative_resize import relative_resize_window

# for logging during debug, use something like
# boss.call_remote_control(window, ("send-text", "--match", "id:{target}", f"{message}\n"))

Expand Down Expand Up @@ -51,17 +54,23 @@ def main():
@result_handler(no_ui=True)
def handle_result(args, result, target_window_id, boss):
window = boss.window_id_map.get(target_window_id)
action = args[1]
direction = args[2]
key_mapping = args[3]
vim_id = args[4] if len(args) > 4 else "(n?vim|emacs)"
key_mapping = args[3] if action == 'neighboring_window' else args[4]
amount = int(args[3]) if action == 'relative_resize' else None
vim_id_idx = 4 if action == 'neighboring_window' else 5
vim_id = args[vim_id_idx] if len(args) > vim_id_idx else "(n?vim|emacs)"

if window is None:
return

if is_window_vim(window, vim_id):
encoded = encode_key_mapping(window, key_mapping)
window.write_to_child(encoded)
else:
if is_window_vim(window, vim_id) and action == 'neighboring_window':
for keymap in key_mapping.split(">"):
encoded = encode_key_mapping(window, key_mapping)
window.write_to_child(encoded)
elif action == 'neighboring_window':
# do nothing if fully focused on one window
if boss.active_tab._current_layout_name != "stack":
boss.active_tab.neighboring_window(direction)
elif action == 'relative_resize':
relative_resize_window(direction, amount, target_window_id, boss)
65 changes: 65 additions & 0 deletions kitty/relative_resize.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
# Based on MIT licensed code at https://github.com/chancez/dotfiles/blob/badc69d3895a6a942285amount26b8c372a55d77533eamount/kitty/.config/kitty/relative_resize.py
from kittens.tui.handler import result_handler

def main(args):
pass

def relative_resize_window(direction, amount, target_window_id, boss):
window = boss.window_id_map.get(target_window_id)
if window is None:
return

neighbors = boss.active_tab.current_layout.neighbors_for_window(window, boss.active_tab.windows)
current_window_id = boss.active_tab.active_window

left_neighbors = neighbors.get('left')
right_neighbors = neighbors.get('right')
top_neighbors = neighbors.get('top')
bottom_neighbors = neighbors.get('bottom')

# has a neighbor on both sides
if direction == 'left' and (left_neighbors and right_neighbors):
boss.active_tab.resize_window('narrower', amount)
# only has left neighbor
elif direction == 'left' and left_neighbors:
boss.active_tab.resize_window('wider', amount)
# only has right neighbor
elif direction == 'left' and right_neighbors:
boss.active_tab.resize_window('narrower', amount)

# has a neighbor on both sides
elif direction == 'right' and (left_neighbors and right_neighbors):
boss.active_tab.resize_window('wider', amount)
# only has left neighbor
elif direction == 'right' and left_neighbors:
boss.active_tab.resize_window('narrower', amount)
# only has right neighbor
elif direction == 'right' and right_neighbors:
boss.active_tab.resize_window('wider', amount)

# has a neighbor above and below
elif direction == 'up' and (top_neighbors and bottom_neighbors):
boss.active_tab.resize_window('shorter', amount)
# only has top neighbor
elif direction == 'up' and top_neighbors:
boss.active_tab.resize_window('taller', amount)
# only has bottom neighbor
elif direction == 'up' and bottom_neighbors:
boss.active_tab.resize_window('shorter', amount)

# has a neighbor above and below
elif direction == 'down' and (top_neighbors and bottom_neighbors):
boss.active_tab.resize_window('taller', amount)
# only has top neighbor
elif direction == 'down' and top_neighbors:
boss.active_tab.resize_window('shorter', amount)
# only has bottom neighbor
elif direction == 'down' and bottom_neighbors:
boss.active_tab.resize_window('taller', amount)

@result_handler(no_ui=True)
def handle_result(args, result, target_window_id, boss):
direction = args[1]
amount = int(args[2])
if boss.active_tab._current_layour_name != "stack":
relative_resize_window(direction, amount, target_window_id, boss)

0 comments on commit f4957a2

Please sign in to comment.