[英]Perl Term::Readline for multiple terminal history
我想將Term :: ReadLine用於具有獨立歷史記錄的兩個流。 這可能嗎? 當我將下面的代碼與兩個單獨的“終端”一起使用時,歷史混合在一起,當我查看$ 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';
}
我使用的是Perl 5,版本26的Ubuntu 16.04。
謝謝
對於后端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';
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.