logarithmic scale option
authorMischa POSLAWSKY <perl@shiar.org>
Sat, 19 Nov 2022 19:26:02 +0000 (20:26 +0100)
committerMischa POSLAWSKY <perl@shiar.org>
Tue, 22 Nov 2022 23:06:46 +0000 (00:06 +0100)
Outstanding wishlist.

barcat
t/t0002-usage_-h_|wc_-l.out
t/t1260-powers_of_ten.in [new file with mode: 0644]
t/t1260-powers_of_ten_-e.out [new file with mode: 0644]
t/t1261-binary_powers.in [new file with mode: 0644]
t/t1261-binary_powers_-e.out [new file with mode: 0644]
t/t1262-powers_of_tenths.in [new file with mode: 0644]
t/t1262-powers_of_tenths_-e.out [new file with mode: 0644]

diff --git a/barcat b/barcat
index d93f251526e113c6508fda0cb602bbb9de411310..b3d9ce9a9c7e7fa784e07d25a384b5d8b4710104 100755 (executable)
--- a/barcat
+++ b/barcat
@@ -54,6 +54,7 @@ GetOptions(\%opt,
                        " (range expected)\n"
                );
        },
                        " (range expected)\n"
                );
        },
+       'log|e!',
        'header!',
        'markers|m=s',
        'graph-format=s' => sub {
        '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;
 ) // 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[$_] }
 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;
                        next;
                };
                $pos -= $minval;
+               $pos &&= log $pos if $opt{log};
                $pos >= 0 or next;
                color(36) for $barmark[$pos * $size] = $char;
        }
                $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;
                next;
        }
        printf '%-*s', $len + length($val), $line;
+       my $barlen = $values[$nr] || 0;
+       $barlen &&= log $barlen if $opt{log};
        print $barmark[$_] // $opt{'graph-format'}
        print $barmark[$_] // $opt{'graph-format'}
-               for 1 .. $size && (($values[$nr] || 0) - $minval) * $size + .5;
+               for 1 .. $size && ($barlen - $minval) * $size + .5;
        say '';
 }
 continue {
        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
   -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
       --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.
 
 All input is still counted and analyzed for statistics,
 but disregarded for padding and bar size.
 
+=item -e, --log
+
+Logarithmic (I<e>xponential) scale instead of linear
+to compare orders of magnitude.
+
 =item --graph-format=<character>
 
 Glyph to repeat for the graph line.
 =item --graph-format=<character>
 
 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 |
 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:
 
 
 And of course various Git statistics, such commit count by year:
 
index 8f92bfdd49766b1907d4aec8d3b0f9ed6129d0e6..7facc89938bbc5635e3d36ffa56b4c85e9b07db8 100644 (file)
@@ -1 +1 @@
-35
+36
diff --git a/t/t1260-powers_of_ten.in b/t/t1260-powers_of_ten.in
new file mode 100644 (file)
index 0000000..9f65adf
--- /dev/null
@@ -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 (file)
index 0000000..76ed8f4
--- /dev/null
@@ -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 (file)
index 0000000..1a5ae96
--- /dev/null
@@ -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 (file)
index 0000000..e1cd17a
--- /dev/null
@@ -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 (file)
index 0000000..93c30aa
--- /dev/null
@@ -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 (file)
index 0000000..018283a
--- /dev/null
@@ -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 -----------------------------------