-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapply_iterm_config.py
More file actions
105 lines (91 loc) · 3.78 KB
/
apply_iterm_config.py
File metadata and controls
105 lines (91 loc) · 3.78 KB
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
#!/usr/bin/env python3
import colorsys
import hashlib
import iterm2
import json
import os
import subprocess
import sys
import types
CONFIG_FILE = ".iterm.json"
GLOBAL_CONFIG_FILE = os.path.expanduser("~/.iterm-global.json")
def hex_to_rgb(h):
h = h.lstrip('#')
return tuple(int(h[i:i+2], 16) for i in (0, 2, 4))
def repo_color(name):
hue = int(hashlib.md5(name.encode()).hexdigest(), 16) % 360
r, g, b = colorsys.hls_to_rgb(hue / 360, 0.40, 0.45)
return int(r * 255), int(g * 255), int(b * 255)
def git_info(cwd):
try:
root = subprocess.check_output(
['git', 'rev-parse', '--show-toplevel'],
cwd=cwd, stderr=subprocess.DEVNULL
).decode().strip()
branch = subprocess.check_output(
['git', 'rev-parse', '--abbrev-ref', 'HEAD'],
cwd=cwd, stderr=subprocess.DEVNULL
).decode().strip()
return os.path.basename(root), branch
except (subprocess.CalledProcessError, FileNotFoundError):
return None, None
async def apply_changes(connection):
config_dir = sys.argv[1] if len(sys.argv) > 1 else ""
cwd = sys.argv[2] if len(sys.argv) > 2 else os.getcwd()
if config_dir:
with open(os.path.join(config_dir, CONFIG_FILE)) as f:
config = json.load(f)
repo_name, branch = None, None
else:
config = {}
global_config = {}
if os.path.exists(GLOBAL_CONFIG_FILE):
with open(GLOBAL_CONFIG_FILE) as f:
global_config = json.load(f)
repo_name, branch = git_info(cwd) if global_config.get("git") else (None, None)
app = await iterm2.async_get_app(connection)
session = app.current_terminal_window.current_tab.current_session
default_profile = await iterm2.Profile.async_get_default(connection)
change = iterm2.LocalWriteOnlyProfile()
if "background_color" in config:
rgb = hex_to_rgb(config["background_color"])
change.set_background_color(iterm2.Color(*rgb))
elif default_profile.background_color is not None:
change.set_background_color(default_profile.background_color)
if "tab_color" in config:
rgb = hex_to_rgb(config["tab_color"])
change.set_use_tab_color(True)
change.set_tab_color(iterm2.Color(*rgb))
elif repo_name:
change.set_use_tab_color(True)
change.set_tab_color(iterm2.Color(*repo_color(repo_name)))
else:
change.set_use_tab_color(default_profile.use_tab_color)
if default_profile.tab_color is not None:
change.set_tab_color(default_profile.tab_color)
if "icon" in config:
icon_path = config["icon"]
if not icon_path.startswith("/"):
icon_path = os.path.join(config_dir or cwd, icon_path)
change.set_icon_mode(types.SimpleNamespace(value=2)) # 2 = CUSTOM
change.set_custom_icon_path(icon_path)
else:
change.set_icon_mode(types.SimpleNamespace(value=default_profile.icon_mode))
change.set_custom_icon_path(default_profile.custom_icon_path or "")
if "badge" in config:
change.set_badge_text(config["badge"])
else:
change.set_badge_text(default_profile.badge_text or "")
if "subtitle" in config:
change.set_subtitle(config["subtitle"])
elif repo_name:
change.set_subtitle(f"{repo_name} · {branch}" if branch else repo_name)
else:
change.set_subtitle(default_profile.subtitle or "")
if config_dir or repo_name:
change.set_initial_directory_mode(iterm2.InitialWorkingDirectory.INITIAL_WORKING_DIRECTORY_CUSTOM)
change.set_custom_directory(config_dir or cwd)
else:
change.set_initial_directory_mode(iterm2.InitialWorkingDirectory.INITIAL_WORKING_DIRECTORY_HOME)
await session.async_set_profile_properties(change)
iterm2.run_until_complete(apply_changes)