I would like to use Term::ReadLine for two streams with separate historys. Is this possible? When I use the code below with two separate 'terminals' the history gets mixed together and when I look at the Attribs hash returned by $term _1->Attribs and $term_2->Attribs, the debugger says they use the same memory location. The add_history function called in both cases is the same Gnu XS function and it doesn't take an option for a buffer as far as I can tell.
Is this possible?
#!/usr/bin/perl -w
use strict;
use warnings;
use utf8;
binmode(STDIN, 'utf8');
binmode(STDOUT, 'utf8');
# turn off underline on prompt.
$ENV{"PERL_RL"} = "o=0";
use Term::ReadLine;
my $term_1 = Term::ReadLine->new('term 1');
$term_1->enableUTF8();
my $OUT_1 = $term_1->OUT || \*STDOUT;
my $term_2 = Term::ReadLine->new('term 2');
$term_2->enableUTF8();
my $OUT_2 = $term_2->OUT || \*STDOUT;
# my $attrs = $term_1->Attribs();
# for my $key (sort keys %{$attrs}) {
# printf("%15s : %s\n", $key, $attrs->{$key});
# }
my $i_1 = 1;
my $i_2 = 1;
while (1) {
$_ = $term_1->readline(sprintf("T 1:%2d > ", $i_1++));
$term_1->addhistory($_) if /\S/;
print $OUT_1 "\"$_\"\n";
exit() if $_ eq 'q';
$_ = $term_2->readline(sprintf("T 2:%2d > ", $i_2++));
$term_2->addhistory($_) if /\S/;
print $OUT_2 "\"$_\"\n";
exit() if $_ eq 'q';
}
I'm on Ubuntu 16.04 with perl 5, version 26.
Thanks
For the backend Term::ReadLine::Gnu
you can use clear_history()
and SetHistory()
to emulate two separate histories. For example:
my @hist1;
my @hist2;
while (1) {
$term_1->clear_history();
$term_1->SetHistory(@hist1);
$_ = $term_1->readline(sprintf("T 1:%2d > ", $i_1++));
push @hist1, $_ if /\S/;
print $OUT_1 "\"$_\"\n";
exit() if $_ eq 'q';
$term_2->clear_history();
$term_2->SetHistory(@hist2);
$_ = $term_2->readline(sprintf("T 2:%2d > ", $i_2++));
push @hist2, $_ if /\S/;
print $OUT_2 "\"$_\"\n";
exit() if $_ eq 'q';
}