forked from dflock/kitty-save-session
-
Notifications
You must be signed in to change notification settings - Fork 1
/
kitty-convert-dump.py
executable file
·73 lines (53 loc) · 1.62 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
#!/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
SHELL = os.getenv("SHELL")
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 = ""
if len(fg) < 1:
return s
first_process = fg[0]
s += f"{cmdline_to_str(first_process['cmdline'])}"
# avoid re-launching kitty or kitty controls (like kitty-dump.sh or kitty @ ls)
if 'kitty' in s:
return 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")
for tab in os_window["tabs"]:
print("\nnew_tab")
print(f"layout {tab['layout']}")
for w in tab["windows"]:
print(f"cd {w['cwd']}")
print(
f"launch {SHELL}"
)
if w["is_focused"]:
print("focus")
if __name__ == "__main__":
stdin = sys.stdin.readlines()
session = json.loads("".join(stdin))
convert(session)