diff options
| author | Joel Martin <github@martintribe.org> | 2014-04-23 22:22:42 -0500 |
|---|---|---|
| committer | Joel Martin <github@martintribe.org> | 2014-04-23 22:22:42 -0500 |
| commit | 37b97ee7a874b4b881733477e4633914f41d1106 (patch) | |
| tree | a1da09412472a0b309e83ea8daa8c7a4ab15f273 | |
| parent | e18f93f50711d475c407243c94d26daae5e814cc (diff) | |
| download | mal-37b97ee7a874b4b881733477e4633914f41d1106.tar.gz mal-37b97ee7a874b4b881733477e4633914f41d1106.zip | |
Perl: line edit history.
| -rw-r--r-- | perl/readline.pm | 25 |
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; |
