Tidy the usaco CLI: prefixes everywhere, LOCAL debug builds, git-based dates

- done/skip/undo accept unique prefixes like every other command
- builds define LOCAL (-I repo root): the template includes debugging.h
  and enables DBG/printV only locally, so main.cpp is submittable as-is
- run/build/test --debug (or USACO_DEBUG=1): ASan/UBSan + _GLIBCXX_DEBUG
- list/stats date problems by git history, not mtimes reset by checkouts
- template/tpl is now an alias of reset (which gains -y); fixes template
  dropping the STDIO line
- run shows <io>.out via detect_io instead of the first *.out
- debugging.h: size_t loop indices (no -Wsign-compare noise)
- zsh completion for i/cat/copy; README covers test, io, reset, --debug
- ignore a.out

Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
2026-10-14 22:39:16 +01:00
co-authored by Claude Opus 5.5
parent 1c647072c5
commit ac66ad6b03
18 changed files with 118 additions and 54 deletions
+2 -2
View File
@@ -1,7 +1,7 @@
# usaco done <name>
[[ $# -ge 1 ]] || usage
name="$1"
[[ -d "$ROOT/solving/$name" ]] || { echo "Error: solving/$name not found" >&2; exit 1; }
dir="$(find_problem "$1")"; name="$(basename "$dir")"
[[ "$dir" == "$ROOT/solving/$name" ]] || { echo "Error: $name is not in solving/ (it's in ${dir#$ROOT/})" >&2; exit 1; }
[[ -d "$ROOT/solved/$name" ]] && { echo "Error: solved/$name already exists" >&2; exit 1; }
mv "$ROOT/solving/$name" "$ROOT/solved/$name"
echo "Moved to solved/$name 🎉"
+3 -3
View File
@@ -8,7 +8,7 @@ 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"
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"
# REPL-only words (used for completion and for 'help')
@@ -144,7 +144,7 @@ while :; do
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 ;;
tpl|template|rm|delete) c=reset ;; cp) c=copy ;; sy) c=sync ;;
esac
if [[ " $NO_PROBLEM " == *" $c "* ]]; then
@@ -155,7 +155,7 @@ while :; do
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
if [[ ${#args[@]} -ge 1 && "${args[0]}" != -* ]]; 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
+1 -2
View File
@@ -3,8 +3,7 @@ list_by_mtime() { # list subdirs of $1 oldest-first by last modification
[[ -d "$1" ]] || return 0
for d in "$1"/*/; do
[[ -d "$d" ]] || continue
# newest mtime of any file in the problem dir
printf '%s\t%s\n' "$(find "$d" -type f -printf '%T@\n' | sort -n | tail -1)" "$(basename "$d")"
printf '%s\t%s\n' "$(last_touched "${d%/}")" "$(basename "$d")"
done | sort -n | cut -f2 | sed 's/^/ /'
}
echo "== Solving =="
+8 -6
View File
@@ -1,14 +1,16 @@
# usaco reset/rm/delete <name>
# usaco reset/rm/delete/template/tpl <name> [-y]
[[ $# -ge 1 ]] || usage
dir="$(find_problem "$1")"
name="$(basename "$dir")"
io="$(detect_io "$dir" || true)"
io="${io:-$name}"
echo "This resets ${dir#$ROOT/} to a fresh template:"
echo " main.cpp -> re-created from template (old one saved as main.cpp.bak)"
echo " removes compiled binary and $io.out (tests/ and $io.in are kept)"
read -r -p "Reset '$name'? [y/N] " ans
[[ "$ans" == y || "$ans" == Y ]] || { echo "Aborted."; exit 1; }
if [[ "${2:-}" != -y ]]; then
echo "This resets ${dir#$ROOT/} to a fresh template:"
echo " main.cpp -> re-created from template (old one saved as main.cpp.bak)"
echo " removes compiled binary and $io.out (tests/ and $io.in are kept)"
read -r -p "Reset '$name'? [y/N] " ans
[[ "$ans" == y || "$ans" == Y ]] || { echo "Aborted."; exit 1; }
fi
[[ -f "$dir/main.cpp" ]] && cp "$dir/main.cpp" "$dir/main.cpp.bak"
fresh_main "$dir" "$io"
rm -f "$dir/main" "$dir/a.out" "$dir/$io.out"
+4 -4
View File
@@ -5,8 +5,8 @@ build_problem "$dir"
# run from inside the dir so freopen finds the .in file
(cd "$dir" && ./main)
# show the .out file if the program wrote one
out="$(ls "$dir"/*.out 2>/dev/null | grep -v '/a\.out$' | head -1 || true)"
if [[ -n "$out" ]]; then
echo "── $(basename "$out") ──"
cat "$out"
io="$(detect_io "$dir" || true)"
if [[ -n "$io" && -f "$dir/$io.out" ]]; then
echo "── $io.out ──"
cat "$dir/$io.out"
fi
+2 -2
View File
@@ -1,7 +1,7 @@
# usaco skip <name>
[[ $# -ge 1 ]] || usage
name="$1"
[[ -d "$ROOT/solving/$name" ]] || { echo "Error: solving/$name not found" >&2; exit 1; }
dir="$(find_problem "$1")"; name="$(basename "$dir")"
[[ "$dir" == "$ROOT/solving/$name" ]] || { echo "Error: $name is not in solving/ (it's in ${dir#$ROOT/})" >&2; exit 1; }
[[ -d "$ROOT/skipped/$name" ]] && { echo "Error: skipped/$name already exists" >&2; exit 1; }
mkdir -p "$ROOT/skipped"
mv "$ROOT/solving/$name" "$ROOT/skipped/$name"
+1 -1
View File
@@ -5,7 +5,7 @@ now=$(date +%s); week=0; month=0
if [[ -d "$ROOT/solved" ]]; then
for d in "$ROOT/solved"/*/; do
[[ -d "$d" ]] || continue
t="$(find "$d" -type f -printf '%T@\n' | sort -n | tail -1 | cut -d. -f1)"
t="$(solved_at "${d%/}")"
[[ -z "$t" ]] && continue
age=$(( now - t ))
(( age <= 7*86400 )) && week=$((week+1))
-6
View File
@@ -1,6 +0,0 @@
# usaco template/tpl <name>
[[ $# -ge 1 ]] || usage
dir="$(find_problem "$1")"
[[ -f "$dir/main.cpp" ]] && cp "$dir/main.cpp" "$dir/main.cpp.bak" && echo "Backed up old main.cpp -> main.cpp.bak"
cp "$ROOT/template.cpp" "$dir/main.cpp"
echo "Refreshed $dir/main.cpp from template"
+3 -5
View File
@@ -1,10 +1,8 @@
# usaco undo <name>
[[ $# -ge 1 ]] || usage
name="$1"; from=""
for s in solved skipped; do
[[ -d "$ROOT/$s/$name" ]] && from="$s" && break
done
[[ -n "$from" ]] || { echo "Error: '$name' not found in solved/ or skipped/" >&2; exit 1; }
dir="$(find_problem "$1")"; name="$(basename "$dir")"
from="$(basename "$(dirname "$dir")")"
[[ "$from" != solving ]] || { echo "Error: $name is already in solving/" >&2; exit 1; }
[[ -d "$ROOT/solving/$name" ]] && { echo "Error: solving/$name already exists" >&2; exit 1; }
mv "$ROOT/$from/$name" "$ROOT/solving/$name"
echo "Moved back to solving/$name (was $from)"
+29 -2
View File
@@ -1,7 +1,10 @@
# common.sh - shared helpers for usaco commands (sourced, not executed)
# Expects ROOT to be set by the dispatcher.
CXXFLAGS=(-O2 -std=c++17 -Wall -Wextra)
# -DLOCAL turns on the template's debugging.h include and DBG macros (judges don't define it)
CXXFLAGS=(-O2 -std=c++17 -Wall -Wextra -DLOCAL -I"$ROOT")
# --debug (or USACO_DEBUG=1): catch out-of-bounds, overflow and UB at runtime
DEBUG_CXXFLAGS=(-g -O1 -fsanitize=address,undefined -fno-omit-frame-pointer -D_GLIBCXX_DEBUG)
find_problem() { # echoes dir of problem in solving/, solved/, or skipped/; unique prefix ok
local name="$1" s d matches=()
@@ -24,7 +27,12 @@ find_problem() { # echoes dir of problem in solving/, solved/, or skipped/; uni
}
build_problem() { # $1 = problem dir
g++ "${CXXFLAGS[@]}" "$1/main.cpp" -o "$1/main"
if [[ -n "${USACO_DEBUG:-}" ]]; then
echo "${DIM}(debug build: sanitizers on)${RST}" >&2
g++ "${CXXFLAGS[@]}" "${DEBUG_CXXFLAGS[@]}" "$1/main.cpp" -o "$1/main"
else
g++ "${CXXFLAGS[@]}" "$1/main.cpp" -o "$1/main"
fi
}
detect_io() { # echoes the io basename (first *.in in $1), or nothing
@@ -33,6 +41,25 @@ detect_io() { # echoes the io basename (first *.in in $1), or nothing
return 1
}
last_touched() { # $1 = problem dir: epoch of its last change
# uncommitted work counts as "now"-ish (newest mtime); otherwise the last commit.
# File mtimes alone are useless here: every checkout/pull resets them.
local t
if [[ -n "$(git -C "$ROOT" status --porcelain -- "$1" 2>/dev/null)" ]]; then
find "$1" -type f -printf '%T@\n' | sort -n | tail -1 | cut -d. -f1
elif t="$(git -C "$ROOT" log -1 --format=%ct -- "$1" 2>/dev/null)" && [[ -n "$t" ]]; then
echo "$t"
else
find "$1" -type f -printf '%T@\n' | sort -n | tail -1 | cut -d. -f1
fi
}
solved_at() { # $1 = solved/<name> dir: epoch it (last) entered solved/
local t
t="$(git -C "$ROOT" log -1 --diff-filter=A --format=%ct -- "$1" 2>/dev/null)"
[[ -n "$t" ]] && echo "$t" || last_touched "$1"
}
fresh_main() { # $1 = problem dir, $2 = io name: main.cpp from template with STDIO wired up
sed "s/^int main() {$/int main() {\n STDIO(\"$2\");/" "$ROOT/template.cpp" > "$1/main.cpp"
}