-
-
Notifications
You must be signed in to change notification settings - Fork 183
/
Copy pathset-theme
executable file
·186 lines (148 loc) · 4.94 KB
/
set-theme
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
#!/usr/bin/env python3
import argparse
import fileinput
import os
import re
import sys
import textwrap
from pathlib import Path
from subprocess import run, PIPE
THEMES = {
"gruvbox": {
"dark": {
"terminal": {
"colorScheme": "Gruvbox Dark",
"cursorColor": "#ffb261",
},
"tmux": {
"status-style": "fg=colour244",
"window-status-current-style": "fg=colour222",
"pane-border-style": "fg=colour240",
"pane-active-border-style": "fg=colour243"
}
},
"light": {
"terminal": {
"colorScheme": "Gruvbox Light",
"cursorColor": "#ffb261",
},
"tmux": {
"status-style": "fg=colour179",
"window-status-current-style": "fg=colour172",
"pane-border-style": "fg=colour186",
"pane-active-border-style": "fg=colour178"
}
}
},
"one": {
"dark": {
"terminal": {
"colorScheme": "One Half Dark",
"cursorColor": "#6de6f5",
},
"tmux": {
"status-style": "fg=colour110",
"window-status-current-style": "fg=colour39",
"pane-border-style": "fg=colour240",
"pane-active-border-style": "fg=colour243"
}
},
"light": {
"terminal": {
"colorScheme": "One Half Light",
"cursorColor": "#6de6f5",
},
"tmux": {
"status-style": "fg=colour110",
"window-status-current-style": "fg=colour39",
"pane-border-style": "fg=colour253",
"pane-active-border-style": "fg=colour250"
}
}
}
}
def get_windows_user():
result = run(['powershell.exe', '$env:UserName'],
stdout=PIPE,
universal_newlines=True)
return result.stdout.rstrip()
HOME = str(Path.home())
TERMINAL_CONFIG = f'/c/Users/{get_windows_user()}/AppData/Local/Packages/Microsoft.WindowsTerminal_8wekyb3d8bbwe/LocalState/settings.json' # noqa: E501
SHELL_CONFIG = f'{os.environ.get("XDG_CONFIG_HOME")}/zsh/.zshrc'
TMUX_CONFIG = f'{HOME}/.tmux.conf'
VIM_CONFIG = f'{HOME}/.vimrc'
def edit_inplace(file, preserve_symlinks=True):
if preserve_symlinks:
file = os.path.realpath(file)
return fileinput.input(files=(file,), inplace=True)
def active_theme_and_background():
with open(VIM_CONFIG, 'r') as f:
for line in f:
match = re.match('^set background=(.*)$', line)
if match:
bg = match.group(1)
continue
match = re.match('^colorscheme (.*$)$', line)
if match:
theme = match.group(1)
continue
try:
return theme, bg
except NameError:
sys.exit('error: "set background" or "colorscheme" statement not found'
f' in {VIM_CONFIG}')
def change_terminal_theme(theme, bg):
terminal_options = THEMES[theme][bg]['terminal']
with edit_inplace(TERMINAL_CONFIG) as f:
for line in f:
for key, value in terminal_options.items():
line = re.sub(rf'"{key}": ".*"', f'"{key}": "{value}"', line)
sys.stdout.write(line)
def change_tmux_theme(theme, bg):
tmux_options = THEMES[theme][bg]['tmux']
with edit_inplace(TMUX_CONFIG) as f:
for line in f:
for key, value in tmux_options.items():
line = re.sub(rf'^set -g {key} .*$', f'set -g {key} {value}',
line)
sys.stdout.write(line)
run(['tmux', 'source-file', TMUX_CONFIG])
def change_vim_theme(theme, bg):
with edit_inplace(VIM_CONFIG) as f:
for line in f:
line = re.sub(r'^colorscheme .*$', f'colorscheme {theme}', line)
line = re.sub(r'^set background=.*$', f'set background={bg}', line)
sys.stdout.write(line)
def change_fzf_theme(bg):
with edit_inplace(SHELL_CONFIG) as f:
for line in f:
re.sub(r'FZF_DEFAULT_OPTS="--color=.*"$',
f'FZF_DEFAULT_OPTS="--color={bg}"', line)
sys.stdout.write(line)
def parseargs():
parser = argparse.ArgumentParser(
formatter_class=argparse.RawDescriptionHelpFormatter,
description=textwrap.dedent('''\
Set a theme along with optionally toggling dark and light backgrounds.
'''))
parser.add_argument('theme', choices=THEMES, nargs='?',
help='the theme name')
parser.add_argument('--toggle-bg', action='store_true',
help='toggle the background between dark and light')
args = parser.parse_args()
if len(sys.argv) == 1:
parser.error('at least one argument is required')
return args
def main():
args = parseargs()
theme, bg = active_theme_and_background()
if args.theme:
theme = args.theme
if args.toggle_bg:
bg = 'light' if bg == 'dark' else 'dark'
change_terminal_theme(theme, bg)
change_tmux_theme(theme, bg)
change_vim_theme(theme, bg)
change_fzf_theme(bg)
if __name__ == '__main__':
main()