fix(fastfetch): make fastfetch theme sync non-destructive to preserve layout and modules

This commit is contained in:
Sho 2026-08-01 21:19:39 +09:00
parent 18f6c8fc88
commit 4cb5174b1d

View File

@ -1006,10 +1006,35 @@ theme[upload_end]="#ff80bf"
pass pass
def sync_fastfetch(bg, surface, current_line, fg, accent, sub_accent): def sync_fastfetch(bg, surface, current_line, fg, accent, sub_accent):
import re
fastfetch_dir = os.path.expanduser('~/.config/fastfetch') fastfetch_dir = os.path.expanduser('~/.config/fastfetch')
os.makedirs(fastfetch_dir, exist_ok=True) os.makedirs(fastfetch_dir, exist_ok=True)
conf_path = os.path.join(fastfetch_dir, 'config.jsonc') conf_path = os.path.join(fastfetch_dir, 'config.jsonc')
if os.path.exists(conf_path):
try:
with open(conf_path, 'r', encoding='utf-8') as f:
content = f.read()
modified = False
if '"keys"' in content:
content, count1 = re.subn(r'"keys"\s*:\s*"[^"]*"', f'"keys": "{accent}"', content)
if count1 > 0: modified = True
if '"title"' in content:
content, count2 = re.subn(r'"title"\s*:\s*"[^"]*"', f'"title": "{sub_accent}"', content)
if count2 > 0: modified = True
if not modified and '"display"' in content:
content = re.sub(r'"display"\s*:\s*\{', f'"display": {{\n "color": {{\n "keys": "{accent}",\n "title": "{sub_accent}"\n }},', content, count=1)
modified = True
if modified:
with open(conf_path, 'w', encoding='utf-8') as f:
f.write(content)
return
except Exception:
pass
fastfetch_jsonc = f"""{{ fastfetch_jsonc = f"""{{
"$schema": "https://github.com/fastfetch-cli/fastfetch/raw/dev/doc/json_schema.json", "$schema": "https://github.com/fastfetch-cli/fastfetch/raw/dev/doc/json_schema.json",
"display": {{ "display": {{
@ -1039,7 +1064,7 @@ def sync_fastfetch(bg, surface, current_line, fg, accent, sub_accent):
}} }}
""" """
try: try:
with open(conf_path, 'w') as f: with open(conf_path, 'w', encoding='utf-8') as f:
f.write(fastfetch_jsonc) f.write(fastfetch_jsonc)
except Exception: except Exception:
pass pass