`til-kron` works
[sunny256-utils.git] / fold-stdout
blob17d7d788e45bda076b6a6d682227bcf6409c381f
1 #!/usr/bin/env perl
3 #=======================================================================
4 # fold-stdout
5 # File ID: 40153a86-f4a5-11e4-8530-000df06acc56
7 # [Description]
9 # Character set: UTF-8
10 # ©opyleft 2015– Øyvind A. Holm <sunny@sunbase.org>
11 # License: GNU General Public License version 2 or later, see end of
12 # file for legal stuff.
13 #=======================================================================
15 use strict;
16 use warnings;
17 use Digest::SHA qw(sha1_hex);
18 use Getopt::Long;
20 local $| = 1;
22 our %Opt = (
24 'help' => 0,
25 'quiet' => 0,
26 'verbose' => 0,
27 'version' => 0,
31 our $progname = $0;
32 $progname =~ s/^.*\/(.*?)$/$1/;
33 our $VERSION = '0.1.1';
35 Getopt::Long::Configure('bundling');
36 GetOptions(
38 'help|h' => \$Opt{'help'},
39 'quiet|q+' => \$Opt{'quiet'},
40 'verbose|v+' => \$Opt{'verbose'},
41 'version' => \$Opt{'version'},
43 ) || die("$progname: Option error. Use -h for help.\n");
45 $Opt{'verbose'} -= $Opt{'quiet'};
46 $Opt{'help'} && usage(0);
47 if ($Opt{'version'}) {
48 print_version();
49 exit(0);
52 exit(main());
54 sub main {
55 # {{{
56 my $Retval = 0;
58 my $FOLD_BEGIN = "\\{\\{\\{";
59 my $FOLD_END = "\\}\\}\\}";
60 my $fold_dir = "/tmp/folds";
62 my $count = 0;
63 my $fold_str = '';
64 my $level = 0;
65 my $suppress = 0;
66 my $deleted_text = '';
68 while (my $line = <>) {
69 if ($line =~ /$FOLD_BEGIN/) {
70 $level++;
71 $suppress = 1;
72 $count++;
73 chomp($fold_str = $line);
74 $fold_str =~ s/$FOLD_BEGIN//;
75 $fold_str =~ s/^\s+(.*)$/$1/;
76 $fold_str =~ s/^(.*?)\s+$/$1/;
78 if ($line =~ /$FOLD_END/) {
79 $level--;
80 $level < 0 && msg(0, "Extra end fold found, level = $level");
81 if (!$level) {
82 $deleted_text .= $line;
83 my $data_sha = sha1_hex($deleted_text);
84 -d $fold_dir || mkdir($fold_dir);
85 my $destfile = "$fold_dir/fold-$data_sha";
86 my $created_file = 0;
87 if (open(DestFP, ">$destfile")) {
88 print(DestFP $deleted_text);
89 close(DestFP) && ($created_file = 1);
90 } else {
91 warn("$progname: $destfile: Could not create file: $!\n");
93 printf("[ Removed %s, %u line%s%s ]\n",
94 length($fold_str) ? "\"$fold_str\"" : "Vim fold",
95 $count, $count == 1 ? '' : 's',
96 $created_file ? " stored in $destfile" : "",
98 $count = 0;
99 $suppress = 0;
100 $deleted_text = '';
102 } else {
103 if ($suppress) {
104 $count++;
105 $deleted_text .= $line;
106 } else {
107 print("$line");
111 if ($level > 0) {
112 msg(0, sprintf("Missing %u end fold%s, output may be truncated",
113 $level, $level == 1 ? '' : 's'));
116 return $Retval;
117 # }}}
118 } # main()
120 sub print_version {
121 # Print program version {{{
122 print("$progname $VERSION\n");
123 return;
124 # }}}
125 } # print_version()
127 sub usage {
128 # Send the help message to stdout {{{
129 my $Retval = shift;
131 if ($Opt{'verbose'}) {
132 print("\n");
133 print_version();
135 print(<<"END");
137 Usage: $progname [options] [file [files [...]]]
139 Options:
141 -h, --help
142 Show this help.
143 -q, --quiet
144 Be more quiet. Can be repeated to increase silence.
145 -v, --verbose
146 Increase level of verbosity. Can be repeated.
147 --version
148 Print version information.
151 exit($Retval);
152 # }}}
153 } # usage()
155 sub msg {
156 # Print a status message to stderr based on verbosity level {{{
157 my ($verbose_level, $Txt) = @_;
159 if ($Opt{'verbose'} >= $verbose_level) {
160 print(STDERR "$progname: $Txt\n");
162 return;
163 # }}}
164 } # msg()
166 __END__
168 # This program is free software; you can redistribute it and/or modify
169 # it under the terms of the GNU General Public License as published by
170 # the Free Software Foundation; either version 2 of the License, or (at
171 # your option) any later version.
173 # This program is distributed in the hope that it will be useful, but
174 # WITHOUT ANY WARRANTY; without even the implied warranty of
175 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
176 # See the GNU General Public License for more details.
178 # You should have received a copy of the GNU General Public License
179 # along with this program.
180 # If not, see L<http://www.gnu.org/licenses/>.
182 # vim: set fenc=UTF-8 ft=perl fdm=marker ts=4 sw=4 sts=4 et fo+=w :