From 1b78b3587623576ba03efcaa7479ddc4ed67a559 Mon Sep 17 00:00:00 2001 From: Adam French Date: Thu, 24 Sep 2026 13:12:37 +0100 Subject: [PATCH] Add usaco interactive REPL with current-problem context Co-Authored-By: Claude Code --- .gitignore | 3 + lib/cmd/interactive.sh | 174 +++++++++++++++++++++++++++++++++++++++++ usaco | 9 +++ 3 files changed, 186 insertions(+) create mode 100644 lib/cmd/interactive.sh diff --git a/.gitignore b/.gitignore index e4fea15..133b4c9 100644 --- a/.gitignore +++ b/.gitignore @@ -3,3 +3,6 @@ solving/*/main solved/*/main skipped/*/main +.current +.usaco_history +.current_stack diff --git a/lib/cmd/interactive.sh b/lib/cmd/interactive.sh new file mode 100644 index 0000000..e876844 --- /dev/null +++ b/lib/cmd/interactive.sh @@ -0,0 +1,174 @@ +# 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" +# 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 < select a problem (a bare name or prefix works too) + s show the current problem + none clear the selection + push [] stash the current problem, optionally select + 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 + help [] this, or the dispatcher's usage line for + !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 ' 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 [io-name] [--url ]" >&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 ;; + 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 " >&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 ', or a command like 'run'" >&2 +done diff --git a/usaco b/usaco index a9c9727..d96027d 100755 --- a/usaco +++ b/usaco @@ -1,6 +1,14 @@ #!/usr/bin/env bash # usaco - manage USACO problems # Usage: +# usaco i Interactive mode: holds a current problem so commands +# drop the name (aliases: interactive, repl) +# s select a problem (bare name works too) +# s show the current problem +# none clear the selection +# r / t / e act on the current problem +# !cmd run a shell command in the problem dir +# q quit # usaco new [io-name] [--url ] # Create solving/ from template.cpp # (io-name sets freopen filenames, defaults to ) @@ -41,6 +49,7 @@ usage() { awk 'NR>1 { if (!/^#/) exit; sub(/^# ?/,""); print }' "$0"; exit 1; } cmd="${1:-}"; shift || true case "$cmd" in + interactive|i|repl) cmd=interactive ;; new|n) cmd=new ;; run|r) cmd=run ;; build|b) cmd=build ;;