summaryrefslogtreecommitdiffstats
path: root/lib/Reply/Plugin/ReadLine.pm
blob: 29ad5a014db4475e8f6ff3a874505cc8dd897bd8 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
package Reply::Plugin::ReadLine;
use strict;
use warnings;

use base 'Reply::Plugin';

use File::HomeDir;
use File::Spec;
use Term::ReadLine;

my $history = File::Spec->catfile(File::HomeDir->my_data, '.reply_history');

sub new {
    my $class = shift;

    my $self = $class->SUPER::new(@_);
    $self->{term} = Term::ReadLine->new('Reply');

    if (open my $fh, '<', $history) {
        for my $line (<$fh>) {
            chomp $line;
            $self->{term}->addhistory($line);
        }
    }
    else {
        my $e = $!;
        warn "Couldn't open $history for reading: $e"
            if -e $history;
    }

    return $self;
}

sub read_line {
    my $self = shift;
    my ($next, $prompt) = @_;

    return $self->{term}->readline($prompt);
}

sub DESTROY {
    my $self = shift;

    # XXX support more later
    return unless $self->{term}->ReadLine eq 'Term::ReadLine::Gnu';

    $self->{term}->WriteHistory($history)
        or warn "Couldn't write history to $history";
}

1;