Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100755 167 lines (144 sloc) 3.463 kb
4155609 @nrr Make the spark script work on systems like NetBSD where bash lives somep...
nrr authored
1 #!/usr/bin/env bash
8b17457 @holman ▁▂▃▅▂▇
holman authored
2 #
3 # spark
4 # https://github.com/holman/spark
5 #
6 # Generates sparklines for a set of data.
7 #
8 # Here's a a good web-based sparkline generator that was a bit of inspiration
9 # for spark:
10 #
11 # https://datacollective.org/sparkblocks
12 #
13 # spark takes a comma-separated list of data and then prints a sparkline out of
14 # it.
15 #
16 # Examples:
17 #
18 # spark 1,5,22,13,53
19 # # => ▁▁▃▂▇
20 #
21 # spark 0,30,55,80,33,150
22 # # => ▁▂▃▅▂▇
23 #
24 # spark -h
25 # # => Prints the spark help text.
26 set -e
ccd59de @mjdominus -d flag to enable debugging mode
authored
27 debug='false'
8b17457 @holman ▁▂▃▅▂▇
holman authored
28
29 # Prints the help text for spark.
30 #
31 # Returns nothing.
32 help()
33 {
4ecdbe2 @rentalcustard Fix usage text formatting
rentalcustard authored
34 cat <<-EOF
1602499 @holman reformat help
holman authored
35
4ecdbe2 @rentalcustard Fix usage text formatting
rentalcustard authored
36 USAGE:
37 spark [comma,separated,value,list]
1602499 @holman reformat help
holman authored
38
4ecdbe2 @rentalcustard Fix usage text formatting
rentalcustard authored
39 EXAMPLES:
40 spark 1,5,22,13,53
41 ▁▁▃▂▇
42 spark 0,30,55,80,33,150
43 ▁▂▃▅▂▇
44 EOF
8b17457 @holman ▁▂▃▅▂▇
holman authored
45 }
46
47 # The actual fun characters we are generating in the sparkline.
f07309f @holman THE MERGE BUTTON, ITS TOO POWERFUL
holman authored
48 ticks=(▁ ▂ ▃ ▄ ▅ ▆ ▇)
0cde7d0 @mjdominus Use rational arithmetic instead of bc
authored
49 number_of_tiers=$(( ${#ticks[@]} - 1 ))
8b17457 @holman ▁▂▃▅▂▇
holman authored
50 # The numbers the user gave us.
51 numbers=()
52
53 # The sorted array of the numbers.
54 sorted=()
55
56 # This sets up our secondary array so we can actually generate the correct
57 # tick.
58 #
59 # Returns nothing.
60 setup_array() {
61 # 3,6,2 => 2,3,6
62 sorted=$(echo $1 | tr ',' '\n' | sort -k1,1n | paste -s -d',' -)
63
64 # convert comma-separated string to array
65 IFS=,
66 sorted=($sorted)
0cde7d0 @mjdominus Use rational arithmetic instead of bc
authored
67
68 to_rational ${sorted[0]}
69 min_n=$n
70 min_d=$d
71
72 to_rational ${sorted[${#sorted[@]} - 1]}
73 max_n=$n
74 max_d=$d
75
76 range_n=$(( max_n * min_d - min_n * max_d ))
77 range_d=$(( max_d * min_d ))
78
8b17457 @holman ▁▂▃▅▂▇
holman authored
79 numbers=($1)
80 }
81
0cde7d0 @mjdominus Use rational arithmetic instead of bc
authored
82 # given an input number which might be a decimal, convert it to
83 # a rational number; set n and d to its numerator and
84 # denominator. For example, 3.3 becomes n=33 and d=10;
85 # 17 becomes n=17 and d=1.
86 to_rational() {
87 # Crapulent bash can't handle decimal numbers, so we will convert
88 # the input number to a rational
89 if [[ $1 =~ (.*)\.(.*) ]] ; then
90 i_part=${BASH_REMATCH[1]}
91 f_part=${BASH_REMATCH[2]}
92 n="$i_part$f_part";
93 d=$(( 10 ** ${#f_part} ))
94 else
95 n=$1
96 d=1
97 fi
8b17457 @holman ▁▂▃▅▂▇
holman authored
98 }
99
100 # Determines what tick we should print for this number and prints it.
101 #
102 # Returns nothing.
103 print_tick()
104 {
0cde7d0 @mjdominus Use rational arithmetic instead of bc
authored
105 # The following rigamarole calculates
106 # ($number - $min) / $tier_size
107 # using rational arithmetic. This is necessary because $((...)) only
108 # does integer calculations and because shelling out to bc or dc is
109 # slow and nonportable
110 to_rational $1
111 tick_index_d=$(( range_n * d * min_d ))
112 tick_index_n=$(( ( n * min_d - min_n * d ) * number_of_tiers * range_d ))
113
114 # round to nearest integer: first add 1/2
115 tick_index_an=$(( tick_index_n * 2 + tick_index_d ))
116 tick_index_ad=$(( tick_index_d * 2 ))
117
118 # divide and truncate
119 tick_index=$(( tick_index_an / tick_index_ad ))
120
121 if $debug; then
122 echo "$number $tick_index_n / $tick_index_d = $tick_index"
ccd59de @mjdominus -d flag to enable debugging mode
authored
123 else
124 echo -n ${ticks[$tick_index]};
125 fi
d5afa01 @mjdominus fix arithmetic errors
authored
126 }
8b17457 @holman ▁▂▃▅▂▇
holman authored
127
d5afa01 @mjdominus fix arithmetic errors
authored
128 # Iterate over all of our data and print them out.
8b17457 @holman ▁▂▃▅▂▇
holman authored
129 #
130 # Returns nothing.
131 print_ticks()
132 {
133 for number in ${numbers[@]}
134 do
a179f1f @peff print sparks incrementally instead of building string
peff authored
135 print_tick $number
8b17457 @holman ▁▂▃▅▂▇
holman authored
136 done
a179f1f @peff print sparks incrementally instead of building string
peff authored
137 echo
8b17457 @holman ▁▂▃▅▂▇
holman authored
138 }
139
ccd59de @mjdominus -d flag to enable debugging mode
authored
140 while getopts ":hd" option; do
8b17457 @holman ▁▂▃▅▂▇
holman authored
141 case "$option" in
142 h) help && exit ;;
143 # [?]) echo "$OPTARG";;
ccd59de @mjdominus -d flag to enable debugging mode
authored
144 d) debug='true'; fudge='0'; shift $((OPTIND-1)) ;;
8b17457 @holman ▁▂▃▅▂▇
holman authored
145 esac
146 done
147
148 # Accept input from $1 or from the pipeline.
7736c9e @patricklucas Change back to using 'test' for string equality
patricklucas authored
149 if test "$*" != ""
8b17457 @holman ▁▂▃▅▂▇
holman authored
150 then
b597103 @patricklucas Allow spaced input
patricklucas authored
151 data="$*"
8b17457 @holman ▁▂▃▅▂▇
holman authored
152 else
c4fde8a @holman don't hang on tty (closes #1)
holman authored
153 # check to see if stdin's a tty
154 if [ -t 0 ]; then
155 help
156 exit
157 fi
158
424194d @peff drop pointless loop
peff authored
159 read data
8b17457 @holman ▁▂▃▅▂▇
holman authored
160 fi
161
b597103 @patricklucas Allow spaced input
patricklucas authored
162 # Trim spaces to allow input like '1, 2, 3'
cf1269b @patricklucas Keep individual numbers separate
patricklucas authored
163 data=$(echo $data | tr -s ' ' ',')
b597103 @patricklucas Allow spaced input
patricklucas authored
164
8b17457 @holman ▁▂▃▅▂▇
holman authored
165 setup_array $data
166 print_ticks $data
Something went wrong with that request. Please try again.