dot

packages and services management
Log | Files | Refs | README

commit eb594865c496336c4ab0246d2f96ff6181079143
parent cfc72726f20d4e56d5efd5701ef14dfbf3a2e377
Author: Josuah Demangeon <josuah.demangeon@gandi.net>
Date:   Thu, 27 Jul 2017 12:11:11 +0200

bin/plot: automatically choose float or int

If the step is lower than 1 (0.???), choose float, otherwise choose int.

Diffstat:
Mbin/plot | 30++++++++++++++----------------
1 file changed, 14 insertions(+), 16 deletions(-)

diff --git a/bin/plot b/bin/plot @@ -2,7 +2,7 @@ awk ' BEGIN { for (i = 1; i < ARGC; i++) { - if (ARGV[i] == "-h" && i + 1 < ARGC) height = ARGV[++i] + if (ARGV[i] == "-h" && i + 1 < ARGC && ARGV[i]) height = ARGV[++i] else { print "Usage: plot [-h height]" exit 1 @@ -13,27 +13,25 @@ BEGIN { } { + maxx = split($0, dots) stepy = 1 - maxx = split($0, dots) # 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 + if (!height) height = maxy + height -= - height % 2 + stepy = maxy / height # draw the grid - 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 + margin = (length(maxy) + 3 < 15) ? 15 : length(maxy) + 3 + float = (stepy < 1) + for (y = height - height % 2; y >= 0; y -= 2) { + if (num = !num) { + if (float) printf "%" margin ".2f -|", (y + 1) * stepy + else printf "%" margin "d -|", (y + 1) * stepy + } else printf "%" margin "s |", "" + for (x = 1; x <= maxx; x++) printf "%s", (dots[x] > (y + 1) * stepy ? ":" : \ dots[x] > (y ) * stepy ? "." : " ") @@ -44,7 +42,7 @@ BEGIN { printf "%" margin "s0 +", "" for (x = 1; x <= maxx; x++) printf "-" - print "+ " maxx * stepx + print "+ " maxx exit }