On 10/27/2017 09:35 PM, ToddAndMargo wrote:
Hi All,
With the help of Geekosaur over on the chat line, I fixed
my Pause.pm6 module. I came across a computer where the
module froze the terminal. Now it is fixed. I stood on
the shoulders of giants! Love the chat line!
Geekosaur found and reported a bug in the example of
Term::termios
https://github.com/krunen/term-termios/issues/10
To get my module and test program without the annoying eMail
line breaks:
http://vpaste.net/y37Pe
-T
<code Pause.pm6>
#!/usr/bin/env perl6
# Pause.pm6
#`{
References:
https://github.com/krunen/term-termios
https://github.com/ab5tract/Terminal-Print/blob/master/lib/Terminal/Print/RawInput.pm6#L17
https://linux.die.net/man/3/termios
# zef install Term::termios (as root)
# zef install Terminal::Print::RawInput (as root)
}
use Term::termios;
use Terminal::Print::RawInput;
sub Pause ( $PauseMessage) is export {
#`{
This sub will
1) dump (flush) the keyboard buffer
2) print out $PauseMessage
3) wait for a new (fresh) keyboard stroke
4) return the ASCII equivalent key that was pressed
Note: the GUI (Xfce, ec.) has precedence over reading
unusual keys, such as the "F" keys, etc.
}
my $c;
my $char;
my $saved_termios;
my $termios;
# Save the previous attrs
$saved_termios := Term::termios.new(fd => 1).getattr;
# Get the existing attrs in order to modify them
$termios := Term::termios.new(fd => 1).getattr;
# Set the tty to raw mode
$termios.makeraw;
# You could also do the same in the old-fashioned way
$termios.unset_iflags(<BRKINT ICRNL ISTRIP IXON>);
$termios.set_oflags(<ONLCR>);
$termios.set_cflags(<CS8>);
$termios.unset_lflags(<ECHO ICANON IEXTEN ISIG>);
# int "\n";
print "$PauseMessage";
$termios.setattr(:DRAIN);
$termios.setattr(:FLUSH);
# Disable Unicode grapheme processing
$*IN.encoding: Nil;
$c = $*IN.read(1).decode('latin-1');
$char = $c.ord < 32 ?? '^' ~ ($c.ord + 64).chr !! $c;
print "got: " ~ $c.ord ~ " $char\r\n";
^^^^^ you should comment the above line out
#`{
my $in-supply = raw-input-supply;
react {
whenever $in-supply -> $c {
$char = $c.ord < 32 ?? '^' ~ ($c.ord + 64).chr !! $c;
# printf "got: %3d %2s %2s\r\n", $c.ord, $c.ord.base(16),
$char;
# done if $c eq 'q';
done;
}
}
}
# Restore the saved, previous attributes before exit
$saved_termios.setattr(:DRAIN);
# print "$char\n";
return $char
}
</code>
<code Pause.pl6>
#!/usr/bin/env perl6
use lib '/home/linuxutil';
use strict;
use Pause; # qw[ Pause ];
my @z = qw[z zz zzz zzzz snore];
say "Sleeping for ", @z.elems, " seconds. Press some keys.";
for @z { sleep 1; print "$_ " }; print "\n";
my $Key = Pause( 'Press any key to continue... ' );
print "The key that was pressed was <$Key>\n";
</code>
--
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Computers are like air conditioners.
They malfunction when you open windows
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~