-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdotFill
More file actions
executable file
·201 lines (136 loc) · 4.29 KB
/
dotFill
File metadata and controls
executable file
·201 lines (136 loc) · 4.29 KB
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
#!/usr/bin/env perl -w
#
# dotFill: Add dot-leader fills for readbility.
# 2007-11-09: Written by Steven J. DeRose.
#
use strict;
use Getopt::Long;
our %metadata = (
'title' => "dotFill",
'rightsHolder' => "Steven J. DeRose",
'creator' => "http://viaf.org/viaf/50334488",
'type' => "http://purl.org/dc/dcmitype/Software",
'language' => "Perl 5",
'created' => "2007-11-09",
'modified' => "2020-08-19",
'publisher' => "http://github.com/sderose",
'license' => "https://creativecommons.org/licenses/by-sa/3.0/"
);
our $VERSION_DATE = $metadata{'modified'};
=pod
=head1 Usage
dotFill [options] [file]
Turns space-runs into runs of some other character, on some or all lines.
This is useful for making tabular data more easily readable.
Tabs are expanded first.
=head1 Options
=over
=item * B<-c> I<c>
Character to fill in (default: '.').
=item * B<--expand>
Expand tabs (default).
=item * B<--min> I<n>
Only fill runs at least I<n> spaces long (default: 2).
=item * B<-n> I<n>
Do this every I<n> lines (default: 1).
=item * B<-q>
Suppress most messages.
=item * B<--runchar> I<c>
Replace runs of character I<c> (default: space).
=item * B<-t> I<n>
Set the tab interval to I<n> (default: 4).
=item * B<-v>
Add more detailed messages.
=item * B<--version>
Display version info and exit.
=back
=head1 Related commands
These commands are also useful for readability:
C<ds> -- double-space a file
C<align> -- pad inter-field space to line things up
C<dropBlankLines> -- delete lines with only whitespace
C<normalizeSpace> -- reduce runs of whitespace to a single space
C<numberLines> -- Display a line-number before each line
=head1 History
=over
=item * 2007-11-09: Written by Steven J. DeRose.
=item * 2010-03-24: Add -min, -runchar, -expand, perldoc.
=item * 2013-12-12: Clean up.
=item * 2020-08-19: New layout.
=back
=head1 To do
Option to only fill runs longer than a certain amount.
Handle all Unicode spaces.
=head1 Rights
Copyright 2007-11-09 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
###############################################################################
# Process options
#
my $expand = 1;
my $fillchar = ".";
my $min = 2;
my $num = 1;
my $quiet = 0;
my $runchar = " ";
my $tabinterval = 4;
my $verbose = 0;
Getopt::Long::Configure ("ignore_case");
my $result = GetOptions(
"c|fillchar=s" => \$fillchar,
"expand!" => \$expand,
"h|help|?" => sub { system "perldoc $0"; exit; },
"min=i" => \$min,
"n=i" => \$num,
"q|quiet!" => \$quiet,
"runchar=s" => \$runchar,
"tab=i" => \$tabinterval,
"v|verbose+" => \$verbose,
"version" => sub {
die "Version of $VERSION_DATE, by Steven J. DeRose.\n";
}
);
($result) || die "Bad options.\n";
$fillchar = substr($fillchar,0,1);
$runchar = substr($runchar,0,1);
($min > 0) || die "-min value must be >0.\n";
($num > 0) || die "-n value must be >0.\n";
###############################################################################
# Main
#
my $lhs = "((" . $runchar . "){" . $min . ",}" . ")";
($verbose) && warn "lhs: /$lhs/\n";
my $lineNum = 0;
while (my $rec = <>) {
$lineNum++;
if ($lineNum % $num == 0) {
if ($expand) { $rec = expandTabs($rec); }
$rec =~ s/$lhs/{$fillchar x length($1)}/ge;
}
print $rec;
}
($quiet) || warn "Done, $lineNum lines processed.\n";
exit;
###############################################################################
# Should do the same as the *nix 'expand' command.
#
sub expandTabs {
my $rec = $_[0];
my $interval = $_[1];
my $buf = "";
for (my $i=0; $i<length($rec); $i++) {
if (substr($rec,$i,1) eq "\t") {
my $needed = $interval - (length($buf) % $interval);
$buf .= (" " x $needed);
}
else {
$buf .= substr($rec,$i,1);
}
}
return($buf);
}