Created
February 26, 2024 15:47
-
-
Save smuuf/92b8ab66d87f908f3ca3afb4b66852cf to your computer and use it in GitHub Desktop.
git-gui dark theme patcher
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/env python | |
import os | |
import subprocess | |
from sys import stderr | |
FORCED_GIT_GUI_PATH=None | |
# Do NOT modify anything below. | |
UNPATCHED_NEEDLE = 'pave_toplevel .' | |
PATCHED_NEEDLE_START = 'Dark theme: START' | |
PATCHED_NEEDLE_END = 'Dark theme: END' | |
def error_exit(msg: str) -> None: | |
print(f"ERROR: {msg}", file=stderr) | |
exit(1) | |
def check_access(filepath: str, mode: str): | |
try: | |
open(filepath, mode) | |
return True | |
except PermissionError: | |
return False | |
def find_git_gui_path() -> str: | |
result = subprocess.run(['git', '--exec-path'], capture_output=True, encoding='utf-8') | |
if result.returncode != 0: | |
error_exit(f"Couldn't find git exec path\n{result.stderr}") | |
git_core_path = result.stdout.strip() | |
return f'{git_core_path}/git-gui' | |
def patch_git_gui(path: bytes) -> None: | |
with open(path, 'r') as f: | |
lines = f.readlines() | |
found_patched = False | |
for i, line in enumerate(lines): | |
if UNPATCHED_NEEDLE in line: | |
start = i + 1 | |
break | |
if PATCHED_NEEDLE_START in line: | |
found_patched = True | |
if found_patched: | |
error_exit("Already patched with dark theme") | |
lines.insert(start, THEME) | |
print(''.join(lines)) | |
THEME = f""" | |
# {PATCHED_NEEDLE_START} | |
ttk::style theme use default | |
ttk::style configure TFrame -background #333 | |
ttk::style configure TLabelframe -background #333 | |
ttk::style configure TLabelframe.Label -background #333 -foreground #fff | |
ttk::style configure TPanedwindow -background #333 | |
ttk::style configure EntryFrame -background #333 | |
ttk::style configure TScrollbar -background #666 -troughcolor #444 -arrowcolor #fff -arrowsize 15 | |
ttk::style map TScrollbar -background [list active #333 disabled #000] | |
ttk::style configure TLabel -background #333 -foreground #fff | |
ttk::style configure TButton -background #333 -foreground #fff -borderwidth 2 -bordercolor #fff | |
ttk::style map TButton -background [list active #555 disabled #111 readonly #000] | |
ttk::style configure TCheckbutton -background #333 -foreground #fff -indicatorbackground #666 -indicatorcolor #fff | |
ttk::style map TCheckbutton -background [list active #555 disabled #111 readonly #000] | |
ttk::style configure TEntry -fieldbackground #333 -background #333 -foreground #fff -insertcolor #fff | |
ttk::style configure TRadiobutton -background #333 -foreground #fff | |
ttk::style map TRadiobutton -background [list active #555 disabled #111 readonly #000] | |
option add *TCombobox*Listbox.background #333 interactive | |
option add *TCombobox*Listbox.foreground #fff interactive | |
option add *TCombobox*Listbox.selectBackground blue interactive | |
option add *TCombobox*Listbox.selectForeground #fff interactive | |
option add *Listbox.Background #333 interactive | |
option add *Listbox.Foreground #fff interactive | |
option add *Text.Background #333 interactive | |
option add *Text.Foreground #fff interactive | |
ttk::style configure TSpinbox -fieldbackground #333 -background #333 -foreground #fff -insertcolor #fff -arrowcolor #fff \\ | |
.vpane.lower.commarea.buffer.frame.t \\ | |
configure -background #0d1117 -foreground #fff -insertbackground #fff \\ | |
.vpane.lower.diff.body.t configure -background #0d1117 -foreground #fff \\ | |
.vpane.files.workdir.list configure -background #0d1117 -foreground #fff \\ | |
.vpane.files.index.list configure -background #0d1117 -foreground #fff \\ | |
.about_dialog.git_logo configure -background #333 | |
# {PATCHED_NEEDLE_END} | |
""" | |
git_gui_path = FORCED_GIT_GUI_PATH or find_git_gui_path() | |
if not os.path.isfile(git_gui_path): | |
error_exit(f"Cannot read file '{git_gui_path}'") | |
patch_git_gui(git_gui_path) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment