dot

packages and services management
Log | Files | Refs | README

commit cfc72726f20d4e56d5efd5701ef14dfbf3a2e377
parent a74e2e0cf738f74b095b9696b2d5045958537f10
Author: Josuah Demangeon <josuah.demangeon@gandi.net>
Date:   Thu, 27 Jul 2017 00:24:39 +0200

bin/plot: automatic height

Add a '-h num' argument that sets the height of the plot

Diffstat:
Mbin/plot | 43++++++++++++++++++++++---------------------
1 file changed, 22 insertions(+), 21 deletions(-)

diff --git a/bin/plot b/bin/plot @@ -1,13 +1,10 @@ awk ' BEGIN { - stepx = stepy = 1 - for (i = 1; i < ARGC; i++) { - if (ARGV[i] == "-x" && i + 1 < ARGC) stepx = ARGV[++i] - else if (ARGV[i] == "-y" && i + 1 < ARGC) stepy = ARGV[++i] + if (ARGV[i] == "-h" && i + 1 < ARGC) height = ARGV[++i] else { - print "Usage: plot [-x step] [-y step]" + print "Usage: plot [-h height]" exit 1 } } @@ -16,28 +13,30 @@ BEGIN { } { + stepy = 1 maxx = split($0, dots) - maxy = max = 0 - # scale the values and find the max - for (x = 1; x <= maxx; x++) { - if (max < dots[x]) max = dots[x] - dots[x] = int(dots[x] / stepy) - if (maxy < dots[x]) maxy = dots[x] - } + # find the max and the steps + for (x = 1; x <= maxx; x++) + if (maxy < dots[x]) maxy = dots[x] + if (height) + stepy = maxy / height + else + height = maxy # draw the grid - margin = length(max) > 10 ? length(max) : 10 - for (y = maxy + maxy % 2; y >= 1; y -= 2) { - if (y % 4) - printf "%" margin "s |", "" + float = (int(stepy) != stepy) + margin = length(maxy) + float * 4 > 10 ? length(maxy) + float * 4 : 10 + for (y = height + height % 2; y >= 0; y -= 2) { + if (num = !num) + printf "%" margin "s |", "" + else if (float) + printf "%" margin ".3f -|", (y + 1) * stepy else - printf "%" margin "d -|", (y - 1) * stepy - + printf "%" margin "d -|", (y + 1) * stepy for (x = 1; x <= maxx; x++) - printf "%s", (dots[x] >= y ? ":" : \ - dots[x] >= y - 1 ? "." : " ") - + printf "%s", (dots[x] > (y + 1) * stepy ? ":" : \ + dots[x] > (y ) * stepy ? "." : " ") print "" } @@ -46,5 +45,7 @@ BEGIN { for (x = 1; x <= maxx; x++) printf "-" print "+ " maxx * stepx + + exit } ' "$@"