Luigit
repositories / dotfiles

dotfiles

bugabingas dorkfiles

owned by admin

nushell/modules/termux-theme.nu

Raw
# Resolve the module itself first because ~/.config/nushell is a symlink.
const themes = (path self | path expand | path dirname | path join ".." ".." "termux" "themes" | path expand)

def modes [] { [dark light] }

# Select a generated Nugu palette and reload Termux without restarting sessions.
export def main [mode: string@modes] {
    if $mode not-in [dark light] {
        error make {msg: "Expected dark or light"}
    }
    if ($env.TERMUX_VERSION? | is-empty) or (which termux-reload-settings | is-empty) {
        error make {msg: "Run termux-theme inside Termux with termux-reload-settings available"}
    }

    let source = ($themes | path join $"nugu-($mode).properties")
    # Read before touching the active configuration, failing on missing palettes.
    let colors = (open --raw $source)
    let directory = ($nu.home-dir | path join ".termux")
    let active = ($directory | path join "colors.properties")
    let backup = ($directory | path join "colors.properties.before-nugu")

    # Do not follow links into managed palettes or outside HOME.
    if ($directory | path type) not-in [null dir] {
        error make {msg: "~/.termux must be a regular directory, not a symlink"}
    }
    for file in [$active $backup] {
        if ($file | path type) not-in [null file] {
            error make {msg: $"Refusing non-regular file: ($file)"}
        }
    }
    mkdir $directory
    if ($active | path exists) and not ($backup | path exists) {
        cp --preserve [mode timestamps] $active $backup
    }
    $colors | save --raw --force $active

    let result = (^termux-reload-settings | complete)
    if $result.exit_code != 0 {
        error make {msg: $"Nugu ($mode) saved, but Termux reload failed with exit code ($result.exit_code)"}
    }
    print $"Termux: Nugu ($mode)"
}