| 1 | package IO::EditPrompt; | ||||||||||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||||||||||
| 3 | 7 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||
| 4 | 7 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||||||||||||
| 6 | 7 | ▶ | use File::Temp (); | ||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||
| 7 | 7 | ▶ | use IO::Prompter (); | ||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||||||||||||
| 9 | 7 | our $VERSION = '0.02'; | |||||||||||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||||||||||||
| 11 | 13 | ▶ | sub new { | ||||||||||||||||||||||||||||||||||||
|
Subroutine
new: called CC=5 SCAR=16.1
Pod
documented
| |||||||||||||||||||||||||||||||||||||||
| 12 | 13 | my ($class, $opts) = (@_); | |||||||||||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||||||||||||
| 14 | 13 | ▶ | $opts ||= {}; | ||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$opts ||= {}
MC/DC100%1/1
$opts ||= {}
$opts
Truth table100%2/2
A
A= $opts
| |||||||||||||||||||||||||||||||||||||||
| 15 | 13 | ▶ | die "parameter is not a hashref.\n" unless ref $opts eq ref {}; | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 16 | 11 | ▶ | die "'$opts->{tmpdir}' is not a directory.\n" if defined $opts->{tmpdir} and !-d $opts->{tmpdir}; | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
defined $opts->{"tmpdir"} and not -d $opts->{"tmpdir"}
MC/DC100%2/2
defined $opts->{"tmpdir"} and not -d $opts->{"tmpdir"}
defined $opts->{"tmpdir"}
not -d $opts->{"tmpdir"}
Truth table100%3/3
A and not(B)
A= defined $opts->{"tmpdir"}B= not -d $opts->{"tmpdir"}
| |||||||||||||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||||||||||||
| 18 | my $self = bless { | ||||||||||||||||||||||||||||||||||||||
| 19 | dir => $opts->{tmpdir}, | ||||||||||||||||||||||||||||||||||||||
| 20 | editor => $opts->{editor}, | ||||||||||||||||||||||||||||||||||||||
| 21 | 10 | editor_args => [], | |||||||||||||||||||||||||||||||||||||
| 22 | }, $class; | ||||||||||||||||||||||||||||||||||||||
| 23 | 10 | $self->_normalize_editor( $opts->{default_editor} ); | |||||||||||||||||||||||||||||||||||||
| 24 | 10 | return $self; | |||||||||||||||||||||||||||||||||||||
| 25 | } | ||||||||||||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||||||||||||
| 27 | 2 | ▶ | sub prompt { | ||||||||||||||||||||||||||||||||||||
|
Subroutine
prompt: called CC=3 SCAR=11.1
Pod
documented
| |||||||||||||||||||||||||||||||||||||||
| 28 | 2 | my ($self, $prompt, $deftext) = @_; | |||||||||||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||||||||||||
| 30 | 2 | my $output = ''; | |||||||||||||||||||||||||||||||||||||
| 31 | 2 | my $fmt_prompt = _format_prompt( $prompt ); | |||||||||||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||||||||||||
| 33 | 2 | ▶ | do { | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
0 == length $output and IO::Prompter::prompt("Content is empty, retry?", "-y")
MC/DC0%0/2
0 == length $output and IO::Prompter::prompt("Content is empty, retry?", "-y")
0 == length $output
IO::Prompter::prompt("Content is empty, retry?", "-y")
Truth table66%2/3
A and B
A= 0 == length $outputB= IO::Prompter::prompt("Content is empty, retry?", "-y")
| |||||||||||||||||||||||||||||||||||||||
| 34 | 2 | my ($tmp, $filename) = $self->_create_tmp_file( $fmt_prompt, $deftext ); | |||||||||||||||||||||||||||||||||||||
| 35 | 2 | $self->_run_editor( $filename ); | |||||||||||||||||||||||||||||||||||||
| 36 | 2 | $output = $self->_get_output( $filename, $fmt_prompt ); | |||||||||||||||||||||||||||||||||||||
| 37 | } while( (0 == length $output) && IO::Prompter::prompt( 'Content is empty, retry?', '-y' ) ); | ||||||||||||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||||||||||||
| 39 | 2 | return $output; | |||||||||||||||||||||||||||||||||||||
| 40 | } | ||||||||||||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||||||||||||
| 42 | 18 | ▶ | sub _normalize_editor { | ||||||||||||||||||||||||||||||||||||
|
Subroutine
_normalize_editor: called CC=5 SCAR=16.1
| |||||||||||||||||||||||||||||||||||||||
| 43 | 18 | my ($self, $def_editor) = @_; | |||||||||||||||||||||||||||||||||||||
| 44 | 18 | ▶ | $self->{editor} ||= $ENV{EDITOR} || $def_editor || 'vim'; | ||||||||||||||||||||||||||||||||||||
|
Condition87%7/8
$self->{"editor"} ||= $ENV{"EDITOR"} || $def_editor || "vim"
$ENV{'EDITOR'} || $def_editor || 'vim'
$ENV{"EDITOR"} or $def_editor
MC/DC0%0/3
$self->{"editor"} ||= $ENV{"EDITOR"} || $def_editor || "vim"
$self->{"editor"}
$ENV{"EDITOR"}
$def_editor
Truth table0%0/4
A ||= B or C
A= $self->{"editor"}B= $ENV{"EDITOR"}C= $def_editor
| |||||||||||||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||||||||||||
| 46 | # Turn off saving state on vim | ||||||||||||||||||||||||||||||||||||||
| 47 | 18 | ▶ | $self->{editor_args} = [ '-i', 'NONE' ] if $self->{editor} =~ /\bvim$/; | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 48 | 18 | return; | |||||||||||||||||||||||||||||||||||||
| 49 | } | ||||||||||||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||||||||||||
| 51 | 7 | ▶ | sub _format_prompt { | ||||||||||||||||||||||||||||||||||||
|
Subroutine
_format_prompt: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||
| 52 | 7 | my ($prompt) = @_; | |||||||||||||||||||||||||||||||||||||
| 53 | 7 | ▶ | return '' unless defined $prompt; | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 54 | 6 | return join( q{}, map { "# $_\n" } split /\n/, $prompt ); | |||||||||||||||||||||||||||||||||||||
| 55 | } | ||||||||||||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||||||||||||
| 57 | 4 | ▶ | sub _create_tmp_file { | ||||||||||||||||||||||||||||||||||||
|
Subroutine
_create_tmp_file: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||||||||||||
| 58 | 4 | my ($self, @texts) = @_; | |||||||||||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||||||||||||
| 60 | 4 | ▶ | my $tmp = File::Temp->new( UNLINK => 1, EXLOCK => 1, ($self->{dir} ? (DIR => $self->{dir}) : ()) ); | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 61 | 4 | my $filename = $tmp->filename; | |||||||||||||||||||||||||||||||||||||
| 62 | 4 | print {$tmp} grep { defined $_ } @texts; | |||||||||||||||||||||||||||||||||||||
| 63 | 4 | ▶ | close( $tmp ) or die "Unable to write '$filename': $!\n"; | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||||||||||||
| 65 | 4 | return ($tmp, $filename); | |||||||||||||||||||||||||||||||||||||
| 66 | } | ||||||||||||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||||||||||||
| 68 | 5 | ▶ | sub _read_file { | ||||||||||||||||||||||||||||||||||||
|
Subroutine
_read_file: called CC=2 SCAR=7.0
| |||||||||||||||||||||||||||||||||||||||
| 69 | 5 | my ($self, $filename) = @_; | |||||||||||||||||||||||||||||||||||||
| 70 | 5 | ▶ | open my $fh, '<', $filename or die "Unable to re-read '$filename': $!\n"; | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 71 | 5 | local $/; | |||||||||||||||||||||||||||||||||||||
| 72 | 5 | return scalar <$fh>; | |||||||||||||||||||||||||||||||||||||
| 73 | } | ||||||||||||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||||||||||||
| 75 | 8 | ▶ | sub _get_output { | ||||||||||||||||||||||||||||||||||||
|
Subroutine
_get_output: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||
| 76 | 8 | my ($self, $filename, $prompt) = @_; | |||||||||||||||||||||||||||||||||||||
| 77 | 8 | ▶ | return '' if -s $filename eq length $prompt; | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 78 | 5 | my $output = $self->_read_file( $filename ); | |||||||||||||||||||||||||||||||||||||
| 79 | 5 | $output =~ s/^#[^\n]*(?:\n|\Z)//smg; | |||||||||||||||||||||||||||||||||||||
| 80 | 5 | return $output; | |||||||||||||||||||||||||||||||||||||
| 81 | } | ||||||||||||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||||||||||||
| 83 | 0 | ▶ | sub _run_editor { | ||||||||||||||||||||||||||||||||||||
|
Subroutine
_run_editor: not called CC=5 SCAR=34.0
| |||||||||||||||||||||||||||||||||||||||
| 84 | 0 | my ($self, $file) = @_; | |||||||||||||||||||||||||||||||||||||
| 85 | 0 | my $err = system $self->{editor}, @{$self->{editor_args}}, $file; | |||||||||||||||||||||||||||||||||||||
| 86 | 0 | ▶ | return unless $err; | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 87 | 0 | ▶ | if ($? == -1) { | ||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||
| 88 | 0 | die "failed to execute '$self->{editor}': $!\n"; | |||||||||||||||||||||||||||||||||||||
| 89 | } | ||||||||||||||||||||||||||||||||||||||
| 90 | elsif ($? & 127) { | ||||||||||||||||||||||||||||||||||||||
| 91 | 0 | ▶ | die sprintf "'$self->{editor}' died with signal %d, %s coredump\n", | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 92 | ($? & 127), ($? & 128) ? 'with' : 'without'; | ||||||||||||||||||||||||||||||||||||||
| 93 | } | ||||||||||||||||||||||||||||||||||||||
| 94 | else { | ||||||||||||||||||||||||||||||||||||||
| 95 | 0 | die sprintf "'$self->{editor}' exited with value %d\n", $? >> 8; | |||||||||||||||||||||||||||||||||||||
| 96 | } | ||||||||||||||||||||||||||||||||||||||
| 97 | } | ||||||||||||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||||||||||||
| 99 | 7 | 1; | |||||||||||||||||||||||||||||||||||||
| 100 | __END__ | ||||||||||||||||||||||||||||||||||||||