-
Notifications
You must be signed in to change notification settings - Fork 39
/
kitty-convert-dump.py
executable file
·78 lines (61 loc) · 2.42 KB
/
kitty-convert-dump.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#!/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']}")
# skip dump "/usr/bin/kitty +hold command" or "/usr/bin/kitty +kitten ssh ..."
# also skip "/usr/bin/kitten __hold_till_enter__ ...."
if os.path.basename(w['foreground_processes'][0]['cmdline'][0]) in ["kitty", "kitten"]:
continue
if len(w['foreground_processes'][0]['cmdline']) >1 and os.path.basename(w['foreground_processes'][0]['cmdline'][1]) in ["dump-session.sh"]:
continue
# print(f"launch {env_to_str(w['env'])} {fg_proc_to_str(w['foreground_processes'])}")
print(f"launch {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)