| 1 | package Shell::Amazon::S3::Plugin::ReadLineHistory; | ||
| 2 | |||
| 3 | use Moose::Role; | ||
| 4 | use File::Slurp; | ||
| 5 | use Path::Class; | ||
| 6 | use File::HomeDir; | ||
| 7 | |||
| 8 | around 'run' => sub { | ||
| 9 | my $orig = shift; | ||
| 10 | my ( $self, @args ) = @_; | ||
| 11 | $self->read_history; | ||
| 12 | $self->$orig(@args); | ||
| 13 | $self->write_history; | ||
| 14 | }; | ||
| 15 | |||
| 16 | sub _history_file { | ||
| 17 | return file( File::HomeDir->my_home, '.psh3ll_history' )->stringify; | ||
| 18 | } | ||
| 19 | |||
| 20 | sub read_history { | ||
| 21 | my $self = shift; | ||
| 22 | my $term = $self->term; | ||
| 23 | my $h = _history_file; | ||
| 24 | |||
| 25 | if ( $term->Features->{readHistory} ) { | ||
| 26 | $term->ReadHistory($h); | ||
| 27 | } | ||
| 28 | elsif ( $term->Features->{setHistory} ) { | ||
| 29 | if ( -e $h ) { | ||
| 30 | my @h = File::Slurp::read_file($h); | ||
| 31 | chomp @h; | ||
| 32 | $term->SetHistory(@h); | ||
| 33 | } | ||
| 34 | } | ||
| 35 | else { | ||
| 36 | |||
| 37 | # warn "Your ReadLine doesn't support setHistory\n"; | ||
| 38 | } | ||
| 39 | |||
| 40 | } | ||
| 41 | |||
| 42 | sub write_history { | ||
| 43 | my $self = shift; | ||
| 44 | my $term = $self->term; | ||
| 45 | my $h = _history_file; | ||
| 46 | |||
| 47 | if ( $term->Features->{writeHistory} ) { | ||
| 48 | $term->WriteHistory($h); | ||
| 49 | } | ||
| 50 | elsif ( $term->Features->{getHistory} ) { | ||
| 51 | my @h = map {"$_\n"} $term->GetHistory; | ||
| 52 | File::Slurp::write_file( $h, @h ); | ||
| 53 | } | ||
| 54 | else { | ||
| 55 | |||
| 56 | # warn "Your ReadLine doesn't support getHistory\n"; | ||
| 57 | } | ||
| 58 | } | ||
| 59 | |||
| 60 | 1; |