Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100755 104 lines (90 sloc) 2.038 kB
4155609 @nrr Make the spark script work on systems like NetBSD where bash lives so…
nrr authored
1 #!/usr/bin/env bash
8b17457 @holman ▁▂▃▅▂▇
authored
2 #
2e2f748 @holman Update docs
authored
3 # spark
8b17457 @holman ▁▂▃▅▂▇
authored
4 # https://github.com/holman/spark
5 #
6 # Generates sparklines for a set of data.
7 #
fe2e8af @holman Fix docs typo
authored
8 # Here's a good web-based sparkline generator that was a bit of inspiration
8b17457 @holman ▁▂▃▅▂▇
authored
9 # for spark:
10 #
11 # https://datacollective.org/sparkblocks
12 #
114b5eb @holman Clarify usage in docs (fixes #82)
authored
13 # spark takes a comma-separated or space-separated list of data and then prints
14 # a sparkline out of it.
8b17457 @holman ▁▂▃▅▂▇
authored
15 #
16 # Examples:
17 #
90b7ae7 @heyc Example updates, documentation changes
heyc authored
18 # spark 1 5 22 13 53
8b17457 @holman ▁▂▃▅▂▇
authored
19 # # => ▁▁▃▂▇
20 #
90b7ae7 @heyc Example updates, documentation changes
heyc authored
21 # spark 0 30 55 80 33 150
8b17457 @holman ▁▂▃▅▂▇
authored
22 # # => ▁▂▃▅▂▇
23 #
24 # spark -h
25 # # => Prints the spark help text.
26
ecdfe37 @markusfisch streamlined processing
markusfisch authored
27 # Generates sparklines.
8b17457 @holman ▁▂▃▅▂▇
authored
28 #
2e2f748 @holman Update docs
authored
29 # $1 - The data we'd like to graph.
200be3c @thcipriani Remove echo, Create _echo Function, Use printf
thcipriani authored
30 _echo()
31 {
32 if [ "X$1" = "X-n" ]; then
33 shift
34 printf "%s" "$*"
35 else
36 printf "%s\n" "$*"
37 fi
38 }
39
ecdfe37 @markusfisch streamlined processing
markusfisch authored
40 spark()
8b17457 @holman ▁▂▃▅▂▇
authored
41 {
388429c @holman retab
authored
42 local n numbers=
ecdfe37 @markusfisch streamlined processing
markusfisch authored
43
388429c @holman retab
authored
44 # find min/max values
45 local min=0xffffffff max=0
ecdfe37 @markusfisch streamlined processing
markusfisch authored
46
388429c @holman retab
authored
47 for n in ${@//,/ }
48 do
49 # on Linux (or with bash4) we could use `printf %.0f $n` here to
50 # round the number but that doesn't work on OS X (bash3) nor does
51 # `awk '{printf "%.0f",$1}' <<< $n` work, so just cut it off
52 n=${n%.*}
53 (( n < min )) && min=$n
54 (( n > max )) && max=$n
55 numbers=$numbers${numbers:+ }$n
56 done
ecdfe37 @markusfisch streamlined processing
markusfisch authored
57
388429c @holman retab
authored
58 # print ticks
59 local ticks=(▁ ▂ ▃ ▄ ▅ ▆ ▇ █)
643bbdc @holman Remove comment
authored
60
2550076 @ncanceill use a larger tick if data is constant
ncanceill authored
61 # use a high tick if data is constant
62 (( min == max )) && ticks=(▅ ▆)
63
388429c @holman retab
authored
64 local f=$(( (($max-$min)<<8)/(${#ticks[@]}-1) ))
65 (( f < 1 )) && f=1
ecdfe37 @markusfisch streamlined processing
markusfisch authored
66
388429c @holman retab
authored
67 for n in $numbers
68 do
200be3c @thcipriani Remove echo, Create _echo Function, Use printf
thcipriani authored
69 _echo -n ${ticks[$(( ((($n-$min)<<8)/$f) ))]}
388429c @holman retab
authored
70 done
200be3c @thcipriani Remove echo, Create _echo Function, Use printf
thcipriani authored
71 _echo
8b17457 @holman ▁▂▃▅▂▇
authored
72 }
73
60c4d0d @akatrevorjay Allow spark to be sourced by other scripts easily
akatrevorjay authored
74 # If we're being sourced, don't worry about such things
1912376 @zsprackett Fix it so that sourcing works properly
zsprackett authored
75 if [ "$BASH_SOURCE" == "$0" ]; then
76 # Prints the help text for spark.
77 help()
78 {
fae5393 @jleclanche Use $0 in the usage, instead of hardcoding the name "spark"
jleclanche authored
79 local spark=$(basename $0)
1912376 @zsprackett Fix it so that sourcing works properly
zsprackett authored
80 cat <<EOF
60c4d0d @akatrevorjay Allow spark to be sourced by other scripts easily
akatrevorjay authored
81
1912376 @zsprackett Fix it so that sourcing works properly
zsprackett authored
82 USAGE:
fae5393 @jleclanche Use $0 in the usage, instead of hardcoding the name "spark"
jleclanche authored
83 $spark [-h|--help] VALUE,...
1912376 @zsprackett Fix it so that sourcing works properly
zsprackett authored
84
85 EXAMPLES:
fae5393 @jleclanche Use $0 in the usage, instead of hardcoding the name "spark"
jleclanche authored
86 $spark 1 5 22 13 53
1912376 @zsprackett Fix it so that sourcing works properly
zsprackett authored
87 ▁▁▃▂█
fae5393 @jleclanche Use $0 in the usage, instead of hardcoding the name "spark"
jleclanche authored
88 $spark 0,30,55,80,33,150
1912376 @zsprackett Fix it so that sourcing works properly
zsprackett authored
89 ▁▂▃▄▂█
fae5393 @jleclanche Use $0 in the usage, instead of hardcoding the name "spark"
jleclanche authored
90 echo 9 13 5 17 1 | $spark
1912376 @zsprackett Fix it so that sourcing works properly
zsprackett authored
91 ▄▆▂█▁
92 EOF
93 }
8b17457 @holman ▁▂▃▅▂▇
authored
94
1912376 @zsprackett Fix it so that sourcing works properly
zsprackett authored
95 # show help for no arguments if stdin is a terminal
312210d @Niggler --help also invokes help
Niggler authored
96 if { [ -z "$1" ] && [ -t 0 ] ; } || [ "$1" == '-h' ] || [ "$1" == '--help' ]
1912376 @zsprackett Fix it so that sourcing works properly
zsprackett authored
97 then
98 help
3110338 @mohnish Use an exit status of 0
mohnish authored
99 exit 0
1912376 @zsprackett Fix it so that sourcing works properly
zsprackett authored
100 fi
101
102 spark ${@:-`cat`}
103 fi
Something went wrong with that request. Please try again.