Skip to content

Commit

Permalink
Add a pause after printing the command $PROMPT.
Browse files Browse the repository at this point in the history
  • Loading branch information
zechris committed Oct 17, 2021
1 parent 615844a commit 6bf3d8c
Show file tree
Hide file tree
Showing 6 changed files with 248 additions and 63 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# asciinema-rec_script changelog

## 0.9.1 (2021-10-18)

* Add a pause after printing the command prompt

## 0.9.0 (2021-10-17)

* Initial release
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ Place `asciinema-rec_script` somewhere in your $PATH.
* _(so eg. `--help` will show all the [asciinema rec [options]](https://github.com/asciinema/asciinema#rec-filename))_
* `SLEEP=0 ./screencasts/demo-bash_functions.asc`
* _(env vars can be passed into the script in the regular way)_
* _(to eg set `PROMPT="$ "`, `PROMPT_PAUSE=5`)_
* `source ./screencasts/demo-date_maths.asc`
* _Nb. It should also be possibe to source the `.asc` script in your $SHELL and run it as a regular bash script_
* _(Maintaining this compatability means that the `.asc` file won't require any special commands that a regular shell script wouldn't already have in it. Which hopefully results in regular shell scripts resulting in half-decent looking recordings.)_
Expand Down
10 changes: 8 additions & 2 deletions bin/asciinema-rec_script
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ build_augmented_script() { local script="$1"
}

execute() {
# A pause can be added between printing the shell prompt
# and simulating the script typing out a command
PROMPT_PAUSE=${PROMPT_PAUSE:-1}

case $1 in
sleep*)
# translate any sleep commands to our pretty 'countdown'
Expand All @@ -47,7 +51,8 @@ build_augmented_script() { local script="$1"
;;
source*)
# we'll use `source` to define multiline commands
printf "${PROMPT}%s\n" "$(eval "${1//source/$CAT}")"
printf "${PROMPT}"
printf "%s\n" "$(sleep $PROMPT_PAUSE; eval "${1//source/$CAT}")"
# shellcheck disable=SC1090
eval "$1"
;;
Expand All @@ -60,7 +65,8 @@ build_augmented_script() { local script="$1"
comment "$(eval echo "${1/: /}")"
;;
*)
printf "${PROMPT}%s\n" "$(echo "$1" | $CAT)"
printf "${PROMPT}"
printf "%s\n" "$(sleep $PROMPT_PAUSE; echo "$1" | $CAT)"
eval "$1"
;;
esac
Expand Down
43 changes: 16 additions & 27 deletions screencasts/demo-bash_functions.asc
Original file line number Diff line number Diff line change
@@ -1,41 +1,37 @@
#!/usr/bin/env asciinema-rec_script
: "\`${script:=$0}\`"
: "There'll be \`${SLEEP:-5}s\` countdowns where you can hit \`[space]\` to pause."
: "There'll be \`${SLEEP:-2}s\` countdowns where you can hit \`[space]\` to pause."

sleep ${SLEEP:-5}
# First we'll write a function `f1` that just prints out some variables
source "${script%.asc}/f1.1"
sleep ${SLEEP:-5}
# Note that vars can be accessed using `$a` or `${a}` syntax
echo "a='$a', b='${b}', c='$c'"
sleep ${SLEEP:-5}
sleep ${SLEEP:-2}
# And double quotes are used for interpolation of:
# * env vars (eg. `"${a}"` )
# * functions/commands (eg. `"$(date)"`)
sleep ${SLEEP:-5}
# So let's call our function
f1


sleep ${SLEEP:-5}
sleep ${SLEEP:-2}
# Nothing there, so we'll set a couple of env vars for it
# (And remember to *never* put spaces around the `=`)
a='a 0'
b='b 0'


sleep ${SLEEP:-5}
# Now we'll call it again
f1
sleep ${SLEEP:-5}
sleep ${SLEEP:-2}
# Next we'll change `f1` to accept "arguments"
# Unfortunately bash functions don't have named arguments,
# they just use positional parameters `$1`, `$2` etc
Expand All @@ -45,120 +41,113 @@ sleep ${SLEEP:-5}
source "${script%.asc}/f1.2"


sleep ${SLEEP:-5}
# Then call `f1` (checking on our env vars BEFORE & AFTER the call)
:;echo "BEFORE: a='$a', b='$b', c='$c'"
f1 'a 1' 'b 1' 'c 1'
sleep ${SLEEP:-2}
:;echo "AFTER : a='$a', b='$b', c='$c'"

sleep ${SLEEP:-5}
sleep ${SLEEP:-2}
# Hang on!
# 👎 Fail!
# That's no good, the `$a` & `$b` env vars we set previously have changed!
sleep ${SLEEP:-5}
# Let's try again.
# But first re-assign `$a` & `$b`...
a='a 0'
b='b 0'
unset c # and unset `$c`


sleep ${SLEEP:-5}
# Then fix our function by using `local` env vars...
source "${script%.asc}/f1.3"


sleep ${SLEEP:-5}
# Now we'll just check to make sure it worked...
:;echo "BEFORE: a='$a', b='$b', c='$c'"
f1 'a 1' 'b 1' 'c 1'
sleep ${SLEEP:-2}
:;echo "AFTER : a='$a', b='$b', c='$c'"


sleep ${SLEEP:-5}
# 🏆 Success!
# No global env vars were harmed in the making or calling of this function.


sleep ${SLEEP:-5}
sleep ${SLEEP:-2}
# Now, give the "arguments" default values using `${name:-default}` syntax
source "${script%.asc}/f1.4"

f1 'a 1'


sleep ${SLEEP:-5}
# Now, what about defaulting to the values of env vars outside the function?
# ie. with this syntax: `a=${1:-$a}`

# And how bout falling back to our original default values?
# ie. with this syntax: `a=${1:-${a:-a 2}}`
sleep ${SLEEP:-2}
source "${script%.asc}/f1.5"

f1 'a 1'


sleep ${SLEEP:-5}
sleep ${SLEEP:-2}
# Now, let's get even funkier with `$c` and get its default from a function.
# And just cause we can, let's call *it's* function `c`
c() { date; }
sleep ${SLEEP:-5}
sleep ${SLEEP:-2}
# ie. which just prints the date
c
sleep ${SLEEP:-5}
# So now change `c=...` in `f1` to make use of our `$(c)` function...
source "${script%.asc}/f1.6"
f1 'a 1'


sleep ${SLEEP:-5}
# Now let's mix things up a bit and call `f1` in a different way.
# We can call it in context of new values for `$b` & `$c`,
# by placing them in front of the call to `f1`.
# 🤔 ... but won't that change the global values for `$b` & `$c`?

# Let's check:
sleep ${SLEEP:-5}
sleep ${SLEEP:-2}
:;echo "BEFORE: a='$a', b='$b', c='$c'"
c='c 3' b='b 3' f1 'a 1'
sleep ${SLEEP:-2}
:;echo "AFTER : a='$a', b='$b', c='$c'"


sleep ${SLEEP:-5}
sleep ${SLEEP:-2}
# 🏆 Success!
# Once again...
# No global env vars were harmed in the making or calling of this function.


sleep ${SLEEP:-5}
sleep ${SLEEP:-2}
# Hmm... now here's one more trick, but IMO it's more trouble than it's worth.
# This trick involves passing in var assignments as "arguments" to the
# function, for `local` to evaluate.
# *Then* we can default back to original values with a *second* `local`.
sleep ${SLEEP:-5}
sleep ${SLEEP:-2}
# That magic function (which we'll call `f2` for short) looks like this:
source "${script%.asc}/f2"


# And it can be called like this...
sleep ${SLEEP:-5}
f2 a='a 4' b='b 4'


# Or this...
sleep ${SLEEP:-5}
a='a 4' f2 b='b 4'


# Or this...
sleep ${SLEEP:-5}
a='a 4' f2 c='c 4' b='b 4'
Loading

0 comments on commit 6bf3d8c

Please sign in to comment.