rustimal 0.5.0

A minimal, GPU-rendered terminal for Windows that runs Git Bash, with animated split panes and a glass look
# rustimal shell integration. Regenerated on every launch; edits will be lost.
# Loaded once through PROMPT_COMMAND so the normal Git Bash login setup still runs.

__RUSTIMAL=1
export -n PROMPT_COMMAND
__rustimal_first=1

# rustimal keeps $RUSTIMAL_STATE/winpath holding the Windows PATH from the
# registry. When an installer changes it, swap the new one into this shell's
# PATH (Git Bash's own folders stay in front), so no restart is needed.
__rustimal_syncpath() {
    local f="$RUSTIMAL_STATE/winpath" win="" new
    [ -n "$RUSTIMAL_STATE" ] && [ -r "$f" ] || return 0
    IFS= read -r win <"$f"
    [ -n "$win" ] || return 0
    if [ -z "${__rustimal_winpath+x}" ]; then
        __rustimal_winpath="$win"
        return 0
    fi
    [ "$win" = "$__rustimal_winpath" ] && return 0
    __rustimal_winpath="$win"
    new=$(cygpath -u -p "$win" 2>/dev/null) || return 0
    [ -n "$new" ] || return 0
    if [ -n "$ORIGINAL_PATH" ] && [[ ":$PATH:" == *":$ORIGINAL_PATH:"* ]]; then
        PATH=":$PATH:"
        PATH="${PATH/":$ORIGINAL_PATH:"/":$new:"}"
        PATH="${PATH#:}"
        PATH="${PATH%:}"
    else
        # PATH was rearranged by a profile script; just add what's missing.
        local e parts
        IFS=: read -ra parts <<<"$new"
        for e in "${parts[@]}"; do
            [[ -z "$e" || ":$PATH:" == *":$e:"* ]] || PATH+=":$e"
        done
    fi
    ORIGINAL_PATH="$new"
    hash -r
}

__rustimal_prompt() {
    local code=$? d="$PWD" head="" chev="35"
    __rustimal_syncpath
    if [ -n "$__rustimal_autocd" ]; then
        __rustimal_autocd=
        ls
    fi
    while :; do
        if [ -f "$d/.git/HEAD" ]; then
            read -r head <"$d/.git/HEAD"
            break
        fi
        [ -z "$d" ] || [ "$d" = "/" ] && break
        d="${d%/*}"
    done
    case "$head" in
        "ref: refs/heads/"*) __rustimal_branch="${head#ref: refs/heads/}" ;;
        "") __rustimal_branch="" ;;
        *) __rustimal_branch="${head:0:7}" ;;
    esac
    # Full path with $HOME shown as ~; the last segment is highlighted.
    local p="$PWD" tilde='~'
    case "$PWD" in
        "$HOME") p="$tilde" ;;
        "$HOME"/*) p="$tilde${PWD#"$HOME"}" ;;
    esac
    case "$p" in
        / | "$tilde") __rustimal_parent="" __rustimal_dir="$p" ;;
        */*) __rustimal_parent="${p%/*}/" __rustimal_dir="${p##*/}" ;;
        *) __rustimal_parent="" __rustimal_dir="$p" ;;
    esac
    [ "$code" -ne 0 ] && chev="31"

    # Tell rustimal where we are (shown in the header, used for new splits).
    printf '\e]2;rustimal:%s\a' "$PWD"

    local sep='\n'
    if [ -n "$__rustimal_first" ]; then
        sep=''
        unset __rustimal_first
        # One-time tip (set by rustimal on the first run that isn't installed yet).
        if [ -n "$RUSTIMAL_HINT" ]; then
            unset RUSTIMAL_HINT
            printf '\e[90mTip: run \e[0m\e[36mrustimal --install\e[0m\e[90m to add the Start menu, Win+R and right-click entries.\e[0m\n'
        fi
    fi
    # Directory and branch are referenced, not interpolated, so odd folder names stay inert.
    PS1="${sep}\[\e[36m\]\${__rustimal_parent}\[\e[1;36m\]\${__rustimal_dir}\[\e[0m\]"
    if [ -n "$__rustimal_branch" ]; then
        PS1+=" \[\e[90m\]\${__rustimal_branch}\[\e[0m\]"
    fi
    PS1+=" \[\e[${chev}m\]❯\[\e[0m\] "
    __rustimal_armed=1
}
PROMPT_COMMAND=__rustimal_prompt

# Typing just a name does the obvious thing, as long as it isn't also a command (a real
# command, alias or function always wins):
#   folder        change into it, then list it (autocd, plus the `ls` above)
#   text file     print it
#   program       run it (.exe, .bat, scripts with the executable bit or a #! line)
#   other file    open it with its default app
# Bash calls this handler only when no command of that name exists.
shopt -s autocd 2>/dev/null
command_not_found_handle() {
    local f="$1"
    if [ -f "$f" ]; then
        if [ -x "$f" ]; then
            "./$f" "${@:2}"
            return $?
        elif [ $# -eq 1 ]; then
            if [ ! -s "$f" ] || { [ "$(stat -c %s -- "$f")" -le 1048576 ] && grep -Iq . -- "$f"; }; then
                cat -- "$f"
            else
                open "$f"
            fi
            return $?
        fi
    fi
    printf 'bash: %s: command not found\n' "$1" >&2
    return 127
}

# `open` opens the current folder in Explorer; `open path` opens a file or folder with
# its default app, `open https://...` opens a URL, and `open file:20` (or `file:20:5`) opens
# the file in your editor at that line.
open() {
    local t m
    [ $# -eq 0 ] && set -- .
    for t in "$@"; do
        case "$t" in
            *://*) ;;
            *)
                # file:LINE or file:LINE:COL (the longer form first, as `.+` is greedy)
                m=""
                if [ ! -e "$t" ] && [ -x "$RUSTIMAL_EXE" ]; then
                    if [[ "$t" =~ ^(.+):([0-9]+):([0-9]+)$ ]]; then m=1; else [[ "$t" =~ ^(.+):([0-9]+)$ ]] && m=1; fi
                fi
                if [ -n "$m" ] && [ -f "${BASH_REMATCH[1]}" ]; then
                    "$RUSTIMAL_EXE" --open "$(cygpath -aw -- "${BASH_REMATCH[1]}")" "${BASH_REMATCH[2]}" ${BASH_REMATCH[3]:+"${BASH_REMATCH[3]}"}
                    continue
                fi
                # file:LINE that couldn't be opened at that line: open the plain file if it
                # exists, otherwise say so (cygpath would turn the ":" into "file50").
                if [ ! -e "$t" ] && [[ "$t" =~ ^(.+):([0-9]+)(:[0-9]+)?$ ]]; then
                    if [ -e "${BASH_REMATCH[1]}" ]; then
                        t="${BASH_REMATCH[1]}"
                    else
                        printf 'open: %s: no such file
' "$t" >&2
                        continue
                    fi
                fi
                t=$(cygpath -w -- "$t" 2>/dev/null || printf '%s' "$t") ;;
        esac
        cmd.exe //c start "" "$t"
    done
}

# `claude` with no arguments asks rustimal to show its session picker,
# then reads the choice back: a session id, "new", or an empty line to cancel.
claude() {
    if [ $# -eq 0 ] && [ -t 0 ] && [ -t 1 ]; then
        printf '\e]2;__rustimal_pick__:%s\a' "$PWD"
        local choice=""
        IFS= read -rs choice
        case "$choice" in
            "") return 0 ;;
            new) ;;
            *) set -- --resume "$choice" ;;
        esac
    fi
    printf '\e]2;__rustimal_claude__\a\e[H\e[2J'
    if [ -n "$RUSTIMAL_CLAUDE_SETTINGS" ] && [ -f "$RUSTIMAL_CLAUDE_SETTINGS" ]; then
        set -- --settings "$RUSTIMAL_CLAUDE_SETTINGS" "$@"
    fi
    command claude "$@"
}

# Tab on an empty line would make bash look up every command on PATH. With a
# Windows-sized PATH that takes ~10 seconds (the terminal looks frozen) and then
# prints thousands of names. Complete the files here instead, like `ls`.
complete -E -o filenames -f 2>/dev/null
# Never stop to ask "Display all N possibilities?"; just list them, all at once (rustimal
# scrolls, so there's no need for a --More-- pager that would also swallow clicks and typing).
bind 'set completion-query-items -1' 2>/dev/null
bind 'set page-completions off' 2>/dev/null

# Right before a command line runs: sync PATH, so a change made while the prompt
# was waiting applies to that command, and tell rustimal which command is starting
# so it can say when it's done (taskbar flash for long commands in the background).
__rustimal_preexec() {
    __rustimal_syncpath
    local cmd="${1//[[:cntrl:]]/ }" w="$1"
    printf '\e]2;__rustimal_cmd__:%s\a' "${cmd:0:200}"
    # A lone folder name that isn't also a command: autocd changes into it, and the next
    # prompt lists it. (A name with spaces comes quoted: 'My Folder'.)
    case "$w" in
        \'*\') w="${w#\'}"; w="${w%\'}" ;;
        \"*\") w="${w#\"}"; w="${w%\"}" ;;
        *[[:space:]]*) w="" ;;
    esac
    if [ -n "$w" ] && [ -d "$w" ] && [ -z "$(type -t -- "$w" 2>/dev/null)" ]; then
        __rustimal_autocd=1
    fi
}

# Runs once per command line, and only if nothing else (e.g. bash-preexec)
# already uses the DEBUG trap.
if [ -z "$(trap -p DEBUG)" ]; then
    trap '[ -n "$__rustimal_armed" ] && { __rustimal_armed=; __rustimal_preexec "$BASH_COMMAND"; }' DEBUG
fi

# We are being sourced from the first PROMPT_COMMAND run; build the first prompt now.
__rustimal_prompt