From 37b97ee7a874b4b881733477e4633914f41d1106 Mon Sep 17 00:00:00 2001 From: Joel Martin Date: Wed, 23 Apr 2014 22:22:42 -0500 Subject: Perl: line edit history. --- perl/readline.pm | 25 ++++++++++++++++++++++--- 1 file changed, 22 insertions(+), 3 deletions(-) diff --git a/perl/readline.pm b/perl/readline.pm index 5389afd..f0710b1 100644 --- a/perl/readline.pm +++ b/perl/readline.pm @@ -16,17 +16,36 @@ $_rl->ornaments(0); my $OUT = $_rl->OUT || \*STDOUT; my $_history_loaded = 0; +my $history_file = $ENV{"HOME"} . "/.mal-history"; + +sub save_line { + my ($line) = @_; + open(my $fh, '>>', $history_file) or return; + say $fh $line; + close $fh; +} + +sub load_history { + open my $fh, $history_file or return; + + while(my $line = <$fh>) { + chomp $line; + $_rl->addhistory($line) if $line =~ /\S/; + } + + close $fh; +} + sub mal_readline { my($prompt) = @_; my $line = undef; if (! $_history_loaded) { $_history_loaded = 1; - # TODO: load history + load_history(); } if (defined ($line = $_rl->readline($prompt))) { - $_rl->addhistory($line) if $line =~ /\S/; - # TODO: save history + save_line($line); return $line; } else { return undef; -- cgit v1.2.3