-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathshowScale
executable file
·337 lines (256 loc) · 8.58 KB
/
showScale
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
#!/usr/bin/env perl -w
#
# showScale: Display a line to help count character columns and widths.
# 2006-11-??: Written by Steven J. DeRose.
#
use strict;
use Getopt::Long;
our %metadata = (
'title' => "showScale",
'description' => "Display a line with character columns, or a separator.",
'rightsHolder' => "Steven J. DeRose",
'creator' => "http://viaf.org/viaf/50334488",
'type' => "http://purl.org/dc/dcmitype/Software",
'language' => "Perl 5.18",
'created' => "2006-11",
'modified' => "2021-08-23",
'publisher' => "http://github.com/sderose",
'license' => "https://creativecommons.org/licenses/by-sa/3.0/"
);
our $VERSION_DATE = $metadata{'modified'};
=pod
=head1 Usage
scale [options]
Displays a scale for counting columns in your *nix shell, such as:
----+----1----+----2----+----3----+----4----+----5
The numbers represent the second-smallest digit of the column number (in
decimal, the default, that's the "tens" digit). For even bases, a "+"
appears at the "halfway" points (multiples of 5, for decimal).
To start counting somewhere other than from 1, see I<--startCount>.
As long as environment variable '\$COLUMNS' has been set (for example, by
having C<export COLUMNS> in your F<.bashrc> or similar file),
the scale will be sized to fit your window.
Can also make plain separator line (see C<--separator>), such as shown below,
insert I<--comment> delimiters,
and/or display the line C<--up n> lines in an ANSI terminal program, so you
can place it near a line you want to measure.
###############################################################################
//=============================================================================
=head1 Options
=over
=item * B<--base> I<n>
Label the scale in base I<n>.
=item * B<--char> I<c>
The character to repeat for rules made via I<--separator> (default "#").
=item * B<--color>
Use color.
=item * B<--comment> I<extsion>
Turn the line into a syntactic comment as befits the specified extension
(don't include the dot),
such as by prefixing '//' or '#', or surrounding with '<!--' and '-->'.
=item * B<--hex>
Shorthand for I<--base 16>.
=item * B<--octal>
Shorthand for I<--base 8>.
=item * B<--separator>
Instead of a numbered scale, just draw I<--width> copies of I<--char>
(default "#").
=item * B<--startCount> I<n>
Number the first column as I<n> (default: 1).
I<n> may be specified in the usual bases.
This could be useful for showing offsets into 0-based strings,
absolute offsets for text extracted from known starting point(s) in a
file (say, with C<cut> or I<body>), etc.
=item * B<--up> I<n>
Display the scale I<n> lines above the current place,
for example, to put it closer to what you want to measure.
I<--up> uses ANSI terminal cursor-movement commands.
Not applicable to I<--separator> lines.
=item * B<--width> I<n>
Display I<n> columns of scale. Default: the value of the C<$COLUMNS>
environment variable, or 160 if not available.
I<n> may be specified in the usual bases.
=item * B<--zero>
Start the scale at offset 0 instead of 1.
This is just shorthand for C<--startCount 0>.
=back
=head1 Related commands
C<colorstring.pm> -- used for the I<--color> option.
=head1 Known bugs and limitations
B<Note>: C<bash> does not automatically C<export COLUMNS>, so
for this script to know your terminal width
your F<.bashrc> or similar file must do that explicitly.
Using I<--up> to move the scale further than the terminal's height
in lines, likely will not work (even if the location can be scrolled to).
This probably depends on the particular terminal program in use.
=head1 To do
Provide comment delimiters for various languages by extension.
See L<https://gist.github.com/ppisarczyk]>.
Finish makeVerticalLine() option to draw a vertical line at a given column(s).
=head1 History
2006-11-??: Written by Steven J. DeRose.
2008-01-17 sjd: Getopt.
2012-09-12: ?
2015-01-15: Add --startCount, --up, --width, --base.
2016-02-29: Fix doc.
2018-08-26: Add --separator.
2018-10-22: Add --zero. Fix 'colorstring' call.
2020-09-08: New layout. Add --char, doc --separator.
2020-12-18: Clean up. Start --extension.
2021-08-23: Add --hex and --octal synonyms.
=head1 Rights
Copyright 2006 by Steven J. DeRose. This work is licensed under a
Creative Commons Attribution-Share Alike 3.0 Unported License.
For further information on this license, see
L<https://creativecommons.org/licenses/by-sa/3.0>.
For the most recent version, see L<http://www.derose.net/steve/utilities> or
L<https://github.com/sderose>.
=cut
my %other = (
'asp' => '//',
'swf' => '//',
'php' => '//',
'rb' => '//',
'rss' => '//',
'cgi' => '//',
'r' => '//',
'el' => ';;',
# prolog, haskell, lisp, clisp, scheme, smalltalk
);
my %slash = (
'c' => '//',
'cpp' => '//',
'c#' => '//',
'css' => '//',
'js' => '//',
'jsp' => '//',
);
my %pound = (
'py' => '#',
'sh' => '#',
'pl' => '#',
);
my %sgml = (
'xml' => '//',
'htm' => '//',
'html' => '//',
'sgm' => '//',
'sgml' => '//',
'svg' => '//',
);
# Could use 'curses' but it seems like overkill.
my $cursorUp = "\x1B[A";
my $cursorDown = "\x1B[B";
my $cursorLeft = "";
my $cursorRight = "";
my $cursorCol1 = "";
# TODO: Add escaping, indentation. Hook up.
sub commenter {
my ($line, $ext) = @_;
if ($slash{$ext}) { return '// ' . $line; }
if ($pound{$ext}) { return '# ' . $line; }
if ($sgml{$ext}) { return '<!-- ' . $line . ' -->'; }
die "Unsupported --ext value '$ext'.\n";
}
###############################################################################
# Options
#
my $base = 10;
my $char = "#";
my $color = ($ENV{CLI_COLOR} && -t STDERR) ? 1:0;
my $comment = 0;
my $quiet = 0;
my $rule = "";
my $separator = 0;
my $startCount = 1;
my $up = 0;
my $verbose = 0;
my $width = 0;
my $zero = 0;
Getopt::Long::Configure ("ignore_case");
my $result = GetOptions(
"base=o" => \$base,
"char=s" => \$char,
"c|color!" => \$color,
"comment!" => \$comment,
"h|help|?" => sub { system "perldoc $0"; exit; },
"hex" => sub { $base = 16; },
"octal" => sub { $base = 8; },
"q|quiet!" => \$quiet,
"rule=s" => \$rule,
"separator!" => \$separator,
"startCount=o" => \$startCount,
"up=o" => \$up,
"v|verbose+" => \$verbose,
"version" => sub {
die "Version of $VERSION_DATE, by Steven J. DeRose.\n";
},
"width=o" => \$width,
"zero!" => \$zero,
);
($result) || die "Bad options.\n";
(length($char) == 1) || die "--char must be exactly one character.\n";
###############################################################################
# Main
#
if ($width == 0) { $width = $ENV{COLUMNS}; }
if (!$width) {
if (!$quiet) {
warn "Can't get \$COLUMNS -- .profile could export it (cf --width).\n";
}
$width = 160;
}
if ($separator) {
print(($char x $width) . "\n");
exit;
}
my $r=""; my $c="";
if ($color) {
$r = `colorstring.pm red`;
$c = `colorstring.pm cancel`;
}
my $scale = "";
if (0) {
$scale = "----+----1----+----2----+----3----+----4----+----5"
. "----+----6----+----7----+----8----+----9----+----0";
my $h = ($width/100) + 1;
$scale = $scale x $h;
$scale = substr($scale, 0, $width-1);
}
else {
$scale = makeScale($startCount, $base, $width);
}
if ($zero) { $scale = "0" . substr($scale, 0, length($scale)-1); }
if ($up) {
$scale = ($cursorUp x $up) . "\r" . $scale . ($cursorDown x $up);
}
print "$r$scale$c\n";
exit;
##############################################################################
#
sub makeScale {
my ($start, $base, $width) = @_;
my $halfBase = ($base % 2) ? 0:int($base/2); # Even bases only
my $buf = "";
for (my $i=0, my $n=$start; $i<$width; $i++, $n++) {
if ($n % $base == 0) {
my $label = sprintf("%d", int($n/$base));
$buf .= substr($label, length($label)-1);
}
elsif ($halfBase && ($n % $halfBase == 0)) {
$buf .= '+';
}
else {
$buf .= '-';
}
}
return($buf);
}
sub makeVerticalLine {
my ($lines, $col) = @_;
my $buf = $cursorUp x $lines;
for (my $i=0; $i<$lines; $i++) {
$buf .= $cursorDown + "|" + $cursorLeft;
}
return $buf;
}