如何Term::ReadLine在用戶按下時不列印換行符來讀取用戶輸入Enter?
我想這樣做的原因是因為我想從螢屏底部的提示中讀取用戶輸入(如lessor vim)。目前,按下Enter會導致螢屏向下滾動,這可能是一個問題。另外,我想避免ncurses在這一點上上訴。
設定
$term->Attribs->{echo_control_characters}為0、undef或off似乎不起作用。
#!perl
use Term::ReadLine;
use Term::ReadKey;
my $term = new Term::ReadLine ('me');
$term->ornaments(0);
$term->Attribs->{echo_control_characters} = 0;
print STDERR "\e[2J\e[s\e[" . ( ( GetTerminalSize() ) [1] ) . ";1H"; # clear screen, save cursor position, and go to the bottom;
my $input = $term->readline('> ');
print STDOUT "\e[uinput = $input\n"; # restore cursor position, and print
我可以使用 as 讀取模式來做到這Term::ReadKey一點cbreak:
#!perl
use Term::ReadKey;
ReadMode 'cbreak';
my ( $k, $input );
print STDERR "> ";
while ( defined ( $k = ReadKey 0 ) and $k !~ /\n/ )
{
$input .= $k;
print STDERR $k;
}
print STDOUT "input = $input\n";
ReadMode 'restore';
但是那樣我就無法使用Term::ReadLine歷史記錄、補全和行編輯等功能。
uj5u.com熱心網友回復:
您可以將 設定為rl_getc_function在列印之前攔截回車,如本問題所示。以下對我有用:
use strict;
use warnings;
BEGIN {
$ENV{PERL_RL} = "Gnu";
}
use Term::ReadLine;
use Term::ReadKey;
my $term = Term::ReadLine->new('me');
my $attr = $term->Attribs;
$term->ornaments(0);
$attr->{getc_function} = sub {
my $ord = $term->getc($attr->{instream});
if ( $ord == 13 ) { # carriage return pressed
$attr->{done} = 1;
return 0;
}
return $ord;
};
print STDERR "\e[2J\e[s\e[" . ( ( GetTerminalSize() ) [1] ) . ";1H";
my $input = $term->readline('> ');
print STDOUT "\e[uinput = $input\n"; # restore cursor position, and print
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/429487.html
