-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhbar
executable file
·160 lines (130 loc) · 3.97 KB
/
hbar
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
#!/usr/bin/env perl
use warnings;
use strict;
use List::Util qw( max min );
use Scalar::Util qw(looks_like_number);
use Term::Colormap qw( colormap print_colored );
use Term::ReadKey; # GetTerminalSize
use constant DEBUG => 0;
main();
1;
sub main {
my @data;
# getopt and friends
# nothing in stdin, waiting for user input
usage() if (-t STDIN);
while (<>) {
next unless /\S/;
s/^\s+//;
s/\s+$//;
my ($label, $value);
my ($field_1, $field_2);
if (/,/) {
# Comma Separated
($field_1, $field_2) = split /,/;
} else {
# Whitespace Separated, try to figure out the value column
my @fields = split /\s/;
if (looks_like_number($fields[0])) {
$field_1 = shift @fields;
} elsif (looks_like_number($fields[-1])) {
$field_1 = pop @fields;
} else {
die "Cannot figure out the fields!";
}
$field_2 = join ' ', @fields;
}
# Assign correctly
if (looks_like_number($field_1)) {
$value = $field_1;
$label = $field_2;
} else {
$value = $field_2;
$label = $field_1;
}
# Ignore if we don't understand
if ( defined $label and looks_like_number($value) ) {
push @data, { label => $label, value => $value };
}
}
my $max_label_length = max map { length( $_->{label} ) } @data;
my $max_value = max map { $_->{value} } @data;
my $min_value = min map { $_->{value} } @data;
my ($screen_cols, $screen_rows, undef, undef) = GetTerminalSize();
my $max_data_width = $screen_cols
- $max_label_length # Left Pad for label
- int(0.1 * $screen_cols); # Right Pad for value
my $zero_column;
if ($min_value < 0) {
$zero_column = abs(int( ($min_value) / ($max_value - $min_value) * $max_data_width));
} else {
$zero_column = 0;
}
my $rainbow = colormap('rainbow');
my $rainbow_length = scalar @$rainbow;
my $rows = scalar @data;
my $row = 0;
my $rotate_colors = ($rows > $rainbow_length) ? 1 : 0;
debug("zero_column = $zero_column");
print "\n";
for ( @data ) {
my ($label) = $_->{label};
my ($value) = $_->{value};
print sprintf("% ${max_label_length}s ", $label);
my $scaled_value;
if ($min_value < 0) {
$scaled_value = int($max_data_width*
( $_->{value} - $min_value ) / ( $max_value - $min_value ));
} else {
$scaled_value = int($max_data_width*( $_->{value} / $max_value ));
}
my $color = $rotate_colors
? $rainbow->[ $row % $rainbow_length ]
: $rainbow->[int( $row * ( ( scalar @$rainbow ) / $rows ) )];
debug("scaled_value = $scaled_value");
if ($scaled_value < $zero_column) {
# Negative Values
print " "x($scaled_value); # Whitespace Padding
print_colored( $color, (' ' x ( int ($zero_column - $scaled_value) )));
print_colored( $color, '|' ); # Zero Marker
} else {
# Positive Values
print " "x($zero_column); # Whitespace Padding
print_colored( $color, '|' ); # Zero Marker
print_colored( $color, (' ' x ( int ($scaled_value - $zero_column) )));
}
print " $value\n";
$row++;
}
print "\n";
}
sub usage {
my $usage = <<USAGE;
usage:
echo '
hello,-5
world,-4
bob,-3
cat,-2
foo,-1
bar,0
qux,1
qix,5
baz,20
' | $0
echo '
1 one
1 one
2 two
5 five
8 eight
13 thirteen
21 twenty one
' | $0
who | cut -d' ' -f1 | sort | uniq -c | sort -nr | $0
USAGE
die $usage;
}
sub debug {
DEBUG and warn join('', @_, "\n");
}