From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on dcvr.yhbt.net X-Spam-Level: X-Spam-ASN: X-Spam-Status: No, score=-4.0 required=3.0 tests=ALL_TRUSTED,BAYES_00 shortcircuit=no autolearn=ham autolearn_force=no version=3.4.2 Received: from localhost (dcvr.yhbt.net [127.0.0.1]) by dcvr.yhbt.net (Postfix) with ESMTP id 2C0E41FD5B for ; Wed, 28 Apr 2021 07:52:06 +0000 (UTC) From: Eric Wong To: meta@public-inbox.org Subject: [PATCH 10/11] lei_view_text: translate background colors from git Date: Wed, 28 Apr 2021 07:52:04 +0000 Message-Id: <20210428075205.19440-11-e@80x24.org> In-Reply-To: <20210428075205.19440-1-e@80x24.org> References: <20210428075205.19440-1-e@80x24.org> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit List-Id: This seems to work with or without attributes. We'll deal with 256-color terminal colors when/if somebody cares for it, but the usual 16 ought to be more than enough. --- lib/PublicInbox/LeiViewText.pm | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/lib/PublicInbox/LeiViewText.pm b/lib/PublicInbox/LeiViewText.pm index 5d649840..e0d62c0d 100644 --- a/lib/PublicInbox/LeiViewText.pm +++ b/lib/PublicInbox/LeiViewText.pm @@ -36,8 +36,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 +48,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; } }