| 1 | #!/bin/sh |
| 2 | # sw.sh |
| 3 | # A command-line Stopwatch |
| 4 | |
| 5 | # Author: Pádraig Brady |
| 6 | # http://www.pixelbeat.org/scripts/sw |
| 7 | # (Minor reformatting by ABS Guide author.) |
| 8 | # Used in ABS Guide with script author's permission. |
| 9 | # Notes: |
| 10 | # This script starts a few processes per lap, in addition to |
| 11 | # the shell loop processing, so the assumption is made that |
| 12 | # this takes an insignificant amount of time compared to |
| 13 | # the response time of humans (~.1s) (or the keyboard |
| 14 | # interrupt rate (~.05s)). |
| 15 | # '?' for splits must be entered twice if characters |
| 16 | # (erroneously) entered before it (on the same line). |
| 17 | # '?' since not generating a signal may be slightly delayed |
| 18 | # on heavily loaded systems. |
| 19 | # Lap timings on ubuntu may be slightly delayed due to: |
| 20 | # https://bugs.launchpad.net/bugs/62511 |
| 21 | # Changes: |
| 22 | # V1.0, 23 Aug 2005, Initial release |
| 23 | # V1.1, 26 Jul 2007, Allow both splits and laps from single invocation. |
| 24 | # Only start timer after a key is pressed. |
| 25 | # Indicate lap number |
| 26 | # Cache programs at startup so there is less error |
| 27 | # due to startup delays. |
| 28 | # V1.2, 01 Aug 2007, Work around `date` commands that don't have |
| 29 | # nanoseconds. |
| 30 | # Use stty to change interrupt keys to space for |
| 31 | # laps etc. |
| 32 | # Ignore other input as it causes problems. |
| 33 | # V1.3, 01 Aug 2007, Testing release. |
| 34 | # V1.4, 02 Aug 2007, Various tweaks to get working under ubuntu |
| 35 | # and Mac OS X. |
| 36 | # V1.5, 27 Jun 2008, set LANG=C as got vague bug report about it. |
| 37 | |
| 38 | export LANG=C |
| 39 | |
| 40 | ulimit -c 0 # No coredumps from SIGQUIT. |
| 41 | trap '' TSTP # Ignore Ctrl-Z just in case. |
| 42 | save_tty=`stty -g` && trap "stty $save_tty" EXIT # Restore tty on exit. |
| 43 | stty quit ' ' # Space for laps rather than Ctrl-\. |
| 44 | stty eof '?' # ? for splits rather than Ctrl-D. |
| 45 | stty -echo # Don't echo input. |
| 46 | |
| 47 | cache_progs() { |
| 48 | stty > /dev/null |
| 49 | date > /dev/null |
| 50 | grep . < /dev/null |
| 51 | (echo "import time" | python) 2> /dev/null |
| 52 | bc < /dev/null |
| 53 | sed '' < /dev/null |
| 54 | printf '1' > /dev/null |
| 55 | /usr/bin/time false 2> /dev/null |
| 56 | cat < /dev/null |
| 57 | } |
| 58 | cache_progs # To minimise startup delay. |
| 59 | |
| 60 | date +%s.%N | grep -qF 'N' && use_python=1 # If `date` lacks nanoseconds. |
| 61 | now() { |
| 62 | if [ "$use_python" ]; then |
| 63 | echo "import time; print time.time()" 2>/dev/null | python |
| 64 | else |
| 65 | printf "%.2f" `date +%s.%N` |
| 66 | fi |
| 67 | } |
| 68 | |
| 69 | fmt_seconds() { |
| 70 | seconds=$1 |
| 71 | mins=`echo $seconds/60 | bc` |
| 72 | if [ "$mins" != "0" ]; then |
| 73 | seconds=`echo "$seconds - ($mins*60)" | bc` |
| 74 | echo "$mins:$seconds" |
| 75 | else |
| 76 | echo "$seconds" |
| 77 | fi |
| 78 | } |
| 79 | |
| 80 | total() { |
| 81 | end=`now` |
| 82 | total=`echo "$end - $start" | bc` |
| 83 | fmt_seconds $total |
| 84 | } |
| 85 | |
| 86 | stop() { |
| 87 | [ "$lapped" ] && lap "$laptime" "display" |
| 88 | total |
| 89 | exit |
| 90 | } |
| 91 | |
| 92 | lap() { |
| 93 | laptime=`echo "$1" | sed -n 's/.*real[^0-9.]*\(.*\)/\1/p'` |
| 94 | [ ! "$laptime" -o "$laptime" = "0.00" ] && return |
| 95 | # Signals too frequent. |
| 96 | laptotal=`echo $laptime+0$laptotal | bc` |
| 97 | if [ "$2" = "display" ]; then |
| 98 | lapcount=`echo 0$lapcount+1 | bc` |
| 99 | laptime=`fmt_seconds $laptotal` |
| 100 | echo $laptime "($lapcount)" |
| 101 | lapped="true" |
| 102 | laptotal="0" |
| 103 | fi |
| 104 | } |
| 105 | |
| 106 | echo -n "Space for lap | ? for split | Ctrl-C to stop | Space to start...">&2 |
| 107 | |
| 108 | while true; do |
| 109 | trap true INT QUIT # Set signal handlers. |
| 110 | laptime=`/usr/bin/time -p 2>&1 cat >/dev/null` |
| 111 | ret=$? |
| 112 | trap '' INT QUIT # Ignore signals within this script. |
| 113 | if [ $ret -eq 1 -o $ret -eq 2 -o $ret -eq 130 ]; then # SIGINT = stop |
| 114 | [ ! "$start" ] && { echo >&2; exit; } |
| 115 | stop |
| 116 | elif [ $ret -eq 3 -o $ret -eq 131 ]; then # SIGQUIT = lap |
| 117 | if [ ! "$start" ]; then |
| 118 | start=`now` || exit 1 |
| 119 | echo >&2 |
| 120 | continue |
| 121 | fi |
| 122 | lap "$laptime" "display" |
| 123 | else # eof = split |
| 124 | [ ! "$start" ] && continue |
| 125 | total |
| 126 | lap "$laptime" # Update laptotal. |
| 127 | fi |
| 128 | done |
| 129 | |
| 130 | exit $? |