From 1c54c0c39802740c0a5da001199f669497045a30 Mon Sep 17 00:00:00 2001 From: Mischa POSLAWSKY Date: Sat, 19 Nov 2022 20:26:02 +0100 Subject: [PATCH] logarithmic scale option Outstanding wishlist. --- barcat | 15 +++++++- t/t0002-usage_-h_|wc_-l.out | 2 +- t/t1260-powers_of_ten.in | 7 ++++ t/t1260-powers_of_ten_-e.out | 7 ++++ t/t1261-binary_powers.in | 66 +++++++++++++++++++++++++++++++++ t/t1261-binary_powers_-e.out | 66 +++++++++++++++++++++++++++++++++ t/t1262-powers_of_tenths.in | 15 ++++++++ t/t1262-powers_of_tenths_-e.out | 15 ++++++++ 8 files changed, 190 insertions(+), 3 deletions(-) create mode 100644 t/t1260-powers_of_ten.in create mode 100644 t/t1260-powers_of_ten_-e.out create mode 100644 t/t1261-binary_powers.in create mode 100644 t/t1261-binary_powers_-e.out create mode 100644 t/t1262-powers_of_tenths.in create mode 100644 t/t1262-powers_of_tenths_-e.out diff --git a/barcat b/barcat index d93f251..b3d9ce9 100755 --- a/barcat +++ b/barcat @@ -54,6 +54,7 @@ GetOptions(\%opt, " (range expected)\n" ); }, + 'log|e!', 'header!', 'markers|m=s', 'graph-format=s' => sub { @@ -230,6 +231,7 @@ my $maxval = $opt{maxval} // ( ) // 0; my $minval = $opt{minval} // min $order[-1] // (), 0; my $range = $maxval - $minval; +$range &&= log $maxval if $opt{log}; my $lenval = $opt{'value-length'} // max map { length } @order; my $len = defined $opt{trim} && $opt{trim} <= 0 ? -$opt{trim} + 1 : max map { length $values[$_] && length $lines[$_] } @@ -264,6 +266,7 @@ if ($opt{markers} and $size > 0) { next; }; $pos -= $minval; + $pos &&= log $pos if $opt{log}; $pos >= 0 or next; color(36) for $barmark[$pos * $size] = $char; } @@ -323,8 +326,10 @@ while ($nr <= $limit) { next; } printf '%-*s', $len + length($val), $line; + my $barlen = $values[$nr] || 0; + $barlen &&= log $barlen if $opt{log}; print $barmark[$_] // $opt{'graph-format'} - for 1 .. $size && (($values[$nr] || 0) - $minval) * $size + .5; + for 1 .. $size && ($barlen - $minval) * $size + .5; say ''; } continue { @@ -391,6 +396,7 @@ Options: -l, --length=[-]SIZE[%] Trim line contents (between number and bars) -L, --limit[=(N|-LAST|START-[END])] Stop output after a number of lines + -e, --log Logarithmic (exponential) scale instead of linear --graph-format=CHAR Glyph to repeat for the graph line -m, --markers=FORMAT Statistical positions to indicate on bars --min=N, --max=N Bars extend from 0 or the minimum value if lower @@ -501,6 +507,11 @@ A specific range can be given by two values. All input is still counted and analyzed for statistics, but disregarded for padding and bar size. +=item -e, --log + +Logarithmic (Ixponential) scale instead of linear +to compare orders of magnitude. + =item --graph-format= Glyph to repeat for the graph line. @@ -698,7 +709,7 @@ Total population history in XML from the World Bank: Population and other information for all countries: curl http://download.geonames.org/export/dump/countryInfo.txt | - grep -v '^#\s' | column -ts$'\t' -n | barcat -f+2 -u -l150 -s + grep -v '^#\s' | column -ts$'\t' -n | barcat -f+2 -e -u -l150 -s And of course various Git statistics, such commit count by year: diff --git a/t/t0002-usage_-h_|wc_-l.out b/t/t0002-usage_-h_|wc_-l.out index 8f92bfd..7facc89 100644 --- a/t/t0002-usage_-h_|wc_-l.out +++ b/t/t0002-usage_-h_|wc_-l.out @@ -1 +1 @@ -35 +36 diff --git a/t/t1260-powers_of_ten.in b/t/t1260-powers_of_ten.in new file mode 100644 index 0000000..9f65adf --- /dev/null +++ b/t/t1260-powers_of_ten.in @@ -0,0 +1,7 @@ +1 +10 +100 +1000 +1e4 +100000 +1e6 diff --git a/t/t1260-powers_of_ten_-e.out b/t/t1260-powers_of_ten_-e.out new file mode 100644 index 0000000..76ed8f4 --- /dev/null +++ b/t/t1260-powers_of_ten_-e.out @@ -0,0 +1,7 @@ + 1 + 10 ------ + 100 ----------< + 1000 ----------<----+- + 1e4 ----------<----+------ +100000 ----------<----+--------->-= + 1e6 ----------<----+--------->-=----- diff --git a/t/t1261-binary_powers.in b/t/t1261-binary_powers.in new file mode 100644 index 0000000..1a5ae96 --- /dev/null +++ b/t/t1261-binary_powers.in @@ -0,0 +1,66 @@ +1 2^0 +2 2^1 +4 2^2 +8 2^3 +16 2^4 +32 2^5 +64 2^6 +128 2^7 +256 2^8 +512 2^9 +1024 2^10 +2048 2^11 +4096 2^12 +8192 2^13 +16384 2^14 +32768 2^15 +65536 2^16 +131072 2^17 +262144 2^18 +524288 2^19 +1048576 2^20 +2097152 2^21 +4194304 2^22 +8388608 2^23 +16777216 2^24 +33554432 2^25 +67108864 2^26 +134217728 2^27 +268435456 2^28 +536870912 2^29 +1073741824 2^30 +2147483648 2^31 +4294967296 2^32 +8589934592 2^33 +17179869184 2^34 +34359738368 2^35 +68719476736 2^36 +137438953472 2^37 +274877906944 2^38 +549755813888 2^39 +1099511627776 2^40 +2199023255552 2^41 +4398046511104 2^42 +8796093022208 2^43 +17592186044416 2^44 +35184372088832 2^45 +70368744177664 2^46 +140737488355328 2^47 +281474976710656 2^48 +562949953421312 2^49 +1125899906842624 2^50 +2251799813685248 2^51 +4503599627370496 2^52 +9007199254740992 2^53 +18014398509481984 2^54 +36028797018963968 2^55 +72057594037927936 2^56 +144115188075855872 2^57 +288230376151711744 2^58 +576460752303423488 2^59 +1152921504606846976 2^60 +2305843009213693952 2^61 +4611686018427387904 2^62 +9223372036854775808 2^63 +18446744073709551616 2^64 +36893488147419103232 2^65 diff --git a/t/t1261-binary_powers_-e.out b/t/t1261-binary_powers_-e.out new file mode 100644 index 0000000..e1cd17a --- /dev/null +++ b/t/t1261-binary_powers_-e.out @@ -0,0 +1,66 @@ + 1 2^0 + 2 2^1 + 4 2^2 + 8 2^3 - + 16 2^4 - + 32 2^5 - + 64 2^6 - + 128 2^7 -- + 256 2^8 -- + 512 2^9 -- + 1024 2^10 -- + 2048 2^11 -- + 4096 2^12 --- + 8192 2^13 --- + 16384 2^14 --- + 32768 2^15 --- + 65536 2^16 --- + 131072 2^17 ---< + 262144 2^18 ---< + 524288 2^19 ---< + 1048576 2^20 ---< + 2097152 2^21 ---<- + 4194304 2^22 ---<- + 8388608 2^23 ---<- + 16777216 2^24 ---<- + 33554432 2^25 ---<- + 67108864 2^26 ---<-- + 134217728 2^27 ---<-- + 268435456 2^28 ---<-- + 536870912 2^29 ---<-- + 1073741824 2^30 ---<-- + 2147483648 2^31 ---<--+ + 4294967296 2^32 ---<--+ + 8589934592 2^33 ---<--+ + 17179869184 2^34 ---<--+ + 34359738368 2^35 ---<--+- + 68719476736 2^36 ---<--+- + 137438953472 2^37 ---<--+- + 274877906944 2^38 ---<--+- + 549755813888 2^39 ---<--+- + 1099511627776 2^40 ---<--+-> + 2199023255552 2^41 ---<--+-> + 4398046511104 2^42 ---<--+-> + 8796093022208 2^43 ---<--+-> + 17592186044416 2^44 ---<--+-> + 35184372088832 2^45 ---<--+->- + 70368744177664 2^46 ---<--+->- + 140737488355328 2^47 ---<--+->- + 281474976710656 2^48 ---<--+->- + 562949953421312 2^49 ---<--+->-- + 1125899906842624 2^50 ---<--+->-- + 2251799813685248 2^51 ---<--+->-- + 4503599627370496 2^52 ---<--+->-- + 9007199254740992 2^53 ---<--+->-- + 18014398509481984 2^54 ---<--+->--= + 36028797018963968 2^55 ---<--+->--= + 72057594037927936 2^56 ---<--+->--= + 144115188075855872 2^57 ---<--+->--= + 288230376151711744 2^58 ---<--+->--= + 576460752303423488 2^59 ---<--+->--=- + 1152921504606846976 2^60 ---<--+->--=- + 2305843009213693952 2^61 ---<--+->--=- + 4611686018427387904 2^62 ---<--+->--=- + 9223372036854775808 2^63 ---<--+->--=-- +18446744073709551616 2^64 ---<--+->--=-- +36893488147419103232 2^65 ---<--+->--=-- diff --git a/t/t1262-powers_of_tenths.in b/t/t1262-powers_of_tenths.in new file mode 100644 index 0000000..93c30aa --- /dev/null +++ b/t/t1262-powers_of_tenths.in @@ -0,0 +1,15 @@ +1e-5 +1e-4 +1e-3 +.01 +.1 +.2 +.5 +.8 +.9 +.99 +1 +1.1 +1.2 +1.5 +2 diff --git a/t/t1262-powers_of_tenths_-e.out b/t/t1262-powers_of_tenths_-e.out new file mode 100644 index 0000000..018283a --- /dev/null +++ b/t/t1262-powers_of_tenths_-e.out @@ -0,0 +1,15 @@ +1e-5 +1e-4 +1e-3 + .01 + .1 + .2 + .5 + .8 + .9 + .99 + 1 + 1.1 ----- + 1.2 --------- + 1.5 -------------------- + 2 ----------------------------------- -- 2.30.0