From 9cda45ef3313edb2fe67625bd746292c4e4889fd Mon Sep 17 00:00:00 2001 From: Mario Domgoergen Date: Wed, 9 Mar 2016 08:48:47 +0100 Subject: [PATCH] Add color support for display_format pretty --- META.json | 1 + Makefile.PL | 2 ++ dist.ini | 1 + lib/App/txtnix.pm | 20 +++++++++++++++++++- 4 files changed, 23 insertions(+), 1 deletion(-) diff --git a/META.json b/META.json index 337e0d9..ed87d3f 100644 --- a/META.json +++ b/META.json @@ -49,6 +49,7 @@ "POSIX" : "0", "Path::Tiny" : "0", "String::ShellQuote" : "0", + "Term::ANSIColor" : "1.04", "Time::Local" : "1.2100", "perl" : "v5.14.0", "strict" : "0", diff --git a/Makefile.PL b/Makefile.PL index 65a43aa..5686002 100644 --- a/Makefile.PL +++ b/Makefile.PL @@ -37,6 +37,7 @@ my %WriteMakefileArgs = ( "POSIX" => 0, "Path::Tiny" => 0, "String::ShellQuote" => 0, + "Term::ANSIColor" => "1.04", "Time::Local" => "1.2100", "strict" => 0, "warnings" => 0 @@ -77,6 +78,7 @@ my %FallbackPrereqs = ( "POSIX" => 0, "Path::Tiny" => 0, "String::ShellQuote" => 0, + "Term::ANSIColor" => "1.04", "Test::More" => 0, "Test::Output" => 0, "Time::Local" => "1.2100", diff --git a/dist.ini b/dist.ini index 1806a19..3e86838 100644 --- a/dist.ini +++ b/dist.ini @@ -85,3 +85,4 @@ allow_dirty = lib/App/txtnix.pm [ Prereqs ] Mojolicious = 6.54 Time::Local = 1.2100 +Term::ANSIColor = 1.04 diff --git a/lib/App/txtnix.pm b/lib/App/txtnix.pm index a8bf09e..b90cb03 100644 --- a/lib/App/txtnix.pm +++ b/lib/App/txtnix.pm @@ -41,6 +41,14 @@ has until => sub { Mojo::Date->new() }; has ca_file => sub { '/etc/ssl/certs/ca-certificates.crt' }; has show_new => sub { 0 }; has last_timeline => sub { 0 }; +has use_colors => sub { 0 }; +has colors => sub { + { + nick => 'red', + time => 'green', + mention => 'white on_magenta', + }; +}; has [ qw( twturl pre_tweet_hook post_tweet_hook config force registry key_file cert_file ) @@ -87,6 +95,9 @@ sub new { if ( $config->{following} ) { $args->{following} = $config->{following}; } + if ( $config->{colors} ) { + $args->{colors} = $config->{colors}; + } } for my $path (qw(twtfile cache_dir)) { $args->{$path} = path( $args->{$path} ) if exists $args->{$path}; @@ -389,12 +400,17 @@ sub display_tweets { Mojo::Template->new( vars => 1, encoding => 'UTF-8' )->parse($template); for my $tweet (@tweets) { + if ( !$self->use_colors ) { + $ENV{ANSI_COLORS_DISABLED} = 0; + } + print {$fh} b( $mt->process( { nick => $tweet->source->nick, content => $self->collapse_mentions( $tweet->text || '' ), time => $tweet->strftime( $self->time_format ), + color => $self->colors, } ) )->encode; @@ -462,7 +478,9 @@ sub expand_mention { __DATA__ @@ pretty.txt -* <%= $nick %> (<%= $time %>): +% use Term::ANSIColor; +* <%= colored($nick, $color->{nick}) %> (<%= colored($time, $color->{time}) %>): +% $content =~ s/(@\w+)/colored($1, $color->{mention})/ge; %= $content . "\n" @@ simple.txt