-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinsertAt
More file actions
executable file
·216 lines (159 loc) · 4.86 KB
/
insertAt
File metadata and controls
executable file
·216 lines (159 loc) · 4.86 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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
#!/usr/bin/env perl -w
#
# insertAt: Insert a string or file, at a given place in stdin.
# Written ~2006 by Steven J. DeRose.
#
use strict;
use Getopt::Long;
our %metadata = (
'title' => "insertAt.pm",
'description' => "Insert a string or file, at a given place in stdin.",
'rightsHolder' => "Steven J. DeRose",
'creator' => "http://viaf.org/viaf/50334488",
'type' => "http://purl.org/dc/dcmitype/Software",
'language' => "Perl 5",
'created' => "2006",
'modified' => "2021-02-06",
'publisher' => "http://github.com/sderose",
'license' => "https://creativecommons.org/licenses/by-sa/3.0/"
);
our $VERSION_DATE = $metadata{'modified'};
=pod
=head1 Usage
insertat [-l line] [-expr 'regex'] [-c column] [-m 'msg'] [-f path] [-q]
Inserts a message or the content of a file, at a certain point in the
input. The point can be a given
line number and column, or the first line matching a given regex.
If neither I<-m> nor I<-f> is given, inserts '<HERE/>'.
This can be use to insert error messages at the lines where they apply,
but remember to do them in reverse order, since inserting them changes
following line numbers.
=head1 Options
=over
=item * B<--column> I<n> OR B<-c>
What column to insert before (default 1 (aka 0)).
=item * B<--edit> OR B<-e>
Open result in your $EDITOR.
=item * B<--file> I<name> OR B<-f>
A file to insert the contents of.
=item * B<--indent> OR B<-i>
Run the *nix C<indent> command on the output (implies I<-e>).
This is mainly designed for indenting C code, but may work ok on
other things, too.
=item * B<--lline> I<n> OR B<-l>
What line to insert at (default 1).
=item * B<--message> I<msg> OR B<-m>
A message string to insert.
=item * B<--newlines> OR B<-n>
Put newlines around the message.
=item * B<--quiet> OR B<-q>
Suppress most messages.
=item * B<--verbose> OR B<-v>
Verbose.
=item * B<--version>
Print version info and exit.
=back
=head1 To do
Perhaps support inserting multiple things in one pass, crossing them off as
it goes along.
Support inserting at all matches, not just first.
Options to insert before/replacing/after the match, like splitAt. Or just
move this functionality in there.
=head1 Related commands
C<splitAt> -- similar but inserts line-breaks.
=head1 Ownership
Copyright 2007, 2018 by Steven J. DeRose.
This work by is licensed under a Creative Commons
Attribution-Share Alike 3.0 Unported License. For further information on
this license, see L<http://creativecommons.org/licenses/by-sa/3.0/>.
For the most recent version, see L<http://www.derose.net/steve/utilities/>.
=cut
###############################################################################
# Options
#
my $doEdit = 0;
my $expr = 0;
my $indent = 0;
my $insertion = "<HERE/>";
my $newlines = 0;
my $quiet = 0;
my $targetline = 1;
my $targetcol = 1;
my $verbose = 0;
Getopt::Long::Configure ("ignore_case");
my $result = GetOptions(
"c|column=i" => \$targetcol,
"e|edit!" => \$doEdit,
"expr=s" => \$expr,
"f|file=s" => sub { $insertion = `cat $_[1]`; },
"h|help|?" => sub { system "perldoc $0"; exit; },
"i|indent!" => \$indent,
"l|line=i" => \$targetline,
"m|message=s" => \$insertion,
"n|newlines!" => \$newlines,
"q|quiet!" => \$quiet,
"v|verbose+" => \$verbose,
"version" => sub {
die "Version of $VERSION_DATE, by Steven J. DeRose.\n";
}
);
($result) || die "Bad options.\n";
if ($targetline < 1) {
warn "Bad -l (line) value '$targetline'.\n";
system "perldoc $0";
exit;
}
if ($targetcol < 1) {
warn "Bad -c (column) value '$targetcol'.\n";
system "perldoc $0";
exit;
}
if ($newlines) {
$insertion = "\n$insertion\n";
($verbose) && warn "Putting newlines around the message.\n";
}
if ($indent) { $doEdit = 1; }
my $tfile = "";
my $fh;
if ($doEdit) {
$tfile = "/tmp/insertat_" . int(rand(100000));
open $fh, ">$tfile";
}
else {
$fh = "--";
}
# warn "$targetline, $targetcol, '$insertion'.\n";
###############################################################################
#
my $didit = 0;
my $recnum = 0;
while (my $rec = <>) {
$recnum++;
if ($recnum == $targetline ||
(!$didit && $expr && $rec =~ m/$expr/)) {
vMsg(1, "Inserting at line $recnum.");
if ($targetcol < length($rec)) {
print $fh substr($rec,0,$targetcol-1)
. $insertion . substr($rec,$targetcol-1);
}
else {
print $fh $rec. $insertion;
}
$didit = 1;
}
else {
print $fh $rec;
}
}
($didit) || warn "\nNever reached insertion point, total lines = $recnum.\n";
($quiet) || warn "Inserted '$insertion'.\n";
if ($doEdit) {
if ($indent) {
my $tfile2 = "$tfile" . "_indent";
system "indent $tfile >$tfile2";
system "mv $tfile2 $tfile";
}
system "$ENV{EDITOR} $tfile &";
system "sleep 5s";
system "rm $tfile";
}