feat: add server-side settings module

This commit is contained in:
Brian Krabach
2026-03-29 22:36:19 -07:00
parent 8e4f17068d
commit ffecfa69cb
2 changed files with 184 additions and 0 deletions
+66
View File
@@ -0,0 +1,66 @@
"""
Server-side settings management for muxplex.
Settings are stored at ~/.config/muxplex/settings.json.
"""
import json
from pathlib import Path
SETTINGS_PATH = Path.home() / ".config" / "muxplex" / "settings.json"
DEFAULT_SETTINGS: dict = {
"default_session": None,
"sort_order": "manual",
"hidden_sessions": [],
"window_size_largest": False,
"auto_open_created": True,
"new_session_template": "tmux new-session -d -s {name}",
}
def load_settings() -> dict:
"""Load settings from disk, merging saved values over defaults.
Returns DEFAULT_SETTINGS if the file does not exist or contains corrupt JSON.
Unknown keys in the file are ignored.
"""
result = DEFAULT_SETTINGS.copy()
try:
text = SETTINGS_PATH.read_text()
data = json.loads(text)
for key in DEFAULT_SETTINGS:
if key in data:
result[key] = data[key]
except FileNotFoundError:
pass
except (json.JSONDecodeError, Exception):
pass
return result
def save_settings(data: dict) -> None:
"""Save settings to disk, merging *data* with defaults first.
Creates parent directories as needed. Writes JSON with indent=2 and a
trailing newline.
"""
merged = DEFAULT_SETTINGS.copy()
for key in DEFAULT_SETTINGS:
if key in data:
merged[key] = data[key]
SETTINGS_PATH.parent.mkdir(parents=True, exist_ok=True)
SETTINGS_PATH.write_text(json.dumps(merged, indent=2) + "\n")
def patch_settings(patch: dict) -> dict:
"""Merge known keys from *patch* into the current settings, save, and return result.
Unknown keys in *patch* are silently ignored.
"""
current = load_settings()
for key in DEFAULT_SETTINGS:
if key in patch:
current[key] = patch[key]
save_settings(current)
return current