Perl Term::Readline 用于多个终端历史记录
Perl Term::Readline for multiple terminal history
我想对具有不同历史记录的两个流使用 Term::ReadLine。这可能吗?当我将下面的代码与两个单独的 'terminals' 一起使用时,历史记录混合在一起,当我查看 $term _1->Attribs 和 $term_2->Attribs 返回的 Attribs 散列时,调试器说它们使用相同的内存位置。在这两种情况下调用的 add_history 函数是相同的 Gnu XS 函数,据我所知,它没有选择缓冲区。
这可能吗?
#!/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';
}
我在 Ubuntu 16.04 上使用 perl 5,版本 26。
谢谢
对于后端 Term::ReadLine::Gnu
,您可以使用 clear_history()
和 SetHistory()
来模拟两个独立的历史记录。例如:
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';
}
我想对具有不同历史记录的两个流使用 Term::ReadLine。这可能吗?当我将下面的代码与两个单独的 'terminals' 一起使用时,历史记录混合在一起,当我查看 $term _1->Attribs 和 $term_2->Attribs 返回的 Attribs 散列时,调试器说它们使用相同的内存位置。在这两种情况下调用的 add_history 函数是相同的 Gnu XS 函数,据我所知,它没有选择缓冲区。
这可能吗?
#!/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';
}
我在 Ubuntu 16.04 上使用 perl 5,版本 26。
谢谢
对于后端 Term::ReadLine::Gnu
,您可以使用 clear_history()
和 SetHistory()
来模拟两个独立的历史记录。例如:
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';
}