Thread Projekt Term::ReadLine::Tiny (23 answers)
Opened by Kuerbis at 2014-07-13 16:40

Kuerbis
 2014-07-15 19:23
#176452 #176452
User since
2011-03-20
947 Artikel
BenutzerIn
[default_avatar]
Hallo,

kann mir jemand sagen, ob hier die encode und die decode korrekt sind?
Ist es in Ordnung, die Rückgabe von InputChar() immer zu dekodieren?

Das ist ein gekürzte Version:

Code (perl): (dl )
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# ...
use Encode::Locale;

sub __init_term {
    my ( $self ) = @_;
    # Windows:
    $self->{input} = Win32::Console->new( STD_INPUT_HANDLE );
    $self->{old_in_mode} = $self->{input}->Mode();
    $self->{input}->Mode( ENABLE_PROCESSED_INPUT );

    # Linux:
    #ReadMode( 'cbreak' );
}

sub __get_key {
    my ( $self ) = @_;
    # Windows:
    return $self->{input}->InputChar();

    # Linux:
    #return ReadKey( 0 );
}

sub readline {
    my ( $self, $prompt, $opt ) = @_;
    # ...
    # ...
    local $| = 1;
    $self->__init_term();
    my $str = encode( 'console_in', $opt->{default} );
    $self->__print_readline( $opt, $prompt, $str );

    while ( 1 ) {
        my $key = $self->{plugin}->__get_key();
        return if ! defined $key;
        elsif ( $key eq "\n" or $key eq "\r" ) {
            print "\n";
            $self->__reset_term();
            return decode( 'console_in', $str ) if $^O eq 'MSWin32';
            return decode( 'console_in', $str ) if utf8::is_utf8( $key );
            return $str;
        }
        elsif ( ord $key == BSPACE || $key eq "\cH" ) {
            if ( length $str ) {
                $str = decode( 'console_in', $str );
                $str =~ s/\X\z//;
                $str = encode( 'console_in', $str );
            }
            $self->__print_readline( $opt, $prompt, $str );
            next;
        }
        $key = encode( 'console_in', $key ) if utf8::is_utf8( $key );
        $str .= $key;
        $self->__print_readline( $opt, $prompt, $str );
    }
}

# ...

View full thread Projekt Term::ReadLine::Tiny