# Nuguland appearance compatibility boundary; Config owns the daily UI. # # The existing nugu/mode file is desired state, not a cross-app transaction. # Installed Nugu artifacts are inputs; generators are never runtime dependencies. const self = path self const modes = [dark light] def fail [message: string] { error make {msg: $message} } def kind [path: string] { if ($path | path exists --no-symlink) { $path | path type } else { "missing" } } def regular-or-missing [path: string] { if (kind $path) not-in [missing file] { fail $"refusing non-regular runtime file: ($path)" } } def directory-or-missing [path: string] { if (kind $path) not-in [missing dir] { fail $"refusing non-directory runtime path: ($path)" } } def roots [] { let home = $env.HOME let state = ($env.XDG_STATE_HOME? | default "") let config = ($env.XDG_CONFIG_HOME? | default "") let roots = { home: $home directory: ((if ($state | is-empty) { $home | path join .local state } else { $state }) | path join nugu) config: (if ($config | is-empty) { $home | path join .config } else { $config }) } if not (($roots.directory | str starts-with "/") and ($roots.config | str starts-with "/")) { fail "XDG state/config paths must be absolute" } $roots } # The selected mode; a missing or unreadable selection is dark. def effective [directory: string] { let value = try { open --raw ($directory | path join mode) | into binary | decode utf-8 | str trim } catch { "" } if $value in $modes { $value } else { "dark" } } # Both variants: an incomplete installation must not select a new mode. def installed [config: string] { $modes | each {|mode| let files = [ (open --raw ($config | path join niri themes $"nugu-($mode).kdl") | into binary) (open --raw ($config | path join aerc stylesets $"nugu-($mode)") | into binary) ] if ($files | any {|data| $data | is-empty }) { fail $"empty installed ($mode) theme" } {mode: $mode, files: $files} } } # A temporary beside `path` holding `data`, or null when `path` already holds it. def stage [path: string, data: binary] { regular-or-missing $path if ($path | path exists) and (open --raw $path | into binary) == $data { return null } let made = (^mktemp -p ($path | path dirname) .theme-XXXXXX | complete) if $made.exit_code != 0 { fail ($made.stderr | str trim) } let temporary = ($made.stdout | str trim) try { $data | save --raw --force $temporary } catch {|error| rm --force $temporary fail $error.msg } $temporary } def replace [temporary: string, path: string] { let moved = (^mv -T -f $temporary $path | complete) if $moved.exit_code != 0 { fail ($moved.stderr | str trim) } } # A consumer's failure line, or null; each consumer has five seconds. def consumer [label: string, accepted: list, command: list] { let result = (^timeout 5 ...$command | complete) if $result.exit_code in $accepted { null } else { $"($label): ($command.0) exited ($result.exit_code): ($result.stderr | str trim)" } } # Runs under the state lock: select, stage, commit authority, then publish. def reconcile [operation: string, wanted?: string] { let roots = roots let directory = $roots.directory let current = (effective $directory) let mode = match $operation { "set" => $wanted "toggle" => (if $current == "dark" { "light" } else { "dark" }) _ => $current } let files = (installed $roots.config | where mode == $mode | first | get files) let bridge = ($roots.home | path join .nugu-runtime) if (kind $bridge) != "missing" and ((kind $bridge) != "symlink" or (^readlink $bridge | str trim) != $directory) { fail $"refusing unrelated runtime path: ($bridge)" } directory-or-missing ($directory | path join aerc) mkdir ($directory | path join aerc) let targets = [($directory | path join niri.kdl) ($directory | path join aerc nugu)] let authority = ($directory | path join mode) regular-or-missing $authority mut pending = [] # Stage every file before committing authority; permission/storage # failures here cannot change the selected mode or desktop preference. let failure = try { for index in 0..1 { let path = ($targets | get $index) $pending ++= [{path: $path, temporary: (stage $path ($files | get $index))}] } let selection = if $operation == "prepare" { null } else { stage $authority ($"($mode)\n" | into binary) } if $selection != null { $pending ++= [{path: $authority, temporary: $selection}] } if (kind $bridge) == "missing" { let linked = (^ln -s $directory $bridge | complete) if $linked.exit_code != 0 { fail ($linked.stderr | str trim) } } if $selection != null { replace $selection $authority } # Authority is now committed. Each consumer can fail independently. # Startup / prepare / same-mode set reconcile, never roll back intent. mut failures = [(consumer "desktop preference" [0] [gsettings set org.gnome.desktop.interface color-scheme $"prefer-($mode)"])] mut mail_ready = false for item in ($pending | first 2) { let problem = try { if $item.temporary != null { replace $item.temporary $item.path } null } catch {|error| $error.msg } if $problem != null { $failures ++= [$"($item.path): ($problem)"] } else if $item.path == $targets.1 { $mail_ready = true } } if $operation != "prepare" and $mail_ready and ($env.XDG_RUNTIME_DIR? | default "" | is-not-empty) { $failures ++= [(consumer "aerc reload" [0 1] [pkill --signal HUP --uid (^id -u | str trim) --exact aerc])] } let failed = ($failures | compact) if ($failed | is-empty) { null } else { $"selected ($mode); incomplete reconciliation: ($failed | str join '; '); retry set ($mode)" } } catch {|error| $error.msg } for item in $pending { if $item.temporary != null { rm --force $item.temporary } } if $failure != null { fail $failure } $mode } def invoke [operation: string, mode?: string] { let roots = roots if $operation == "status" { return (effective $roots.directory) } # Preflight installed inputs before creating even the lock directory. installed $roots.config for tool in [gsettings pkill flock timeout] { if (which $tool | is-empty) { fail $"missing runtime executable: ($tool)" } } directory-or-missing $roots.directory mkdir $roots.directory let lock = ($roots.directory | path join .theme.lock) regular-or-missing $lock let arguments = if $mode == null { [$operation] } else { [$operation $mode] } let result = (^flock $lock nu --no-config-file $self --locked ...$arguments | complete) if $result.exit_code != 0 { fail ($result.stderr | str trim | str replace --regex '^theme: ' '') } $result.stdout | str trim } export def status [] { invoke status } export def prepare [] { invoke prepare } export def set [mode: string] { if $mode not-in $modes { fail $"invalid mode: ($mode)" } invoke set $mode } export def toggle [] { invoke toggle } # `nu theme.nu [mode]`: the mode on stdout, or one plain line on stderr. def main [operation: string, mode?: string, --locked] { try { if $operation not-in [status prepare set toggle] { fail $"invalid operation: ($operation)" } if ($operation == "set") != ($mode in $modes) { fail "only set requires a mode: dark or light" } print (if $locked { reconcile $operation $mode } else { invoke $operation $mode }) } catch {|error| print --stderr $"theme: ($error.msg)" exit 1 } }