1 # Copyright (C) 2021 all contributors <meta@public-inbox.org>
2 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
4 # PublicInbox::Eml to (optionally colorized) text coverter for terminals
5 # the non-HTML counterpart to PublicInbox::View
6 package PublicInbox::LeiViewText;
9 use PublicInbox::MsgIter qw(msg_part_text);
10 use PublicInbox::MID qw(references);
11 use PublicInbox::View;
12 use PublicInbox::Hval;
13 use PublicInbox::ViewDiff;
14 use PublicInbox::Spawn qw(popen_rd);
18 # xhtml_map works since we don't search for HTML ([&<>'"])
19 $_[0] =~ s/([\x7f\x00-\x1f])/$PublicInbox::Hval::xhtml_map{$1}/sge;
23 # mutt names, loaded from ~/.config/lei/config
26 status => 'bright_cyan', # smsg stuff
27 attachment => 'bright_red',
29 # git names and defaults, falls back to ~/.gitconfig
38 my $COLOR = qr/(?:bright)?
39 (?:normal|black|red|green|yellow|blue|magenta|cyan|white)/x;
42 my ($self, $slot, $buf) = @_;
43 my $val = $self->{"color.$slot"} //=
44 $self->{-leicfg}->{"color.$slot"} //
45 $self->{-gitcfg}->{"color.diff.$slot"} //
46 $self->{-gitcfg}->{"diff.color.$slot"} //
47 $DEFAULT_COLOR{$slot};
48 $val = $val->[-1] if ref($val) eq 'ARRAY';
51 # git doesn't use "_", Term::ANSIColor does
52 $val =~ s/\Abright([^_])/bright_$1/ig;
54 # git: "green black" => T::A: "green on_black"
55 $val =~ s/($COLOR)(.+?)($COLOR)/$1$2on_$3/;
57 # FIXME: convert git #XXXXXX to T::A-compatible colors
58 # for 256-color terminals
60 ${$self->{obuf}} .= colored($buf, $val);
62 ${$self->{obuf}} .= $buf;
66 sub uncolored { ${$_[0]->{obuf}} .= $_[2] }
70 my $self = bless { %{$lei->{opt}}, -colored => \&uncolored }, $cls;
71 return $self unless $self->{color} //= -t $lei->{1};
72 my $cmd = [ qw(git config -z --includes -l) ];
73 my ($r, $pid) = popen_rd($cmd, undef, { 2 => $lei->{2} });
74 my $cfg = PublicInbox::Config::config_fh_parse($r, "\0", "\n");
77 $lei->err("# git-config failed, no color (non-fatal)");
80 $self->{-colored} = \&my_colored;
81 $self->{-gitcfg} = $cfg;
82 $self->{-leicfg} = $lei->{cfg};
87 my ($self, $eml) = @_;
89 for my $f (qw(From To Cc)) {
90 for my $v ($eml->header($f)) {
92 PublicInbox::View::fold_addresses($v);
97 for my $f (qw(Subject Date Newsgroups Message-ID X-Message-ID)) {
98 for my $v ($eml->header($f)) {
103 if (my @irt = $eml->header_raw('In-Reply-To')) {
106 $hbuf .= "In-Reply-To: $v\n";
109 my $refs = references($eml);
110 if (defined(my $irt = pop @$refs)) {
112 $hbuf .= "In-Reply-To: <$irt>\n";
115 my $max = $self->{-max_cols};
116 $hbuf .= 'References: ' .
117 join("\n\t", map { '<'._xs($_).'>' } @$refs) .
121 $self->{-colored}->($self, 'hdrdefault', $hbuf .= "\n");
124 sub attach_note ($$$$;$) {
125 my ($self, $ct, $p, $fn, $err) = @_;
126 my ($part, $depth, $idx) = @$p;
127 my $nl = $idx eq '1' ? '' : "\n"; # like join("\n", ...)
128 my $abuf = $err ? <<EOF : '';
129 [-- Warning: decoded text below may be mangled, UTF-8 assumed --]
131 $abuf .= "[-- Attachment #$idx: ";
133 my $size = length($part->body);
134 my $ts = "Type: $ct, Size: $size bytes";
135 my $d = $part->header('Content-Description') // $fn // '';
137 $abuf .= $d eq '' ? "$ts --]\n" : "$d --]\n[-- $ts --]\n";
138 if (my $blob = $self->{-smsg}->{blob}) {
139 $abuf .= "[-- lei blob $blob:$idx --]\n";
141 $self->{-colored}->($self, 'attachment', $abuf);
142 hdr_buf($self, $part) if $part->{is_submsg};
145 sub flush_text_diff ($$) {
146 my ($self, $cur) = @_;
147 my @top = split($PublicInbox::ViewDiff::EXTRACT_DIFFS, $$cur);
148 undef $$cur; # free memory
150 my $obuf = $self->{obuf};
151 my $colored = $self->{-colored};
152 while (defined(my $x = shift @top)) {
153 if (scalar(@top) >= 4 &&
154 $top[1] =~ $PublicInbox::ViewDiff::IS_OID &&
155 $top[0] =~ $PublicInbox::ViewDiff::IS_OID) {
158 $colored->($self, 'meta', $x);
160 # Quiet "Complex regular subexpression recursion limit"
161 # warning. Perl will truncate matches upon hitting
162 # that limit, giving us more (and shorter) scalars than
163 # would be ideal, but otherwise it's harmless.
165 # We could replace the `+' metacharacter with `{1,100}'
166 # to limit the matches ourselves to 100, but we can
167 # let Perl do it for us, quietly.
168 no warnings 'regexp';
170 for my $s (split(/((?:(?:^\+[^\n]*\n)+)|
172 (?:^@@ [^\n]+\n))/xsm, $x)) {
173 if (!defined($dctx)) {
174 ${$self->{obuf}} .= $s;
175 } elsif ($s =~ s/\A(@@ \S+ \S+ @@\s*)//) {
176 $colored->($self, 'frag', $1);
177 $colored->($self, 'func', $s);
178 } elsif ($s =~ /\A\+/) {
179 $colored->($self, 'new', $s);
180 } elsif ($s =~ /\A-- $/sm) { # email sig starts
182 ${$self->{obuf}} .= $s;
183 } elsif ($s =~ /\A-/) {
184 $colored->($self, 'old', $s);
186 $colored->($self, 'context', $s);
190 ${$self->{obuf}} .= $x;
195 sub add_text_buf { # callback for Eml->each_part
197 my ($part, $depth, $idx) = @$p;
198 my $ct = $part->content_type || 'text/plain';
199 my $fn = $part->filename;
200 my ($s, $err) = msg_part_text($part, $ct);
201 return attach_note($self, $ct, $p, $fn) unless defined $s;
202 hdr_buf($self, $part) if $part->{is_submsg};
205 $s .= "\n" unless substr($s, -1, 1) eq "\n";
206 my $diff = ($s =~ /^--- [^\n]+\n\+{3} [^\n]+\n@@ /ms);
207 my @sections = PublicInbox::MsgIter::split_quotes($s);
208 undef $s; # free memory
209 if (defined($fn) || ($depth > 0 && !$part->{is_submsg}) || $err) {
210 # badly-encoded message with $err? tell the world about it!
211 attach_note($self, $ct, $p, $fn, $err);
212 ${$self->{obuf}} .= "\n";
214 my $colored = $self->{-colored};
215 for my $cur (@sections) {
217 $colored->($self, 'quoted', $cur);
219 flush_text_diff($self, \$cur);
221 ${$self->{obuf}} .= $cur;
223 undef $cur; # free memory
227 # returns an arrayref suitable for $lei->out or print
229 my ($self, $smsg, $eml) = @_;
230 local $Term::ANSIColor::EACHLINE = "\n";
231 $self->{obuf} = \(my $obuf = '');
232 $self->{-smsg} = $smsg;
233 $self->{-max_cols} = ($self->{columns} //= 80) - 8; # for header wrap
235 for my $f (qw(blob pct)) {
236 push @h, "$f:$smsg->{$f}" if defined $smsg->{$f};
238 @h = ("# @h\n") if @h;
239 for my $f (qw(kw L)) {
240 my $v = $smsg->{$f} or next;
241 push @h, "# $f:".join(',', @$v)."\n" if @$v;
243 $self->{-colored}->($self, 'status', join('', @h));
244 hdr_buf($self, $eml);
245 $eml->each_part(\&add_text_buf, $self, 1);
246 delete $self->{obuf};