繁体   English   中英

Perl Term :: Readline用于多个终端历史记录

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM