The website commits too, so `usaco sync` pulls its edits before pushing. tests/*.out were swallowed by the *.out rule; they are expected outputs the site shows, so re-include them. Co-Authored-By: Claude Opus 5.5 <[email protected]>
175 lines
6.9 KiB
Bash
175 lines
6.9 KiB
Bash
# usaco interactive | i | repl
|
|
# A REPL that holds a "current problem" so commands don't need the name.
|
|
# Every command shells out to the dispatcher, so all existing commands work unchanged
|
|
# and a failing command can't kill the loop.
|
|
|
|
CURRENT_FILE="$ROOT/.current"
|
|
STACK_FILE="$ROOT/.current_stack"
|
|
HISTFILE="$ROOT/.usaco_history"
|
|
|
|
# commands that take a problem name (the selection is injected when you omit it)
|
|
WITH_PROBLEM="run build test done undo skip io cat copy edit open path template reset"
|
|
# commands that take no problem
|
|
NO_PROBLEM="list stats clean sync"
|
|
# REPL-only words (used for completion and for 'help')
|
|
REPL_WORDS="s use none push pop stack help quit exit"
|
|
|
|
cur=""
|
|
[[ -r "$CURRENT_FILE" ]] && cur="$(<"$CURRENT_FILE")"
|
|
if [[ -n "$cur" ]] && ! find_problem "$cur" >/dev/null 2>&1; then
|
|
echo "${YEL}note:${RST} '$cur' no longer exists — selection cleared"
|
|
cur=""
|
|
fi
|
|
|
|
set_current() { cur="$1"; printf '%s\n' "$cur" > "$CURRENT_FILE"; }
|
|
|
|
status_of() { # $1 = name; echoes solving|solved|skipped, or nothing
|
|
local d; d="$(find_problem "$1" 2>/dev/null)" || return 1
|
|
d="${d#$ROOT/}"; echo "${d%%/*}"
|
|
}
|
|
|
|
real_name() { # $1 = name/prefix; echoes the actual directory name
|
|
local d; d="$(find_problem "$1" 2>/dev/null)" || return 1
|
|
basename "$d"
|
|
}
|
|
|
|
show_current() {
|
|
if [[ -z "$cur" ]]; then echo "no problem selected"
|
|
else echo "$cur ${DIM}($(status_of "$cur"))${RST}"; fi
|
|
}
|
|
|
|
# --- problem stack: push the current problem aside, work elsewhere, pop back ---
|
|
stack=()
|
|
if [[ -r "$STACK_FILE" ]]; then
|
|
while IFS= read -r l; do [[ -n "$l" ]] && stack+=("$l"); done < "$STACK_FILE"
|
|
fi
|
|
save_stack() { printf '%s\n' "${stack[@]+"${stack[@]}"}" > "$STACK_FILE"; }
|
|
|
|
show_stack() {
|
|
if [[ ${#stack[@]} -eq 0 ]]; then echo "stack empty"; return; fi
|
|
local i
|
|
for (( i=${#stack[@]}-1; i>=0; i-- )); do
|
|
echo " ${DIM}$((${#stack[@]}-i)).${RST} ${stack[i]} ${DIM}($(status_of "${stack[i]}"))${RST}"
|
|
done
|
|
}
|
|
|
|
help_text() { # $1 = optional command to explain
|
|
if [[ -n "${1:-}" ]]; then
|
|
# delegate to the dispatcher's own usage text, grepped for that command
|
|
"$ROOT/usaco" 2>&1 | grep -E "^ usaco ($1|[a-z/]*\b$1\b)" || echo "no help for '$1'"
|
|
return
|
|
fi
|
|
cat <<EOF
|
|
${DIM}selection${RST}
|
|
s <name> select a problem (a bare name or prefix works too)
|
|
s show the current problem
|
|
none clear the selection
|
|
push [<name>] stash the current problem, optionally select <name>
|
|
pop restore the last pushed problem
|
|
stack show the stashed problems
|
|
${DIM}problem commands${RST} ${DIM}(name optional — omit it to use the selection; giving one switches to it)${RST}
|
|
$WITH_PROBLEM
|
|
${DIM}other${RST}
|
|
$NO_PROBLEM, new <name>
|
|
help [<cmd>] this, or the dispatcher's usage line for <cmd>
|
|
!cmd run a shell command in the problem dir
|
|
q quit
|
|
EOF
|
|
}
|
|
|
|
_usaco_comp() { # completes REPL commands and problem names
|
|
local w="${COMP_WORDS[COMP_CWORD]}" names=() d
|
|
shopt -s nullglob
|
|
for d in "$ROOT"/solving/*/ "$ROOT"/solved/*/ "$ROOT"/skipped/*/; do names+=("$(basename "$d")"); done
|
|
shopt -u nullglob
|
|
if [[ $COMP_CWORD -eq 0 ]]; then
|
|
COMPREPLY=($(compgen -W "$WITH_PROBLEM $NO_PROBLEM $REPL_WORDS new ${names[*]}" -- "$w"))
|
|
else
|
|
COMPREPLY=($(compgen -W "${names[*]}" -- "$w"))
|
|
fi
|
|
}
|
|
complete -E -F _usaco_comp 2>/dev/null || true
|
|
history -r 2>/dev/null || true
|
|
|
|
echo "usaco interactive — 's <name>' selects, 'none' clears, '!cmd' shells out, 'q' quits"
|
|
|
|
while :; do
|
|
if [[ -n "$cur" ]]; then prompt="usaco ${DIM}[${RST}$cur${DIM}:$(status_of "$cur")]${RST}> "
|
|
else prompt="usaco ${DIM}[none]${RST}> "; fi
|
|
|
|
line=""
|
|
read -e -r -p "$prompt" line || { echo; break; }
|
|
[[ -z "${line// }" ]] && continue
|
|
history -s "$line"; history -a 2>/dev/null || true
|
|
|
|
# shell escape: run in the problem dir if one is selected
|
|
if [[ "$line" == '!'* ]]; then
|
|
( [[ -n "$cur" ]] && cd "$(find_problem "$cur")" || cd "$ROOT"; eval "${line#!}" ) || true
|
|
continue
|
|
fi
|
|
|
|
read -r -a words <<< "$line"
|
|
c="${words[0]}"; args=("${words[@]:1}")
|
|
|
|
case "$c" in
|
|
q|quit|exit) break ;;
|
|
help|h|'?') help_text "${args[0]:-}"; continue ;;
|
|
stack) show_stack; continue ;;
|
|
push)
|
|
[[ -n "$cur" ]] && { stack+=("$cur"); save_stack; }
|
|
if [[ ${#args[@]} -ge 1 ]]; then
|
|
if n="$(real_name "${args[0]}")"; then set_current "$n"
|
|
else find_problem "${args[0]}" >/dev/null || true; fi
|
|
else set_current ""; fi
|
|
continue ;;
|
|
pop)
|
|
if [[ ${#stack[@]} -eq 0 ]]; then echo "stack empty" >&2; continue; fi
|
|
n="${stack[-1]}"; unset 'stack[-1]'; save_stack
|
|
if [[ -n "$(status_of "$n")" ]]; then set_current "$n"
|
|
else echo "${YEL}note:${RST} '$n' no longer exists — selection cleared"; set_current ""; fi
|
|
continue ;;
|
|
s|use)
|
|
if [[ ${#args[@]} -eq 0 ]]; then show_current
|
|
elif n="$(real_name "${args[0]}")"; then set_current "$n"
|
|
else find_problem "${args[0]}" >/dev/null || true; fi
|
|
continue ;;
|
|
none|-) set_current ""; continue ;;
|
|
n|new)
|
|
[[ ${#args[@]} -ge 1 ]] || { echo "usage: n <name> [io-name] [--url <link>]" >&2; continue; }
|
|
if "$ROOT/usaco" new "${args[@]}"; then set_current "${args[0]}"; fi
|
|
continue ;;
|
|
esac
|
|
|
|
# expand short aliases to full names so the tables below match
|
|
case "$c" in
|
|
r) c=run ;; b) c=build ;; t) c=test ;; d) c=done ;; u) c=undo ;; sk) c=skip ;;
|
|
e|vim) c=edit ;; o) c=open ;; l|ls) c=list ;; st) c=stats ;; p) c=path ;;
|
|
tpl) c=template ;; rm|delete) c=reset ;; cp) c=copy ;; sy) c=sync ;;
|
|
esac
|
|
|
|
if [[ " $NO_PROBLEM " == *" $c "* ]]; then
|
|
"$ROOT/usaco" "$c" "${args[@]}" || true
|
|
continue
|
|
fi
|
|
|
|
if [[ " $WITH_PROBLEM " == *" $c "* ]]; then
|
|
# 'test create' keeps its subword first; the name goes after it
|
|
pre=(); if [[ "$c" == test && "${args[0]:-}" =~ ^(create|add|new)$ ]]; then pre=("${args[0]}"); args=("${args[@]:1}"); fi
|
|
if [[ ${#args[@]} -ge 1 ]]; then # explicit name: use it and switch to it
|
|
n="$(real_name "${args[0]}")" || { find_problem "${args[0]}" >/dev/null || true; continue; }
|
|
set_current "$n"; args=("${args[@]:1}")
|
|
elif [[ -z "$cur" ]]; then
|
|
echo "Error: no problem selected — use: s <name>" >&2; continue
|
|
else n="$cur"; fi
|
|
"$ROOT/usaco" "$c" "${pre[@]}" "$n" "${args[@]}" || true
|
|
# done/skip/undo move the dir, so re-resolve the name for the prompt's status
|
|
[[ -n "$(status_of "$cur")" ]] || set_current ""
|
|
continue
|
|
fi
|
|
|
|
# a bare problem name (or prefix) selects it
|
|
if n="$(real_name "$c")" && [[ ${#args[@]} -eq 0 ]]; then set_current "$n"; continue; fi
|
|
|
|
echo "Error: unknown command '$c' — try 'help', 's <name>', or a command like 'run'" >&2
|
|
done
|