| 1 | use v5.20; | ||
| 2 | use strict; | ||
| 3 | use warnings; | ||
| 4 | use experimental qw( signatures lexical_subs postderef ); | ||
| 5 | |||
| 6 | package App::Chit::Command::show; | ||
| 7 | |||
| 8 | use App::Chit -command; | ||
| 9 | |||
| 10 | use App::Chit::Util (); | ||
| 11 | use Term::ANSIColor qw( colored ); | ||
| 12 | |||
| 13 | our $AUTHORITY = 'cpan:TOBYINK'; | ||
| 14 | our $VERSION = '0.001001'; | ||
| 15 | |||
| 16 | sub abstract { | ||
| 17 | return "Show recent chat history." | ||
| 18 | } | ||
| 19 | |||
| 20 | sub opt_spec { | ||
| 21 | return ( | ||
| 22 | [ "msg|n=i", "number of messages (default 20)" ], | ||
| 23 | [ "colour|c", "force colour" ], | ||
| 24 | [ "nocolour", "avoid colour" ], | ||
| 25 | ); | ||
| 26 | } | ||
| 27 | |||
| 28 | sub validate_args ( $self, $opt, $args ) { | ||
| 29 | $opt->{msg} //= 20; | ||
| 30 | $self->usage_error( "expected a positive number" ) | ||
| 31 | unless $opt->{msg} > 0; | ||
| 32 | |||
| 33 | $opt->{colour} //= ( -t STDOUT ); | ||
| 34 | delete $opt->{colour} if $opt->{nocolour} || $ENV{NO_COLOR}; | ||
| 35 | } | ||
| 36 | |||
| 37 | sub execute ( $self, $opt, $args ) { | ||
| 38 | my $dir = App::Chit::Util::find_chit_dir() | ||
| 39 | or $self->usage_error("need to initialize chit first"); | ||
| 40 | my $chit = App::Chit::Util::load_chit( $dir ); | ||
| 41 | |||
| 42 | my @log = @{ $chit->{chat} or [] }; | ||
| 43 | shift( @log ) while @log > $opt->{msg}; | ||
| 44 | |||
| 45 | for my $x ( @log ) { | ||
| 46 | if ( $x->{role} eq 'user' ) { | ||
| 47 | if ( $opt->{colour} ) { | ||
| 48 | print colored( ['bold cyan'], $x->{content} ), "\n"; | ||
| 49 | } | ||
| 50 | else { | ||
| 51 | print "PROMPT: ", $x->{content}, "\n"; | ||
| 52 | } | ||
| 53 | } | ||
| 54 | else { | ||
| 55 | if ( $opt->{colour} ) { | ||
| 56 | print colored( ['white'], $x->{content} ), "\n"; | ||
| 57 | } | ||
| 58 | else { | ||
| 59 | print "RESPONSE: ", $x->{content}, "\n"; | ||
| 60 | } | ||
| 61 | print "\n"; | ||
| 62 | } | ||
| 63 | } | ||
| 64 | } | ||
| 65 | |||
| 66 | 1; |