Files
dotfiles/dot_config/modify_mimeapps.list.tmpl
T
TnSb 951cdb1d36 merge mimeapps defaults via modify_ script instead of full replace
mimeapps.list is rewritten by the desktop environment, so managing it as a
whole file made chezmoi update clobber each machine's own associations. Replace
the full-file template with a modify_ script that merges only the enforced
entries (per-host browser for text/html + http/https, and the nvim text-type
defaults) into whatever the machine already has, preserving everything else.
2026-08-28 16:12:30 -04:00

70 lines
2.2 KiB
Cheetah

#!/usr/bin/env python3
{{ $browser := "firefox.desktop" }}{{ if hasPrefix "UGC" .chezmoi.hostname }}{{ $browser = "default-browser.desktop" }}{{ end }}
# chezmoi modify_ script: merge a curated set of default associations into the
# machine's existing mimeapps.list (read from stdin) instead of replacing the
# whole file, so each machine keeps its own entries.
import sys
BROWSER = "{{ $browser }}"
SECTION = "[Default Applications]"
# Associations enforced on every machine, in [Default Applications].
ENFORCED = {
"application/json": "nvim.desktop",
"application/x-docbook+xml": "nvim.desktop",
"application/x-yaml": "nvim.desktop",
"text/markdown": "nvim.desktop",
"text/x-cmake": "nvim.desktop",
"text/html": BROWSER,
"x-scheme-handler/http": BROWSER,
"x-scheme-handler/https": BROWSER,
}
def main():
lines = sys.stdin.read().splitlines()
# Split into sections, preserving header lines and body order. The first
# (headerless) section holds any preamble before the first [Header].
sections = [[None, []]]
for line in lines:
stripped = line.strip()
if stripped.startswith("[") and stripped.endswith("]"):
sections.append([stripped, []])
else:
sections[-1][1].append(line)
target = next((s for s in sections if s[0] == SECTION), None)
if target is None:
target = [SECTION, []]
sections.append(target)
remaining = dict(ENFORCED)
body = []
for line in target[1]:
key = line.split("=", 1)[0].strip() if "=" in line else None
if key in remaining:
body.append("{}={};".format(key, remaining.pop(key)))
else:
body.append(line)
# Append any still-missing enforced keys before trailing blank lines.
trailing = []
while body and body[-1].strip() == "":
trailing.insert(0, body.pop())
for key, val in ENFORCED.items():
if key in remaining:
body.append("{}={};".format(key, val))
body.extend(trailing)
target[1] = body
out = []
for header, section_body in sections:
if header is not None:
out.append(header)
out.extend(section_body)
sys.stdout.write("\n".join(out) + "\n")
main()