diff options
| author | Yigit Sever | 2021-11-07 23:42:15 +0300 |
|---|---|---|
| committer | Yigit Sever | 2021-11-07 23:42:15 +0300 |
| commit | 9fa9973c2d2f91cef7bdd26cb52064c13f4d682e (patch) | |
| tree | 13aa64b09bc91e16f3dd3217bf3d647559af2cf9 /.local/bin/spark | |
| parent | 9382166b5522a5d9e2d62e93e702b4d0886cedb4 (diff) | |
| parent | fcb2237c01bcd2b7982bb844cea4cf9447f20d03 (diff) | |
| download | dotfiles-9fa9973c2d2f91cef7bdd26cb52064c13f4d682e.tar.gz dotfiles-9fa9973c2d2f91cef7bdd26cb52064c13f4d682e.tar.bz2 dotfiles-9fa9973c2d2f91cef7bdd26cb52064c13f4d682e.zip | |
Merge remote-tracking branch 'origin/main'
Diffstat (limited to '.local/bin/spark')
| -rwxr-xr-x | .local/bin/spark | 103 |
1 files changed, 103 insertions, 0 deletions
diff --git a/.local/bin/spark b/.local/bin/spark new file mode 100755 index 0000000..53a38be --- /dev/null +++ b/.local/bin/spark | |||
| @@ -0,0 +1,103 @@ | |||
| 1 | #!/usr/bin/env bash | ||
| 2 | # | ||
| 3 | # spark | ||
| 4 | # https://github.com/holman/spark | ||
| 5 | # | ||
| 6 | # Generates sparklines for a set of data. | ||
| 7 | # | ||
| 8 | # Here's 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 or space-separated list of data and then prints | ||
| 14 | # a sparkline out of 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 | |||
| 27 | # Generates sparklines. | ||
| 28 | # | ||
| 29 | # $1 - The data we'd like to graph. | ||
| 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 | |||
| 40 | spark() | ||
| 41 | { | ||
| 42 | local n numbers= | ||
| 43 | |||
| 44 | # find min/max values | ||
| 45 | local min=0xffffffff max=0 | ||
| 46 | |||
| 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 | ||
| 57 | |||
| 58 | # print ticks | ||
| 59 | local ticks=(▁ ▂ ▃ ▄ ▅ ▆ ▇ █) | ||
| 60 | |||
| 61 | # use a high tick if data is constant | ||
| 62 | (( min == max )) && ticks=(▅ ▆) | ||
| 63 | |||
| 64 | local f=$(( (($max-$min)<<8)/(${#ticks[@]}-1) )) | ||
| 65 | (( f < 1 )) && f=1 | ||
| 66 | |||
| 67 | for n in $numbers | ||
| 68 | do | ||
| 69 | _echo -n ${ticks[$(( ((($n-$min)<<8)/$f) ))]} | ||
| 70 | done | ||
| 71 | _echo | ||
| 72 | } | ||
| 73 | |||
| 74 | # If we're being sourced, don't worry about such things | ||
| 75 | if [ "$BASH_SOURCE" == "$0" ]; then | ||
| 76 | # Prints the help text for spark. | ||
| 77 | help() | ||
| 78 | { | ||
| 79 | local spark=$(basename $0) | ||
| 80 | cat <<EOF | ||
| 81 | |||
| 82 | USAGE: | ||
| 83 | $spark [-h|--help] VALUE,... | ||
| 84 | |||
| 85 | EXAMPLES: | ||
| 86 | $spark 1 5 22 13 53 | ||
| 87 | ▁▁▃▂█ | ||
| 88 | $spark 0,30,55,80,33,150 | ||
| 89 | ▁▂▃▄▂█ | ||
| 90 | echo 9 13 5 17 1 | $spark | ||
| 91 | ▄▆▂█▁ | ||
| 92 | EOF | ||
| 93 | } | ||
| 94 | |||
| 95 | # show help for no arguments if stdin is a terminal | ||
| 96 | if { [ -z "$1" ] && [ -t 0 ] ; } || [ "$1" == '-h' ] || [ "$1" == '--help' ] | ||
| 97 | then | ||
| 98 | help | ||
| 99 | exit 0 | ||
| 100 | fi | ||
| 101 | |||
| 102 | spark ${@:-`cat`} | ||
| 103 | fi | ||
