]> Sergey Matveev's repositories - public-inbox.git/blobdiff - lib/PublicInbox/LeiViewText.pm
www_stream: sh-friendly .onion URLs wrapping
[public-inbox.git] / lib / PublicInbox / LeiViewText.pm
index 5d649840e595296a4ef6d9696d585e2d18eb7b65..340a66481225fbcc0a25867a95ff83933de06435 100644 (file)
@@ -7,7 +7,6 @@ package PublicInbox::LeiViewText;
 use strict;
 use v5.10.1;
 use PublicInbox::MsgIter qw(msg_part_text);
-use PublicInbox::ContentHash qw(git_sha);
 use PublicInbox::MID qw(references);
 use PublicInbox::View;
 use PublicInbox::Hval;
@@ -36,8 +35,11 @@ my %DEFAULT_COLOR = (
        context => undef,
 );
 
+my $COLOR = qr/(?:bright)?
+               (?:normal|black|red|green|yellow|blue|magenta|cyan|white)/x;
+
 sub my_colored {
-       my ($self, $slot) = @_; # $_[2] = buffer
+       my ($self, $slot, $buf) = @_;
        my $val = $self->{"color.$slot"} //=
                        $self->{-leicfg}->{"color.$slot"} //
                        $self->{-gitcfg}->{"color.diff.$slot"} //
@@ -45,11 +47,19 @@ sub my_colored {
                        $DEFAULT_COLOR{$slot};
        $val = $val->[-1] if ref($val) eq 'ARRAY';
        if (defined $val) {
+               $val = lc $val;
                # git doesn't use "_", Term::ANSIColor does
-               $val =~ s/\Abright([^_])/bright_$1/i;
-               ${$self->{obuf}} .= Term::ANSIColor::colored($_[2], lc $val);
+               $val =~ s/\Abright([^_])/bright_$1/ig;
+
+               # git: "green black" => T::A: "green on_black"
+               $val =~ s/($COLOR)(.+?)($COLOR)/$1$2on_$3/;
+
+               # FIXME: convert git #XXXXXX to T::A-compatible colors
+               # for 256-color terminals
+
+               ${$self->{obuf}} .= colored($buf, $val);
        } else {
-               ${$self->{obuf}} .= $_[2];
+               ${$self->{obuf}} .= $buf;
        }
 }
 
@@ -58,7 +68,7 @@ sub uncolored { ${$_[0]->{obuf}} .= $_[2] }
 sub new {
        my ($cls, $lei) = @_;
        my $self = bless { %{$lei->{opt}}, -colored => \&uncolored }, $cls;
-       return $self unless $self->{color} || -t $lei->{1};
+       return $self unless $self->{color} //= -t $lei->{1};
        my $cmd = [ qw(git config -z --includes -l) ];
        my ($r, $pid) = popen_rd($cmd, undef, { 2 => $lei->{2} });
        my $cfg = PublicInbox::Config::config_fh_parse($r, "\0", "\n");