Files
fish-config/functions/spark.fish
T
rootiest 02c46ebea7 docs(functions): merge manual-only examples and notes into headers
Folds the examples Section 5 carried but the headers did not into each
function's `# EXAMPLE`, and moves the three lines that only looked like
examples -- the two typo-abbreviation notes and rm's /usr/bin/rm fallback --
into `# NOTES`, the label already in use.

Also corrects gi's synopsis, which omitted -l, and documents yt-dlp's
--no-embed-thumbnail in `# ARGUMENTS`.
2026-07-26 03:47:25 -04:00

59 lines
2.1 KiB
Fish

# Copyright (C) 2026 Rootiest
# SPDX-License-Identifier: AGPL-3.0-or-later
# CATEGORY
# 13-media-and-utilities
#
# SYNOPSIS
# spark [--min=<n>] [--max=<n>] [numbers...]
#
# DESCRIPTION
# Renders a Unicode sparkline bar chart for a sequence of numbers.
# Reads numbers from arguments or from stdin if none are provided.
# Optional --min and --max clamp the scale range.
#
# ARGUMENTS
# --min=<n> Minimum value for scale (default: list minimum)
# --max=<n> Maximum value for scale (default: list maximum)
# numbers... Space-separated numbers to chart; reads stdin if omitted
# -v, --version Print version
# -h, --help Show usage help
#
# EXAMPLE
# spark 1 1 2 5 14 42
# seq 64 | sort --random-sort | spark
# echo "3 7 2 9 1" | spark
function spark --description 'Sparklines'
argparse --ignore-unknown --name=spark v/version h/help m/min= M/max= -- $argv || return
if set --query _flag_version[1]
echo "spark, version 1.1.0"
else if set --query _flag_help[1]
echo "Usage: spark <numbers ...>"
echo " stdin | spark"
echo "Options:"
echo " --min=<number> Minimum range"
echo " --max=<number> Maximum range"
echo " -v or --version Print version"
echo " -h or --help Print this help message"
echo "Examples:"
echo " spark 1 1 2 5 14 42"
echo " seq 64 | sort --random-sort | spark"
else if set --query argv[1]
printf "%s\n" $argv | spark --min="$_flag_min" --max="$_flag_max"
else
command awk -v min="$_flag_min" -v max="$_flag_max" '
{
m = min == "" ? m == "" ? $0 : m > $0 ? $0 : m : min
M = max == "" ? M == "" ? $0 : M < $0 ? $0 : M : max
nums[NR] = $0
}
END {
n = split("▁ ▂ ▃ ▄ ▅ ▆ ▇ █", sparks, " ") - 1
while (++i <= NR)
printf("%s", sparks[(M == m) ? 3 : sprintf("%.f", (1 + (nums[i] - m) * n / (M - m)))])
}
' && echo
end
end