Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100755 153 lines (133 sloc) 2.825 kB
8b17457 @holman ▁▂▃▅▂▇
holman authored
1 #!/bin/sh
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
27
28 # Prints the help text for spark.
29 #
30 # Returns nothing.
31 help()
32 {
33 echo "spark\n"
34 echo "USAGE:"
35 echo " spark [comma,separated,value,list]\n"
36 echo "EXAMPLES:"
37 echo " spark 1,5,22,13,53"
38 echo " ▁▁▃▂▇"
39 echo " spark 0,30,55,80,33,150"
40 echo " ▁▂▃▅▂▇"
41 }
42
43 # The actual fun characters we are generating in the sparkline.
44 ticks=(▁ ▂ ▃ ▅ ▆ ▇)
45
46 # The numbers the user gave us.
47 numbers=()
48
49 # The sorted array of the numbers.
50 sorted=()
51
52 # This sets up our secondary array so we can actually generate the correct
53 # tick.
54 #
55 # Returns nothing.
56 setup_array() {
57 # 3,6,2 => 2,3,6
58 sorted=$(echo $1 | tr ',' '\n' | sort -k1,1n | paste -s -d',' -)
59
60 # convert comma-separated string to array
61 IFS=,
62 sorted=($sorted)
63 numbers=($1)
64 }
65
66 # The maximum value of the sorted array. In other words, the last value.
67 sort_max()
68 {
69 last=${#sorted[@]}
70 echo ${sorted[$last - 1]}
71 }
72
73 # The minimum value of the sorted array. In other words, the first value.
74 sort_min()
75 {
76 echo ${sorted[0]}
77 }
78
79 # Find the distance between tiers so we know which tick to assign a character.
80 tier()
81 {
82 number_of_ticks=${#ticks[@]}
83 distance=$(echo "$(sort_max) / $number_of_ticks" | bc)
84 echo $distance
85 }
86
87 # Determines what tick we should print for this number and prints it.
88 #
89 # Returns nothing.
90 print_tick()
91 {
92 tier=$(tier)
93
94 for (( i = 0 ; i < ${#numbers[@]} ; i++ ))
95 do
96 tick=${ticks[$i]}
97 number=$1
98 less_than=$(echo "$i * $tier + sort_min + $tier" | bc)
99 greater_than=$(echo "($i - 1) * $tier + sort_min + $tier" | bc)
100 result=$(echo "$number <= $less_than && $number >= $greater_than" | bc)
101
102 if [ $result -eq 1 ]
103 then
104 echo "$tick"
105 return
106 fi
107 done
108
109 last=${#ticks[@]}
110 echo ${ticks[$last-1]}
111 }
112
113 # Iterate over all of our ticks and print them out.
114 #
115 # Returns nothing.
116 print_ticks()
117 {
118 for number in ${numbers[@]}
119 do
120 echo $"$(print_tick $number)\c"
121 done
122 echo ""
123 }
124
125 while getopts ":h" option; do
126 case "$option" in
127 h) help && exit ;;
128 # [?]) echo "$OPTARG";;
129 esac
130 done
131
132 # Accept input from $1 or from the pipeline.
133 if test "$1" != ""
134 then
135 data="$1"
136 else
137 data=''
c4fde8a @holman don't hang on tty (closes #1)
holman authored
138
139 # check to see if stdin's a tty
140 if [ -t 0 ]; then
141 help
142 exit
143 fi
144
8b17457 @holman ▁▂▃▅▂▇
holman authored
145 while read data; do
146 data=$data
147 break
148 done
149 fi
150
151 setup_array $data
152 print_ticks $data
Something went wrong with that request. Please try again.