From f4957a209f9ee1df4c0b7682f34ccdbef3ae29c0 Mon Sep 17 00:00:00 2001 From: gzagatti Date: Fri, 12 May 2023 11:25:01 +0800 Subject: [PATCH] location aware resize --- kitty/kitty.conf | 8 ++--- kitty/pass_keys.py | 21 +++++++++---- kitty/relative_resize.py | 65 ++++++++++++++++++++++++++++++++++++++++ 3 files changed, 84 insertions(+), 10 deletions(-) create mode 100644 kitty/relative_resize.py diff --git a/kitty/kitty.conf b/kitty/kitty.conf index 38516eb..8c4d284 100644 --- a/kitty/kitty.conf +++ b/kitty/kitty.conf @@ -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 diff --git a/kitty/pass_keys.py b/kitty/pass_keys.py index e6583d5..9601d4f 100644 --- a/kitty/pass_keys.py +++ b/kitty/pass_keys.py @@ -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")) @@ -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) diff --git a/kitty/relative_resize.py b/kitty/relative_resize.py new file mode 100644 index 0000000..233418b --- /dev/null +++ b/kitty/relative_resize.py @@ -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)