Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

kitty: save a tab cache when closing a tab with ctrl+w #97

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 74 additions & 0 deletions base/terminal/kitty-convert-dump.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
# Code from https://github.com/dflock/kitty-save-session, please check it out!
#!/usr/bin/env python3
"""
this tool is used to convert Kitty session dump to Kitty session file which can be loaded by Kitty
"""

import json
import os
import sys


def env_to_str(env):
"""Convert an env list to a series of '--env key=value' parameters and return as a string."""
s = ""
for key in env:
s += f"--env {key}={env[key]} "

return s.strip()


def cmdline_to_str(cmdline):
"""Convert a cmdline list to a space separated string."""
s = ""
for e in cmdline:
s += f"{e} "

return s.strip()


def fg_proc_to_str(fg):
"""Convert a foreground_processes list to a space separated string."""
s = ""
fg = fg[0]

# s += f"--cwd {fg['cwd']} {cmdline_to_str(fg['cmdline'])}"
s += f"{cmdline_to_str(fg['cmdline'])}"

if s == "kitty @ ls":
return os.getenv("SHELL")
return s


def convert(session):
"""Convert a kitty session dict, into a kitty session file and output it."""
first = True
for os_window in session:
if first:
first = False
else:
print("\nnew_os_window\n")

for tab in os_window["tabs"]:
print("\n")
print(f"new_tab {tab['title']}")
# print('enabled_layouts *)
print(f"layout {tab['layout']}")
# This is a bit of a kludge to set cwd for the tab, as
# setting it in the launch command didn't work, for some reason?
if tab["windows"]:
print(f"cd {tab['windows'][0]['cwd']}")

for w in tab["windows"]:
print(f"title {w['title']}")
print(
f"launch {env_to_str(w['env'])} {fg_proc_to_str(w['foreground_processes'])}"
)
if w["is_focused"]:
print("focus")


if __name__ == "__main__":
stdin = sys.stdin.readlines()
session = json.loads("".join(stdin))
convert(session)
1 change: 0 additions & 1 deletion base/terminal/kitty.nix
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ in
hm.programs.kitty.keybindings =
{
"ctrl+q" = "close_os_window"; # Quit application
"ctrl+w" = "close_window"; # Prioritizes internal windows > tabs

"ctrl+t" = "new_tab";

Expand Down
13 changes: 12 additions & 1 deletion base/terminal/zsh.nix
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,21 @@
autocd = true; # If empty directory given as command, interpret it as cd
shellAliases.src = ". ~/.zshrc";

initExtra =
initExtra = # bash
''
source ${./options.sh}
source ${./keybinds.sh}

function zle_ls
{
kitty @ ls -m recent:0 > /tmp/kittyTabCache.json
cat /tmp/kittyTabCache.json | python3 ./kitty-convert-dump.py > $HOME/.config/kitty/kitty-session.kitty

zle reset-prompt; zle redisplay
}

zle -N zle_ls
bindkey "^O" zle_ls
'';
};

Expand Down