-
Notifications
You must be signed in to change notification settings - Fork 10
/
kitty-convert-dump.py
executable file
·73 lines (56 loc) · 1.9 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
#!/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)