moved helpers separately
This commit is contained in:
+6
-77
@@ -36,73 +36,9 @@
|
||||
|
||||
load_conf "fun"
|
||||
|
||||
# ------------------------------------------------------------------------------
|
||||
# Shared helpers of the demo functions below (busy, fake_compile, hack).
|
||||
# They are exported because the public functions are.
|
||||
|
||||
# True when $1 is a non-negative integer. Every value that ends up in an
|
||||
# arithmetic context must pass this first (this includes values coming from the
|
||||
# configuration file): bash evaluates non-numeric strings inside [[ -gt ]] or
|
||||
# $(( )) as expressions, which can run arbitrary commands.
|
||||
# Usage: _fun_is_uint <value>
|
||||
_fun_is_uint()
|
||||
{
|
||||
[[ "$1" =~ ^[0-9]+$ ]]
|
||||
}
|
||||
export -f _fun_is_uint
|
||||
|
||||
# Convert a number of milliseconds into a seconds string usable by sleep
|
||||
# (1500 -> 1.500), in pure bash (no awk fork per call).
|
||||
# Usage: _fun_ms_to_s <ms>
|
||||
_fun_ms_to_s()
|
||||
{
|
||||
printf '%d.%03d' $(( $1 / 1000 )) $(( $1 % 1000 ))
|
||||
}
|
||||
export -f _fun_ms_to_s
|
||||
|
||||
# Fail with a message unless a terminal is available: the demos wait for a key
|
||||
# read from /dev/tty to stop.
|
||||
# Usage: _fun_need_tty <command-name>
|
||||
_fun_need_tty()
|
||||
{
|
||||
if ! { : </dev/tty; } 2>/dev/null; then
|
||||
disp E "$1: a terminal is required (it runs until a key is pressed)."
|
||||
return 1
|
||||
fi
|
||||
}
|
||||
export -f _fun_need_tty
|
||||
|
||||
# Wait for a key press, then stop the background job $1 and its children.
|
||||
# The job runs as "( ... ) &"; with job control (interactive shell) it is the
|
||||
# leader of its own process group, which is signalled as a whole. Without job
|
||||
# control that group does not exist, so its direct children (the pipeline) are
|
||||
# killed first, then the subshell. SIGINT is trapped meanwhile so that Ctrl-C
|
||||
# stops the job like a key press instead of aborting the caller and leaving it
|
||||
# running. The wait is a polling loop of short timed reads: a blocking read is
|
||||
# restarted by bash after a trapped signal and would never notice the Ctrl-C.
|
||||
# Usage: _fun_stop_on_key <pid>
|
||||
_fun_stop_on_key()
|
||||
{
|
||||
local pid=$1 old_int interrupted=0
|
||||
old_int=$(trap -p INT)
|
||||
trap 'interrupted=1' INT
|
||||
while (( ! interrupted )); do
|
||||
IFS= read -r -n 1 -s -t 0.3 _ </dev/tty && break
|
||||
# Status above 128 is just the timeout; anything else (such as end of
|
||||
# input on the terminal) ends the wait.
|
||||
(( $? > 128 )) || break
|
||||
done
|
||||
kill -- -"$pid" 2>/dev/null || {
|
||||
pkill -P "$pid" 2>/dev/null
|
||||
kill "$pid" 2>/dev/null
|
||||
}
|
||||
wait "$pid" 2>/dev/null
|
||||
trap - INT
|
||||
_trap_restore "$old_int"
|
||||
return 0
|
||||
}
|
||||
export -f _fun_stop_on_key
|
||||
# ------------------------------------------------------------------------------
|
||||
# Common helpers, then the ones private to this module (helpers/fun.sh)
|
||||
declare -F _profile_load_helpers >/dev/null || . "$(dirname -- "${BASH_SOURCE[0]}")/../helpers/common.sh"
|
||||
_profile_load_helpers "${BASH_SOURCE[0]}"
|
||||
|
||||
# ------------------------------------------------------------------------------
|
||||
# Make non-IT peoples think you're busy doing something hard: print the lines of
|
||||
@@ -165,7 +101,7 @@ busy()
|
||||
[[ $# -gt 0 ]] && pattern="$*"
|
||||
|
||||
# Checked here so that the configured default is validated as well as -d
|
||||
if ! _fun_is_uint "$delay_ms"; then
|
||||
if ! _is_uint "$delay_ms"; then
|
||||
disp E "Invalid delay '${delay_ms}': must be an integer (milliseconds)."
|
||||
return 1
|
||||
fi
|
||||
@@ -191,9 +127,6 @@ busy()
|
||||
_fun_stop_on_key "$sub_pid"
|
||||
}
|
||||
export -f busy
|
||||
# ------------------------------------------------------------------------------
|
||||
|
||||
|
||||
# ------------------------------------------------------------------------------
|
||||
# Simulate a long and complex compilation process
|
||||
# Usage: fake_compile [options]
|
||||
@@ -280,7 +213,7 @@ fake_compile()
|
||||
|
||||
# Validated here (not only in the options) so that the values coming from
|
||||
# the configuration file are checked too.
|
||||
if ! _fun_is_uint "$min_ms" || ! _fun_is_uint "$max_ms"; then
|
||||
if ! _is_uint "$min_ms" || ! _is_uint "$max_ms"; then
|
||||
disp E "min-delay and max-delay must be integers (milliseconds)."
|
||||
return 1
|
||||
fi
|
||||
@@ -396,9 +329,6 @@ fake_compile()
|
||||
_fun_stop_on_key "$sub_pid"
|
||||
}
|
||||
export -f fake_compile
|
||||
# ------------------------------------------------------------------------------
|
||||
|
||||
|
||||
# ------------------------------------------------------------------------------
|
||||
# Simulate a dramatic hacking sequence
|
||||
# Usage: hack [options]
|
||||
@@ -470,7 +400,7 @@ hack()
|
||||
esac
|
||||
done
|
||||
|
||||
if ! _fun_is_uint "$min_ms" || ! _fun_is_uint "$max_ms"; then
|
||||
if ! _is_uint "$min_ms" || ! _is_uint "$max_ms"; then
|
||||
disp E "min-delay and max-delay must be integers (milliseconds)."
|
||||
return 1
|
||||
fi
|
||||
@@ -598,5 +528,4 @@ hack()
|
||||
export -f hack
|
||||
# ------------------------------------------------------------------------------
|
||||
|
||||
|
||||
# EOF
|
||||
|
||||
Reference in New Issue
Block a user