__RUSTIMAL=1
export -n PROMPT_COMMAND
__rustimal_first=1
__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
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=
eval 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
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"
printf '\e]2;rustimal:%s\a' "$PWD"
local sep='\n'
if [ -n "$__rustimal_first" ]; then
sep=''
unset __rustimal_first
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
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
shopt -s autocd 2>/dev/null
bind 'set colored-stats on' 2>/dev/null
bind 'set colored-completion-prefix on' 2>/dev/null
bind 'set visible-stats on' 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
}
__rustimal_code_ready() {
[ -n "$RUSTIMAL_REVIEW_TOKEN" ] && [ -n "$RUSTIMAL_STATE" ] && [ -t 0 ] && [ -t 1 ]
}
__rustimal_code_send() {
printf '%s\n%s\n' "$(cygpath -aw -- "$2")" "$3" >"$RUSTIMAL_STATE/code-$RUSTIMAL_PANE.txt" || return
printf '\e]2;__rustimal_code__:%s:%s|%s\a' "$RUSTIMAL_REVIEW_TOKEN" "$PWD" "$1"
local answer
if ! IFS= read -rs -t 3 answer; then
printf 'rustimal did not answer; if you just updated it, open a new window\n' >&2
return 1
fi
}
vscode() {
if [[ $# -gt 1 || "$1" == -* ]] || ! __rustimal_code_ready; then
command code "${@:-.}"
return
fi
local target="${1:-.}" at=""
if [ ! -e "$target" ]; then
if [[ "$target" =~ ^(.+):([0-9]+:[0-9]+)$ && -f "${BASH_REMATCH[1]}" ]] ||
[[ "$target" =~ ^(.+):([0-9]+)$ && -f "${BASH_REMATCH[1]}" ]]; then
target="${BASH_REMATCH[1]}" at="${BASH_REMATCH[2]}"
else
printf 'vscode: %s: no such file or folder\n' "$target" >&2
return 1
fi
fi
local accepted="$RUSTIMAL_STATE/vscode-license-accepted"
if [ ! -f "$accepted" ]; then
printf "VS Code runs in this pane through Microsoft's VS Code Server, which is downloaded\n"
printf 'from Microsoft the first time and has license terms of its own:\n'
printf ' https://aka.ms/vscode-server-license\n'
local reply=""
read -rp 'Do you accept them? [y/N] ' reply
if [[ "$reply" != [yY] && "$reply" != [yY][eE][sS] ]]; then
printf 'Not accepted, so nothing was started. `code` still opens your installed VS Code.\n'
return 1
fi
: >"$accepted" || return
fi
__rustimal_code_send vscode "$target" "$at"
}
__rustimal_open_here() {
__rustimal_code_ready && [ -f "$RUSTIMAL_STATE/vscode-license-accepted" ] && [ -f "$1" ] || return 1
case "${1,,}" in
*.html | *.htm | *.svg | *.bat | *.cmd | *.exe | *.com | *.lnk | *.msi) return 1 ;;
esac
[ ! -s "$1" ] || grep -Iq . -- "$1"
}
open() {
local t m
[ $# -eq 0 ] && set -- .
for t in "$@"; do
case "$t" in
*://*) ;;
*)
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]}" ] && __rustimal_open_here "${BASH_REMATCH[1]}"; then
__rustimal_code_send open "${BASH_REMATCH[1]}" "${BASH_REMATCH[2]}${BASH_REMATCH[3]:+:${BASH_REMATCH[3]}}"
continue
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
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
if __rustimal_open_here "$t"; then
__rustimal_code_send open "$t" ""
continue
fi
t=$(cygpath -w -- "$t" 2>/dev/null || printf '%s' "$t") ;;
esac
cmd.exe //c start "" "$t"
done
}
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 "$@"
}
t3() {
local dir="$PWD" number=""
case $#:$1 in
0:) ;;
*:-l | *:--list | 1:stop)
"$RUSTIMAL_EXE" --t3 "$@"
return ;;
1:*[!0-9]* | 1:)
if ! { [[ "$1" == . || "$1" == .. || "$1" == */* || "$1" == *\\* ]] && [ -d "$1" ]; }; then
__rustimal_t3cli "$@"
return
fi
dir=$(cd -- "$1" && pwd) || return ;;
1:*) number="$1" ;;
*)
__rustimal_t3cli "$@"
return ;;
esac
if [ -z "$RUSTIMAL_REVIEW_TOKEN" ] || [ ! -t 0 ] || [ ! -t 1 ]; then
"$RUSTIMAL_EXE" --t3 ${number:+"$number"} "$(cygpath -aw -- "$dir")"
return
fi
printf '\e]2;__rustimal_t3__:%s:%s|%s\a' "$RUSTIMAL_REVIEW_TOKEN" "$dir" "$number"
local answer
if ! IFS= read -rs -t 3 answer; then
printf 'rustimal did not answer; if you just updated it, open a new window\n' >&2
return 1
fi
}
embed() {
local foreground="" quiet=""
while [ $# -gt 0 ]; do
case "$1" in
-f | --foreground) foreground=1 ;;
--quiet) quiet=1 ;;
*) break ;;
esac
shift
done
local kind="" rest=()
if [ "$1" = browser ] || [ "$1" = fork ]; then
kind="$1" quiet=1
shift
rest=("$@")
elif [ $# -eq 0 ]; then
if [ -f src-tauri/tauri.conf.json ] || [ -f tauri.conf.json ]; then
if grep -q '"tauri"' package.json 2>/dev/null; then
set -- npm run tauri dev
else
set -- cargo tauri dev
fi
elif grep -q '"electron"' package.json 2>/dev/null; then
set -- npm start
else
printf 'usage: embed [-f] COMMAND [ARGS] | embed browser [PAGE] | embed fork [FOLDER]\n' >&2
printf ' (with no command, in a Tauri or Electron project, its app is run)\n' >&2
return 2
fi
rest=("$@")
else
rest=("$@")
fi
local winpid="" reuse=""
if [ -n "$RUSTIMAL_REVIEW_TOKEN" ] && [ -t 0 ] && [ -t 1 ]; then
winpid=$(cat "/proc/$$/winpid" 2>/dev/null)
fi
if [ -n "$winpid" ]; then
printf '\e]2;__rustimal_embed__:%s:%s|start:%s%s\a' "$RUSTIMAL_REVIEW_TOKEN" "$PWD" "$winpid" "${kind:+:$kind}"
local answer
if ! IFS= read -rs -t 3 answer; then
printf 'rustimal did not answer; if you just updated it, open a new window\n' >&2
return 1
fi
[ "$answer" = reuse ] && reuse=1
fi
if [ -n "$kind" ]; then
set -- "${RUSTIMAL_EXE:-rustimal}" "--$kind" ${reuse:+--reuse} "${rest[@]}"
else
set -- "${rest[@]}"
fi
if [ -n "$foreground" ] || { [ -z "$winpid" ] && [ -z "$kind" ]; }; then
"$@"
return
fi
local log=/dev/null
if [ -z "$quiet" ] && [ -n "$RUSTIMAL_STATE" ] && [ -n "$RUSTIMAL_PANE" ]; then
log="$RUSTIMAL_STATE/embed-$RUSTIMAL_PANE.log"
printf 'embed: running in the background; its output is in %s\n' "$log"
fi
{ "$@" </dev/null >"$log" 2>&1 & disown $!; } 2>/dev/null
return 0
}
browser() {
embed browser "$@"
}
zen() {
embed browser "$@"
}
search() {
if [ $# -eq 0 ]; then
printf 'usage: search TERMS...\n' >&2
return 2
fi
embed browser --search "$@"
}
fork() {
embed fork "$@"
}
__rustimal_t3cli() {
local cli
cli=$(type -P t3) || cli="${T3CODE_HOME:-$HOME/.t3}/bin/t3.cmd"
"$cli" "$@"
}
__rustimal_panel() {
printf '\e]2;__rustimal_review__:%s:%s|%s\a' "$RUSTIMAL_REVIEW_TOKEN" "$PWD" "$*"
local answer
if ! IFS= read -rs -t 3 answer; then
printf 'rustimal did not answer; `review --print` shows the review here instead\n' >&2
return 1
fi
}
review() {
case " $* " in
*" --print "* | *" --json "* | *" --prs "* | *" --explain "* | *" --help "* | *" -h "* | *" --diff "* | *" --desc "*)
"$RUSTIMAL_EXE" --review "$@"
return ;;
esac
if [ -n "$RUSTIMAL_REVIEW_TOKEN" ] && [ -t 1 ]; then
__rustimal_panel "$@"
else
"$RUSTIMAL_EXE" --review "$@"
fi
}
pr() {
if [ -n "$RUSTIMAL_REVIEW_TOKEN" ] && [ -t 0 ] && [ -t 1 ]; then
if [ $# -eq 0 ]; then
__rustimal_panel
return
fi
if [ $# -ge 1 ] && [ $# -le 2 ] && [ ! -e "$1" ] && [ "$1" = new ]; then
__rustimal_panel --new ${2:+"$2"}
return
fi
if [ $# -eq 1 ] && [ ! -e "$1" ] && [[ "$1" =~ ^#?[0-9]+$ ]]; then
__rustimal_panel "#${1#\#}"
return
fi
fi
command pr "$@"
}
__rustimal_do() {
local agent="${DO_AGENT:-claude}"
case "$1" in
-x) agent=codex; shift ;;
-c) agent=claude; shift ;;
esac
local task="$*"
if [ -z "$RUSTIMAL_REVIEW_TOKEN" ] || [ -z "$RUSTIMAL_STATE" ] || [ ! -t 0 ] || [ ! -t 1 ]; then
case "$agent" in
codex) printf '%s' "$task" | command codex exec --sandbox workspace-write - ;;
*) printf '%s' "$task" | command claude -p --permission-mode acceptEdits ;;
esac
return
fi
printf '%s' "$task" >"$RUSTIMAL_STATE/do-$RUSTIMAL_PANE.txt" || return
printf '\e]2;__rustimal_do__:%s:%s|%s\a' "$RUSTIMAL_REVIEW_TOKEN" "$PWD" "$agent"
local answer
if ! IFS= read -rs -t 3 answer; then
printf 'rustimal did not answer; if you just updated it, open a new window\n' >&2
return 1
fi
}
doit() {
__rustimal_do "$@"
}
__rustimal_more=0
__rustimal_enter() {
local more=$__rustimal_more
__rustimal_more=0
[ "$more" = 1 ] && return
[[ "$READLINE_LINE" =~ ^[[:space:]]*do([[:space:]]+(.*))?$ ]] || return
local task="${BASH_REMATCH[2]}" flag="" quote="'\\''"
if [[ "$task" =~ ^(-[cx])([[:space:]]+(.*))?$ ]]; then
flag=" ${BASH_REMATCH[1]}" task="${BASH_REMATCH[3]}"
fi
task="${task%"${task##*[![:space:]]}"}"
[ -n "$task" ] && task=" '${task//\'/$quote}'"
READLINE_LINE="doit$flag$task"
READLINE_POINT=${#READLINE_LINE}
}
__rustimal_none=()
PS2='${__rustimal_none[(__rustimal_more=1)*0]}'"${PS2:-> }"
case "$(trap -p INT)" in
"" | *__do_more*) trap '__rustimal_more=0' INT ;;
esac
bind -x '"\C-x\C-a": __rustimal_enter' 2>/dev/null
bind '"\C-m": "\C-x\C-a\C-j"' 2>/dev/null
complete -E -o filenames -f 2>/dev/null
bind 'set completion-query-items -1' 2>/dev/null
bind 'set page-completions off' 2>/dev/null
__rustimal_preexec() {
__rustimal_syncpath
local cmd="${1//[[:cntrl:]]/ }" w="$1"
printf '\e]2;__rustimal_cmd__:%s\a' "${cmd:0:200}"
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
}
if [ -z "$(trap -p DEBUG)" ]; then
trap '[ -n "$__rustimal_armed" ] && [[ $BASH_COMMAND != __rustimal_enter && $BASH_COMMAND != __rustimal_more=* ]] && { __rustimal_armed=; __rustimal_preexec "$BASH_COMMAND"; }' DEBUG
fi
__rustimal_prompt