8

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?

Håkon Hægland
  • 39,012
  • 21
  • 81
  • 174
user2145475
  • 657
  • 3
  • 11
  • There is [Terminal::Print::RawInput](https://github.com/ab5tract/Terminal-Print/blob/master/lib/Terminal/Print/RawInput.pm6). Maybe you could use that? – Håkon Hægland Mar 04 '19 at 22:20
  • cf https://stackoverflow.com/questions/43032827/perl6-is-there-a-way-to-do-editable-prompt-input – raiph Mar 04 '19 at 23:07

1 Answers1

10

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'";
Håkon Hægland
  • 39,012
  • 21
  • 81
  • 174