Add spark

This commit is contained in:
Daniel Flanagan 2024-09-13 13:54:00 -05:00
parent 637c4e4273
commit f247344b05
2 changed files with 141 additions and 10 deletions

View file

@ -0,0 +1,127 @@
#!/usr/bin/env bash
#
# spark
# https://github.com/holman/spark
#
# Generates sparklines for a set of data.
#
# Here's a good web-based sparkline generator that was a bit of inspiration
# for spark:
#
# https://datacollective.org/sparkblocks
#
# spark takes a comma-separated or space-separated list of data and then prints
# a sparkline out of it.
#
# Examples:
#
# spark 1 5 22 13 53
# # => ▁▁▃▂▇
#
# spark 0 30 55 80 33 150
# # => ▁▂▃▅▂▇
#
# spark -h
# # => Prints the spark help text.
# Generates sparklines.
#
# $1 - The data we'd like to graph.
_echo()
{
if [ "X$1" = "X-n" ]; then
shift
printf "%s" "$*"
else
printf "%s\n" "$*"
fi
}
spark()
{
local n numbers=
# find min/max values
local min=0xffffffff max=0
for n in ${@//,/ }
do
# on Linux (or with bash4) we could use `printf %.0f $n` here to
# round the number but that doesn't work on OS X (bash3) nor does
# `awk '{printf "%.0f",$1}' <<< $n` work, so just cut it off
n=${n%.*}
(( n < min )) && min=$n
(( n > max )) && max=$n
numbers=$numbers${numbers:+ }$n
done
# print ticks
local ticks=(▁ ▂ ▃ ▄ ▅ ▆ ▇ █)
# use a high tick if data is constant
(( min == max )) && ticks=(▅ ▆)
local f=$(( (($max-$min)<<8)/(${#ticks[@]}-1) ))
(( f < 1 )) && f=1
for n in $numbers
do
_echo -n ${ticks[$(( ((($n-$min)<<8)/$f) ))]}
done
_echo
}
# If we're being sourced, don't worry about such things
if [ "$BASH_SOURCE" == "$0" ]; then
# Prints the help text for spark.
help()
{
local spark=$(basename $0)
cat <<EOF
USAGE:
$spark [-h|--help] VALUE,...
EXAMPLES:
$spark 1 5 22 13 53
▁▁▃▂█
$spark 0,30,55,80,33,150
▁▂▃▄▂█
echo 9 13 5 17 1 | $spark
▄▆▂█▁
EOF
}
# show help for no arguments if stdin is a terminal
if { [ -z "$1" ] && [ -t 0 ] ; } || [ "$1" == '-h' ] || [ "$1" == '--help' ]
then
help
exit 0
fi
spark ${@:-`cat`}
fi
# source: https://github.com/holman/spark/commit/ab88ac6f8f33698f39ece2f109b1117ef39a68eb
# The MIT License
#
# Copyright (c) Zach Holman, https://zachholman.com
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.

View file

@ -18,13 +18,15 @@
];
"modules-center" = [];
"modules-right" = [
"privacy"
"power-profiles-daemon"
"mpris"
"network"
## "disk"
## TODO: will need a custom module for Disk IO
## "wireplumber" # pulseaudio module is more featureful
"pulseaudio"
"network"
"cpu"
"memory"
"temperature"
@ -60,6 +62,7 @@
"tray" = {
"icon-size" = 24;
"spacing" = 4;
"show-passive-items" = true;
};
"clock" = {
"interval" = 1;
@ -90,8 +93,9 @@
};
"backlight" = {
# "device" = "acpi_video1";
"format" = "{percent}% {icon}";
"format" = "{percent}%\n{icon}";
"format-icons" = ["" ""];
"justify" = "center";
};
"battery" = {
"states" = {
@ -99,13 +103,13 @@
"warning" = 30;
"critical" = 1;
};
"format" = "{capacity}% {time} {icon}";
"format-charging" = "{capacity}% {time} 󱐋";
"format-plugged" = "{capacity}% {time} 󰚥";
"format-alt" = "{capacity}% {icon}";
"format" = "{icon}{capacity}%-\n{time}";
"format-charging" = "{capacity}%+\n{time}";
"format-plugged" = "{capacity}%=\n{time}";
"format-alt" = "{capacity}%";
"format-good" = ""; # An empty format will hide the module
"format-full" = "󰁹";
"format-icons" = ["󰂎" "󰁻" "󰁽" "󰁿" "󰂂"];
"format-time" = "{H}:{M}";
"justify" = "center";
};
"network" = {
"format-wifi" = "{bandwidthUpBits} up \n{bandwidthDownBits} down";
@ -130,8 +134,8 @@
*/
"format" = "{volume}%\n{format_source}";
"format-muted" = "MUTE\n{format_source}";
"format-source" = "HOT";
"format-source-muted" = "OFF";
"format-source" = "MIC ON";
"format-source-muted" = "MIC OFF";
# TODO: toggle mute?
"on-click" = "${pkgs.pavucontrol}/bin/pavucontrol";
"justify" = "center";