Skip to content

Commit

Permalink
record terminal traffic and use as contest
Browse files Browse the repository at this point in the history
  • Loading branch information
simonmysun committed Jul 27, 2024
1 parent e38cc42 commit 279f152
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 0 deletions.
19 changes: 19 additions & 0 deletions ell.sh
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,19 @@ logging_debug "Starting ${0}";
load_config;
parse_arguments "${@}";

if [[ "x${ELL_RECORD}" == "xtrue" && -z $ELL_TMP_SHELL_LOG ]]; then
export ELL_TMP_SHELL_LOG=$(mktemp);
export ELL_RECORD=true;
logging_info "Session being recorded to ${ELL_TMP_SHELL_LOG}";
script -q -f -c "bash -i" ${ELL_TMP_SHELL_LOG};
logging_debug "Removing ${ELL_TMP_SHELL_LOG}";
rm -f ${ELL_TMP_SHELL_LOG};
unset ELL_TMP_SHELL_LOG;
unset ELL_RECORD;
logging_info "Record mode exited";
exit 0;
fi

if [[ ! -f "${ELL_TEMPLATE_PATH}${ELL_TEMPLATE}.json" ]]; then
logging_fatal "Template not found: ${ELL_TEMPLATE_PATH}${ELL_TEMPLATE}.json";
exit 1;
Expand All @@ -42,6 +55,12 @@ if [[ ! -z "${ELL_INPUT_FILE}" ]]; then
fi
fi

if [[ -z "${ELL_TMP_SHELL_LOG}" ]]; then
logging_debug "ELL_TMP_SHELL_LOG not set";
else
logging_debug "Loading shell log from ${ELL_TMP_SHELL_LOG}";
SHELL_CONTEXT=$(tail -c 3000 ${ELL_TMP_SHELL_LOG} | ${BASE_DIR}/helpers/render_to_text.perl | sed -e 's/\\/\\\\/g' -e 's/"/\\"/g'| awk '{printf "%s\\n", $0}');
fi

PAYLOAD=$(eval "cat <<EOF
$(<${ELL_TEMPLATE_PATH}${ELL_TEMPLATE}.json)
Expand Down
15 changes: 15 additions & 0 deletions helpers/render_to_text.perl
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#!/usr/bin/perl

# https://unix.stackexchange.com/questions/14684/removing-control-chars-including-console-codes-colours-from-script-output licensed under CC BY-SA 4.0
# GNU sed and POSIX bash does not support PCRE features like look-ahead. Therefore, adding new a dependency is not avoidable.

while (<>) {
s/ \e[ #%()*+\-.\/]. |
\r | # Remove extra carriage returns also
(?:\e\[|\x9b) [ -?]* [@-~] | # CSI ... Cmd
(?:\e\]|\x9d) .*? (?:\e\\|[\a\x9c]) | # OSC ... (ST|BEL)
(?:\e[P^_]|[\x90\x9e\x9f]) .*? (?:\e\\|\x9c) | # (DCS|PM|APC) ... ST
\e.|[\x80-\x9f] //xg;
1 while s/[^\b][\b]//g;
print;
}

0 comments on commit 279f152

Please sign in to comment.