Skip to content

Instantly share code, notes, and snippets.

@karlkfi
Last active February 2, 2021 07:36
Show Gist options
  • Save karlkfi/3e286f507c2d0ae7f8a1994e4e137a7e to your computer and use it in GitHub Desktop.
Save karlkfi/3e286f507c2d0ae7f8a1994e4e137a7e to your computer and use it in GitHub Desktop.
bash2asciicast.sh - Convert a bash script into an asciicast recording
#!/usr/bin/env bash
# Convert a bash script into an asciicast recording
set -o errexit -o nounset -o pipefail
USER=karl
WORKDIR="~/workspace"
PROMPT="\u001b]0;Cloud Shell\u0007${USER}@cloudshell:\u001b[1;34m${WORKDIR}\u001b[00m$ \u001bkcloudshell\u001b\\\\"
INPUT_FILE=${1:-}
if [[ -z "${INPUT_FILE}" ]]; then
echo "Syntax: $0 <path/to/bash-scipt.sh>"
exit 1
fi
echo '{"version": 2, "width": 233, "height": 69, "timestamp": 1612228753, "env": {"SHELL": "/bin/bash", "TERM": "screen"}}'
TS=0.0
CHAR_STEP=0.05
LINE_STEP=0.5
# Increment TS by the value of the first argument
# Could be done by bc more easily, but Cloud Shell doesn't have bc.
# So use awk instead...
function step() {
TS=$(echo - | awk "{printf \"%1.1f\", ${TS} + ${1}}")
}
while IFS="" read -r line; do
echo "[${TS}, \"o\", \"\r\n\"]"
echo "[${TS}, \"o\", \"${PROMPT}\"]"; step ${LINE_STEP}
while IFS="" read -r char; do
echo "[${TS}, \"o\", \"${char//\"/\\\"}\"]"; step ${CHAR_STEP}
done <<< "$(echo "${line}" | sed -e 's/\(.\)/\1\n/g')"
done < "${INPUT_FILE}"
echo "[${TS}, \"o\", \"\r\n\"]"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment