aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJoel Martin <github@martintribe.org>2014-04-23 22:22:42 -0500
committerJoel Martin <github@martintribe.org>2014-04-23 22:22:42 -0500
commit37b97ee7a874b4b881733477e4633914f41d1106 (patch)
treea1da09412472a0b309e83ea8daa8c7a4ab15f273
parente18f93f50711d475c407243c94d26daae5e814cc (diff)
downloadmal-37b97ee7a874b4b881733477e4633914f41d1106.tar.gz
mal-37b97ee7a874b4b881733477e4633914f41d1106.zip
Perl: line edit history.
-rw-r--r--perl/readline.pm25
1 files 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;