From 210f7f4c83de860c3554302ec10ae16c4a018d1c Mon Sep 17 00:00:00 2001 From: Salt Date: Mon, 31 Dec 2018 14:05:15 -0600 Subject: [PATCH] cowsayeternal: Add multithreading What am I doing with my life --- cowsayeternal | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/cowsayeternal b/cowsayeternal index f942c9c..e1d06bf 100755 --- a/cowsayeternal +++ b/cowsayeternal @@ -4,21 +4,27 @@ printf "Writing cows to temporary file \"%s\"\\n" \ "$cowsaidfile" cowsay "E T E R N A L" > "$cowsaidfile" trap_exit() { + # kill "$cowthreadpid" > /dev/null 2>&1 + wait "$cowthreadpid" printf "\\n" cat "$cowsaidfile" rm "$cowsaidfile" } trap trap_exit EXIT -iterations=1 +# Cow writing thread +( while true; do # Shut up shellcheck; that's the whole point. cowsay does a full read # before it writes back to the file anyway. # shellcheck disable=2094 cowsay -n < "$cowsaidfile" 1<> "$cowsaidfile" - iterations=$(( iterations + 1 )) +done +) & +cowthreadpid=$! +while true; do # This line will only ever grow larger, so we don't need to clear it # before overwriting it. - printf "\\r%s iterations, %s bytes of cowsay; hit Ctrl+C to dump them" \ - "$iterations" \ + printf "\\r%s bytes of cowsay; hit Ctrl+C to dump them" \ "$(stat --printf="%s" "$cowsaidfile")" + sleep 0.02 done