Brings in the stashed Rust work, merged onto the current CLI. A problem's language is whichever source it has (main.cpp or main.rs); 'usaco lang' sets the default for new problems (.lang, or $USACO_LANG), and 'new --rust/--cpp' picks per problem. template.rs is a paste-ready Rust template whose stdio() uses <io>.in/.out when present and stdin/stdout otherwise. run/test now feed <io>.in on stdin and capture stdout when the program doesn't write <io>.out itself, so Codeforces-style solutions work too. 'reset <name> rust|cpp' switches a problem's language (template is still an alias), and --debug builds Rust with overflow checks and debug assertions. 'usaco lint' runs clippy through a generated, gitignored Cargo.toml that also gives rust-analyzer a project; done/skip/undo keep it in step. LeetCode imports stay C++. Also: done stages solved/ and solving/ separately, so a problem that was never committed from solving/ still gets its "Solve" commit, and the site lists main.rs ahead of other files like it does main.cpp. Co-Authored-By: Claude Opus 5.5 <[email protected]>
187 lines
7.8 KiB
Bash
187 lines
7.8 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 reset"
|
|
# commands that take no problem
|
|
NO_PROBLEM="list stats clean sync lang"
|
|
# 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> [io] [--rust|--cpp], new <leetcode-url|daily>
|
|
lint [<name>] clippy the selection, or every rust solution
|
|
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 lint ${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>] | n <leetcode-url|daily>" >&2; continue; }
|
|
# new reports the name it created: a LeetCode link only becomes one after the fetch
|
|
created="$(mktemp)"
|
|
if USACO_CREATED="$created" "$ROOT/usaco" new "${args[@]}"; then set_current "$(<"$created")"; fi
|
|
rm -f "$created"
|
|
continue ;;
|
|
lint)
|
|
# the one command whose problem name is optional: it lints the
|
|
# selection if there is one, otherwise every rust solution
|
|
[[ ${#args[@]} -eq 0 && -n "$cur" ]] && args=("$cur")
|
|
"$ROOT/usaco" lint "${args[@]+"${args[@]}"}" || true
|
|
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|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
|
|
# 'reset rust' switches the selection's language: a language word isn't a name
|
|
lang_arg=""; [[ "$c" == reset ]] && normalize_lang "${args[0]:-}" >/dev/null 2>&1 && lang_arg=1
|
|
if [[ ${#args[@]} -ge 1 && "${args[0]}" != -* && -z "$lang_arg" ]]; 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
|