| 1 | # ABSTRACT: Interface for terminal-based interaction | ||||||||||||||||||||
| 2 | |||||||||||||||||||||
| 3 | package Pinto::Chrome::Term; | ||||||||||||||||||||
| 4 | |||||||||||||||||||||
| 5 | 63 | ▶ | use Moose; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 6 | 63 | ▶ | use MooseX::StrictConstructor; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 7 | 63 | ▶ | use MooseX::Types::Moose qw(Bool ArrayRef Str); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 8 | 63 | ▶ | use MooseX::MarkAsMethods ( autoclean => 1 ); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 9 | |||||||||||||||||||||
| 10 | 63 | ▶ | use Term::ANSIColor; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 11 | 63 | ▶ | use File::Which qw(which); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 12 | |||||||||||||||||||||
| 13 | 63 | ▶ | use Pinto::Editor; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 14 | 63 | ▶ | use Pinto::Types qw(Io ANSIColorPalette); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 15 | 63 | ▶ | use Pinto::Util qw(user_palette itis throw is_interactive); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 16 | |||||||||||||||||||||
| 17 | #----------------------------------------------------------------------------- | ||||||||||||||||||||
| 18 | |||||||||||||||||||||
| 19 | 63 | our $VERSION = '0.14'; # VERSION | |||||||||||||||||||
| 20 | |||||||||||||||||||||
| 21 | #----------------------------------------------------------------------------- | ||||||||||||||||||||
| 22 | |||||||||||||||||||||
| 23 | 63 | extends qw( Pinto::Chrome ); | |||||||||||||||||||
| 24 | |||||||||||||||||||||
| 25 | #----------------------------------------------------------------------------- | ||||||||||||||||||||
| 26 | |||||||||||||||||||||
| 27 | has color => ( | ||||||||||||||||||||
| 28 | is => 'ro', | ||||||||||||||||||||
| 29 | isa => Bool, | ||||||||||||||||||||
| 30 | 226 | ▶ | default => sub { !$ENV{PINTO_NO_COLOR} }, | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 31 | 63 | ); | |||||||||||||||||||
| 32 | |||||||||||||||||||||
| 33 | has palette => ( | ||||||||||||||||||||
| 34 | is => 'ro', | ||||||||||||||||||||
| 35 | isa => ANSIColorPalette, | ||||||||||||||||||||
| 36 | 6 | ▶ | default => sub { user_palette() }, | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 37 | 63 | lazy => 1, | |||||||||||||||||||
| 38 | ); | ||||||||||||||||||||
| 39 | |||||||||||||||||||||
| 40 | 63 | has stdout => ( | |||||||||||||||||||
| 41 | is => 'ro', | ||||||||||||||||||||
| 42 | isa => Io, | ||||||||||||||||||||
| 43 | builder => '_build_stdout', | ||||||||||||||||||||
| 44 | coerce => 1, | ||||||||||||||||||||
| 45 | lazy => 1, | ||||||||||||||||||||
| 46 | ); | ||||||||||||||||||||
| 47 | |||||||||||||||||||||
| 48 | has stderr => ( | ||||||||||||||||||||
| 49 | is => 'ro', | ||||||||||||||||||||
| 50 | isa => Io, | ||||||||||||||||||||
| 51 | 0 | ▶ | default => sub { [ fileno(*STDERR), '>' ] }, | ||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||
| 52 | 63 | coerce => 1, | |||||||||||||||||||
| 53 | lazy => 1, | ||||||||||||||||||||
| 54 | ); | ||||||||||||||||||||
| 55 | |||||||||||||||||||||
| 56 | 63 | has has_made_progress => ( | |||||||||||||||||||
| 57 | is => 'rw', | ||||||||||||||||||||
| 58 | isa => Bool, | ||||||||||||||||||||
| 59 | default => 0, | ||||||||||||||||||||
| 60 | ); | ||||||||||||||||||||
| 61 | |||||||||||||||||||||
| 62 | #----------------------------------------------------------------------------- | ||||||||||||||||||||
| 63 | |||||||||||||||||||||
| 64 | 0 | ▶ | sub _build_stdout { | ||||||||||||||||||
|
Subroutine
_build_stdout: not called CC=6 SCAR=37.4
| |||||||||||||||||||||
| 65 | 0 | my ($self) = @_; | |||||||||||||||||||
| 66 | |||||||||||||||||||||
| 67 | 0 | ▶ | my $pager = $ENV{PINTO_PAGER} || $ENV{PAGER}; | ||||||||||||||||||
|
Condition0%0/3
$ENV{'PINTO_PAGER'} || $ENV{'PAGER'}
MC/DC0%0/2
$ENV{'PINTO_PAGER'} || $ENV{'PAGER'}
$ENV{'PINTO_PAGER'}
$ENV{'PAGER'}
Truth table0%0/3
A || B
A= $ENV{'PINTO_PAGER'}B= $ENV{'PAGER'}
| |||||||||||||||||||||
| 68 | 0 | my $stdout = [ fileno(*STDOUT), '>' ]; | |||||||||||||||||||
| 69 | |||||||||||||||||||||
| 70 | 0 | ▶ | return $stdout if not -t STDOUT; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 71 | 0 | ▶ | return $stdout if not $pager; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 72 | |||||||||||||||||||||
| 73 | my @pager_options = $ENV{PINTO_PAGER_OPTIONS} ? | ||||||||||||||||||||
| 74 | 0 | ▶ | ( $ENV{PINTO_PAGER_OPTIONS} ) : (); | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 75 | |||||||||||||||||||||
| 76 | 0 | ▶ | open my $pager_fh, q<|->, $pager, @pager_options | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 77 | or throw "Failed to open pipe to pager $pager: $!"; | ||||||||||||||||||||
| 78 | |||||||||||||||||||||
| 79 | |||||||||||||||||||||
| 80 | 0 | my $io = bless $pager_fh, 'IO::Handle'; # HACK! | |||||||||||||||||||
| 81 | 0 | $io->autoflush(1); | |||||||||||||||||||
| 82 | |||||||||||||||||||||
| 83 | 0 | return $io; | |||||||||||||||||||
| 84 | } | ||||||||||||||||||||
| 85 | |||||||||||||||||||||
| 86 | #------------------------------------------------------------------------------ | ||||||||||||||||||||
| 87 | |||||||||||||||||||||
| 88 | 8 | ▶ | sub show { | ||||||||||||||||||
|
Subroutine
show: called CC=4 SCAR=13.9
Pod
undocumented
| |||||||||||||||||||||
| 89 | 8 | my ( $self, $msg, $opts ) = @_; | |||||||||||||||||||
| 90 | |||||||||||||||||||||
| 91 | 8 | ▶ | $opts ||= {}; | ||||||||||||||||||
|
Condition100%2/2
$opts ||= {}
MC/DC100%1/1
$opts ||= {}
$opts
Truth table100%2/2
A
A= $opts
| |||||||||||||||||||||
| 92 | |||||||||||||||||||||
| 93 | 8 | $msg = $self->colorize( $msg, $opts->{color} ); | |||||||||||||||||||
| 94 | |||||||||||||||||||||
| 95 | 8 | ▶ | $msg .= "\n" unless $opts->{no_newline}; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 96 | |||||||||||||||||||||
| 97 | 8 | ▶ | print { $self->stdout } $msg or croak $!; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 98 | |||||||||||||||||||||
| 99 | 8 | return $self; | |||||||||||||||||||
| 100 | } | ||||||||||||||||||||
| 101 | |||||||||||||||||||||
| 102 | #----------------------------------------------------------------------------- | ||||||||||||||||||||
| 103 | |||||||||||||||||||||
| 104 | 236 | ▶ | sub diag { | ||||||||||||||||||
|
Subroutine
diag: called CC=8 SCAR=21.4
Pod
undocumented
| |||||||||||||||||||||
| 105 | 236 | my ( $self, $msg, $opts ) = @_; | |||||||||||||||||||
| 106 | |||||||||||||||||||||
| 107 | 236 | ▶ | $opts ||= {}; | ||||||||||||||||||
|
Condition100%2/2
$opts ||= {}
MC/DC100%1/1
$opts ||= {}
$opts
Truth table100%2/2
A
A= $opts
| |||||||||||||||||||||
| 108 | |||||||||||||||||||||
| 109 | 236 | ▶ | return if $self->quiet; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 110 | |||||||||||||||||||||
| 111 | 236 | ▶ | $msg = $msg->() if ref $msg eq 'CODE'; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 112 | |||||||||||||||||||||
| 113 | 236 | ▶ | if ( itis( $msg, 'Pinto::Exception' ) ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 114 | |||||||||||||||||||||
| 115 | # Show full stack trace if we are debugging | ||||||||||||||||||||
| 116 | 74 | ▶ | $msg = $ENV{PINTO_DEBUG} ? $msg->as_string : $msg->message; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 117 | } | ||||||||||||||||||||
| 118 | |||||||||||||||||||||
| 119 | 236 | chomp $msg; | |||||||||||||||||||
| 120 | 236 | $msg = $self->colorize( $msg, $opts->{color} ); | |||||||||||||||||||
| 121 | 236 | ▶ | $msg .= "\n" unless $opts->{no_newline}; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 122 | |||||||||||||||||||||
| 123 | 236 | ▶ | print { $self->stderr } $msg or croak $!; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 124 | } | ||||||||||||||||||||
| 125 | |||||||||||||||||||||
| 126 | #----------------------------------------------------------------------------- | ||||||||||||||||||||
| 127 | |||||||||||||||||||||
| 128 | 0 | ▶ | sub show_progress { | ||||||||||||||||||
|
Subroutine
show_progress: not called CC=3 SCAR=24.8
Pod
undocumented
| |||||||||||||||||||||
| 129 | 0 | my ($self) = @_; | |||||||||||||||||||
| 130 | |||||||||||||||||||||
| 131 | 0 | ▶ | return if not $self->should_render_progress; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 132 | |||||||||||||||||||||
| 133 | 0 | $self->stderr->autoflush; # Make sure pipes are hot | |||||||||||||||||||
| 134 | |||||||||||||||||||||
| 135 | 0 | ▶ | print { $self->stderr } '.' or croak $!; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 136 | |||||||||||||||||||||
| 137 | 0 | $self->has_made_progress(1); | |||||||||||||||||||
| 138 | } | ||||||||||||||||||||
| 139 | |||||||||||||||||||||
| 140 | #----------------------------------------------------------------------------- | ||||||||||||||||||||
| 141 | |||||||||||||||||||||
| 142 | 59 | ▶ | sub progress_done { | ||||||||||||||||||
|
Subroutine
progress_done: called CC=4 SCAR=23.2
Pod
undocumented
| |||||||||||||||||||||
| 143 | 59 | my ($self) = @_; | |||||||||||||||||||
| 144 | |||||||||||||||||||||
| 145 | 59 | ▶ | return unless $self->has_made_progress; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 146 | 0 | ▶ | return unless $self->should_render_progress; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 147 | |||||||||||||||||||||
| 148 | 0 | ▶ | print { $self->stderr } "\n" or croak $!; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 149 | } | ||||||||||||||||||||
| 150 | |||||||||||||||||||||
| 151 | #----------------------------------------------------------------------------- | ||||||||||||||||||||
| 152 | |||||||||||||||||||||
| 153 | 3 | ▶ | sub should_render_progress { | ||||||||||||||||||
|
Subroutine
should_render_progress: called CC=4 SCAR=14.1
Pod
undocumented
| |||||||||||||||||||||
| 154 | 3 | my ($self) = @_; | |||||||||||||||||||
| 155 | |||||||||||||||||||||
| 156 | 3 | ▶ | return 0 if $self->verbose; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 157 | 2 | ▶ | return 0 if $self->quiet; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 158 | 1 | ▶ | return 0 if not is_interactive; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 159 | 0 | return 1; | |||||||||||||||||||
| 160 | } | ||||||||||||||||||||
| 161 | |||||||||||||||||||||
| 162 | #----------------------------------------------------------------------------- | ||||||||||||||||||||
| 163 | |||||||||||||||||||||
| 164 | 0 | ▶ | sub edit { | ||||||||||||||||||
|
Subroutine
edit: not called CC=4 SCAR=30.0
Pod
undocumented
| |||||||||||||||||||||
| 165 | 0 | my ( $self, $document ) = @_; | |||||||||||||||||||
| 166 | |||||||||||||||||||||
| 167 | 0 | ▶ | local $ENV{VISUAL} = $self->find_editor | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 168 | or throw 'Unable to find an editor. Please set PINTO_EDITOR'; | ||||||||||||||||||||
| 169 | |||||||||||||||||||||
| 170 | # If this command is reading input from a pipe or file, then | ||||||||||||||||||||
| 171 | # STDIN will not be connected to a terminal. This causes vim | ||||||||||||||||||||
| 172 | # and emacs to behave oddly (or even segfault). After searching | ||||||||||||||||||||
| 173 | # the internets, this seems to a portable way to reconnect STDIN | ||||||||||||||||||||
| 174 | # to the actual terminal. I haven't actually tried it on Windows. | ||||||||||||||||||||
| 175 | # I'm not sure if/how I should be localizing STDIN here. | ||||||||||||||||||||
| 176 | |||||||||||||||||||||
| 177 | 0 | ▶ | my $term = ( $^O eq 'MSWin32' ) ? 'CON' : '/dev/tty'; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 178 | 0 | ▶ | open( STDIN, '<', $term ) or throw $!; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 179 | |||||||||||||||||||||
| 180 | 0 | return Pinto::Editor->edit( document => $document ); | |||||||||||||||||||
| 181 | } | ||||||||||||||||||||
| 182 | |||||||||||||||||||||
| 183 | #----------------------------------------------------------------------------- | ||||||||||||||||||||
| 184 | |||||||||||||||||||||
| 185 | 249 | ▶ | sub colorize { | ||||||||||||||||||
|
Subroutine
colorize: called CC=4 SCAR=13.9
Pod
undocumented
| |||||||||||||||||||||
| 186 | 249 | my ( $self, $string, $color_number ) = @_; | |||||||||||||||||||
| 187 | |||||||||||||||||||||
| 188 | 249 | ▶ | return '' if not $string; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 189 | 249 | ▶ | return $string if not defined $color_number; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 190 | 92 | ▶ | return $string if not $self->color; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 191 | |||||||||||||||||||||
| 192 | 5 | my $color = $self->get_color($color_number); | |||||||||||||||||||
| 193 | |||||||||||||||||||||
| 194 | 5 | return $color . $string . Term::ANSIColor::color('reset'); | |||||||||||||||||||
| 195 | } | ||||||||||||||||||||
| 196 | |||||||||||||||||||||
| 197 | #----------------------------------------------------------------------------- | ||||||||||||||||||||
| 198 | |||||||||||||||||||||
| 199 | 5 | ▶ | sub get_color { | ||||||||||||||||||
|
Subroutine
get_color: called CC=3 SCAR=11.3
Pod
undocumented
| |||||||||||||||||||||
| 200 | 5 | my ( $self, $color_number ) = @_; | |||||||||||||||||||
| 201 | |||||||||||||||||||||
| 202 | 5 | ▶ | return '' if not defined $color_number; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 203 | |||||||||||||||||||||
| 204 | 5 | my $color = $self->palette->[$color_number]; | |||||||||||||||||||
| 205 | |||||||||||||||||||||
| 206 | 5 | ▶ | throw "Invalid color number: $color_number" if not defined $color; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 207 | |||||||||||||||||||||
| 208 | 5 | return Term::ANSIColor::color($color); | |||||||||||||||||||
| 209 | } | ||||||||||||||||||||
| 210 | |||||||||||||||||||||
| 211 | #----------------------------------------------------------------------------- | ||||||||||||||||||||
| 212 | |||||||||||||||||||||
| 213 | 4 | ▶ | sub find_editor { | ||||||||||||||||||
|
Subroutine
find_editor: called CC=5 SCAR=16.1
Pod
undocumented
| |||||||||||||||||||||
| 214 | 4 | my ($self) = @_; | |||||||||||||||||||
| 215 | |||||||||||||||||||||
| 216 | # Try unsing environment variables first | ||||||||||||||||||||
| 217 | 4 | for my $env_var (qw(PINTO_EDITOR VISUAL EDITOR)) { | |||||||||||||||||||
| 218 | 9 | ▶ | return $ENV{$env_var} if $ENV{$env_var}; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 219 | } | ||||||||||||||||||||
| 220 | |||||||||||||||||||||
| 221 | # Then try typical editor commands | ||||||||||||||||||||
| 222 | 1 | for my $cmd (qw(nano pico vi)) { | |||||||||||||||||||
| 223 | 3 | my $found_cmd = which($cmd); | |||||||||||||||||||
| 224 | 3 | ▶ | return $found_cmd if $found_cmd; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 225 | } | ||||||||||||||||||||
| 226 | |||||||||||||||||||||
| 227 | 1 | return; | |||||||||||||||||||
| 228 | } | ||||||||||||||||||||
| 229 | |||||||||||||||||||||
| 230 | #----------------------------------------------------------------------------- | ||||||||||||||||||||
| 231 | |||||||||||||||||||||
| 232 | 63 | my %color_map = ( warning => 1, error => 2 ); | |||||||||||||||||||
| 233 | 63 | ▶ | while ( my ( $level, $color ) = each %color_map ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 234 | 89 | ▶ | around $level => sub { | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=7.0
| |||||||||||||||||||||
| 235 | 89 | my ( $orig, $self, $msg, $opts ) = @_; | |||||||||||||||||||
| 236 | 89 | ▶ | $opts ||= {}; | ||||||||||||||||||
|
Condition50%1/2
$opts ||= {}
MC/DC0%0/1
$opts ||= {}
$opts
Truth table50%1/2
A
A= $opts
| |||||||||||||||||||||
| 237 | 89 | $opts->{color} = $color; | |||||||||||||||||||
| 238 | 89 | return $self->$orig( $msg, $opts ); | |||||||||||||||||||
| 239 | 126 | }; | |||||||||||||||||||
| 240 | } | ||||||||||||||||||||
| 241 | |||||||||||||||||||||
| 242 | #----------------------------------------------------------------------------- | ||||||||||||||||||||
| 243 | |||||||||||||||||||||
| 244 | 63 | __PACKAGE__->meta->make_immutable; | |||||||||||||||||||
| 245 | |||||||||||||||||||||
| 246 | #----------------------------------------------------------------------------- | ||||||||||||||||||||
| 247 | 63 | 1; | |||||||||||||||||||
| 248 | |||||||||||||||||||||
| 249 | __END__ | ||||||||||||||||||||