Skip to content

Commit b19958b

Browse files
committed
Keep the end of long output, and pass the command line to _helpless
The typescript was capped with head -n 1024, keeping the start of the output. Errors are usually at the end (tracebacks, make: *** Error, segfaults), so a program that printed a lot and then failed lost its error before any helper or _helpless saw it. Now keep the first 64 lines (where the command line is echoed and found) plus the last 1024, with a marker for what was omitted. _helpless now also receives the command line as a second argument, so whatever explains the output (in cs50/codespace, the CS50 Duck via cs50.ai) can see what was run. The default _helpless ignores it; output stays the first argument, so the codespace's empty-output check is unaffected.
1 parent 400e6c1 commit b19958b

1 file changed

Lines changed: 18 additions & 5 deletions

File tree

‎etc/profile.d/help50.sh‎

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,15 @@ function _help50() {
5353
# Remove script's own output (if this is user's first command)
5454
typescript=$(echo "$typescript" | sed '1{/^Script started on .*/d}')
5555

56-
# Cap typescript at MIN(1K lines, 1M bytes), else `read` is slow
57-
typescript=$(echo "$typescript" | head -n 1024 | cut -b 1-1048576)
56+
# Cap typescript, else `read` below is slow. Keep the first few lines, where the
57+
# command line itself is echoed (found below), plus the last 1K lines, where
58+
# errors tend to be (tracebacks, `make: *** Error`, segfaults); a long-running
59+
# program that prints a lot and then crashes would otherwise lose its error.
60+
typescript=$(echo "$typescript" | cut -b 1-1048576)
61+
local total=$(echo "$typescript" | wc -l)
62+
if [[ $total -gt 1088 ]]; then
63+
typescript=$(echo "$typescript" | head -n 64; echo "[... $((total - 1088)) lines omitted ...]"; echo "$typescript" | tail -n 1024)
64+
fi
5865

5966
# Remove ANSI characters
6067
typescript=$(echo "$typescript" | ansi2txt)
@@ -108,8 +115,10 @@ function _help50() {
108115
_helpful "$help"
109116
elif [[ $status -ne 0 ]]; then # If helpless
110117

111-
# Cap what's relayed downstream (e.g., ddb50 rejects > 10,000 characters), keeping the end, where errors tend to be
112-
_helpless "$(echo "$typescript" | tail -c 8192)"
118+
# Pass the output (capped, e.g., since ddb50 rejects > 10,000 characters, keeping the end,
119+
# where errors tend to be) and the command line itself, so that whatever explains the
120+
# output can see what was run
121+
_helpless "$(echo "$typescript" | tail -c 8192)" "$argv"
113122
fi
114123
else
115124
_helped
@@ -123,7 +132,11 @@ function _rhetorical() {
123132
_alert "That was a rhetorical question. <3"
124133
}
125134

126-
# Default helpers
135+
# Default helpers, overridable (e.g., by cs50/codespace) by defining them before this file is sourced:
136+
# _helped last command succeeded
137+
# _helpful ADVICE a helper had advice for the failed command
138+
# _helpless OUTPUT CMD no helper had advice; OUTPUT is the failed command's (cleaned, capped)
139+
# output, possibly empty, and CMD its command line
127140
if ! type _helped >/dev/null 2>&1; then
128141
function _helped() { :; } # Silent
129142
fi

0 commit comments

Comments
 (0)