Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Perl6 Terminal::Print how to prompt the user for input text?

I'm using the Perl6 Terminal::Print module for a console based application.

It's working well - however, now I need to prompt the user for a string of text.

What's a good way to do this?

like image 417
user2145475 Avatar asked Mar 04 '19 22:03

user2145475


1 Answers

Here is an example of using Terminal::Print::RawInput to get a filename as user input:

use v6;
use Terminal::Print;
use Terminal::Print::RawInput;

my $screen = Terminal::Print.new;

# saves current screen state, blanks screen, and hides cursor
$screen.initialize-screen;

$screen.print-string(9, 23, "Enter filename: ");
my $in-supply = raw-input-supply;
my $filename;
react {
    whenever $in-supply -> $c {
        done if $c.ord ~~ 3|13; # Enter pressed or CTRL-C pressed
        my $char = $c.ord < 32 ?? '' !! $c;
        $filename ~= $char;
        print $char;
     }
}

sleep .1;
$screen.shutdown-screen; 
say "Filename entered: '$filename'";
like image 115
Håkon Hægland Avatar answered Oct 28 '22 13:10

Håkon Hægland