aboutsummaryrefslogtreecommitdiff
path: root/challenge-028/markus-holzer
diff options
context:
space:
mode:
authorholli-holzer <holli.holzer@gmail.com>2019-10-01 00:05:42 +0200
committerholli-holzer <holli.holzer@gmail.com>2019-10-01 00:10:51 +0200
commit019e5c99d99ea7698d4f5cef235aab995e15a871 (patch)
tree25c481267b97145d7f40736720ee300a139fdb65 /challenge-028/markus-holzer
parentd92bcbed1a2535200e78b948aabb2905c3ab6fa6 (diff)
downloadperlweeklychallenge-club-019e5c99d99ea7698d4f5cef235aab995e15a871.tar.gz
perlweeklychallenge-club-019e5c99d99ea7698d4f5cef235aab995e15a871.tar.bz2
perlweeklychallenge-club-019e5c99d99ea7698d4f5cef235aab995e15a871.zip
Solutions Markus Holzer
Diffstat (limited to 'challenge-028/markus-holzer')
-rw-r--r--challenge-028/markus-holzer/README16
-rw-r--r--challenge-028/markus-holzer/perl6/ch-1.pl65
-rw-r--r--challenge-028/markus-holzer/perl6/ch-2.pl645
-rw-r--r--challenge-028/markus-holzer/perl6/ch-2.pngbin0 -> 46939 bytes
-rw-r--r--challenge-028/markus-holzer/perl6/lib/Win/VT.pm665
-rw-r--r--challenge-028/markus-holzer/perl6/lib/Win/VT/Auto.pm66
-rw-r--r--challenge-028/markus-holzer/perl6/lib/Win/VT/Auto/I.pm66
-rw-r--r--challenge-028/markus-holzer/perl6/lib/Win/VT/Auto/O.pm68
8 files changed, 148 insertions, 3 deletions
diff --git a/challenge-028/markus-holzer/README b/challenge-028/markus-holzer/README
index 2000655be2..14bd23182a 100644
--- a/challenge-028/markus-holzer/README
+++ b/challenge-028/markus-holzer/README
@@ -1,6 +1,16 @@
Solutions by Markus Holzer.
-Solution #2 only works on the current developer branch and uses a module I wrote
-Please see https://github.com/holli-holzer/perl6-Scalar-History
-To be on CPAN soon
+Solution #1 requires the external command `file`
+For Windows that program is available at
+http://gnuwin32.sourceforge.net/packages/file.htm
+
+To make the clock tick on Windows, solution #2 requires a module
+I wrote which is not on CPAN yet: Win::VT (Name is likely to change).
+The module enables the terminal emulation in the "DOS" prompt.
+
+To run the script you need to say
+
+perl6 -MWin::VT::Auto::O ch-2.pl6 --at=2,2
+
+I included the module. It is not to be considered part of the challenge. \ No newline at end of file
diff --git a/challenge-028/markus-holzer/perl6/ch-1.pl6 b/challenge-028/markus-holzer/perl6/ch-1.pl6
new file mode 100644
index 0000000000..cebca072be
--- /dev/null
+++ b/challenge-028/markus-holzer/perl6/ch-1.pl6
@@ -0,0 +1,5 @@
+sub MAIN( $file )
+{
+ my $magic = run( "file", $file, :out ).out.slurp;
+ say "The file content is ", ($magic ~~ / \s text \, / ?? "ascii" !! "binary");
+}
diff --git a/challenge-028/markus-holzer/perl6/ch-2.pl6 b/challenge-028/markus-holzer/perl6/ch-2.pl6
new file mode 100644
index 0000000000..3cd1416b99
--- /dev/null
+++ b/challenge-028/markus-holzer/perl6/ch-2.pl6
@@ -0,0 +1,45 @@
+# This program requires a VT-100 compatible terminal or emulator thereof
+# xterm is just fine
+
+subset CoordStr of Str where / ^ \d+ \, \d+ $ /;
+
+my @numbers = map *.comb(3).Array,
+ "╻━╻┃ ┃╹━╹", " ╻ ┃ ╹", "╺━╻╻━╹╹━╸", "╺━╻╺━┃╺━╹", "╻ ╻╹━┃ ╹",
+ "╻━╸╹━╻╺━╹", "╻━╸┃━╻╹━╹", "╺━╻ ┃ ╹", "╻━╻┃━┃╹━╹", "╻━╻╹━┃╺━╹";
+
+
+multi sub MAIN( CoordStr :$at )
+{
+ my ($x, $y) = $at.Str.split(',');
+
+ react {
+ whenever Supply.interval(1) -> $v {
+ print clear-screen;
+ display-time( $x, $y, DateTime.now );
+ print go-to(0,0);
+ }
+ }
+}
+
+sub display-time( $x, $y, $time )
+{
+ for $time.hh-mm-ss.comb.kv -> $column, $part
+ {
+ if $part ~~ /\d/
+ {
+ for |@numbers[$part].kv -> $idx, $line
+ {
+ print go-to( $x + ($column * 3), $y + $idx ) ~ $line;
+ }
+ }
+ else
+ {
+ print go-to( $x + ($column * 3) , $y + 1) ~ " : ";
+ }
+ }
+}
+
+sub clear-screen() { escape("2J") ~ escape(";H"); }
+sub go-to( $column, $row ) { escape( "$row;$column" ~ "H" ); }
+sub escape( $value ) { "\e[" ~ $value; }
+
diff --git a/challenge-028/markus-holzer/perl6/ch-2.png b/challenge-028/markus-holzer/perl6/ch-2.png
new file mode 100644
index 0000000000..0ddc6b37b6
--- /dev/null
+++ b/challenge-028/markus-holzer/perl6/ch-2.png
Binary files differ
diff --git a/challenge-028/markus-holzer/perl6/lib/Win/VT.pm6 b/challenge-028/markus-holzer/perl6/lib/Win/VT.pm6
new file mode 100644
index 0000000000..88fbcb97c6
--- /dev/null
+++ b/challenge-028/markus-holzer/perl6/lib/Win/VT.pm6
@@ -0,0 +1,65 @@
+unit module Win::VT;
+
+use NativeCall;
+
+constant STD_INPUT_HANDLE = -0xA;
+constant STD_OUTPUT_HANDLE = -0xB;
+constant ENABLE_VIRTUAL_TERMINAL_INPUT = 0x0200;
+constant ENABLE_PROCESSED_INPUT = 0x0001;
+constant ENABLE_PROCESSED_OUTPUT = 0x0001;
+constant ENABLE_VIRTUAL_TERMINAL_PROCESSING = 0x0004;
+constant DWORD := uint32;
+constant BOOL := int32;
+constant HANDLE := Pointer[void];
+
+sub GetConsoleMode(HANDLE, DWORD is rw) is native('Kernel32') returns BOOL { * };
+sub SetConsoleMode(HANDLE, DWORD) is native('Kernel32') returns BOOL { * };
+sub GetStdHandle(DWORD) is native('Kernel32') returns HANDLE { * };
+
+my HANDLE $input-handle;
+my HANDLE $output-handle;
+my DWORD $output-mode;
+my DWORD $input-mode;
+
+INIT {
+ if $*VM.osname eq "mswin32"
+ {
+ $input-handle = GetStdHandle( STD_INPUT_HANDLE );
+ $output-handle = GetStdHandle( STD_OUTPUT_HANDLE );
+ }
+}
+sub foo
+{
+
+}
+
+sub vt-on(Bool :$vt-input=True, Bool :$vt-output=True ) returns Bool is export(:MANDATORY)
+{
+ return False
+ unless $input-handle.defined && $output-handle.defined;
+
+ return False
+ unless GetConsoleMode($input-handle, $input-mode) && GetConsoleMode($output-handle, $output-mode);
+
+ my $new-input-mode = $input-mode +| ENABLE_PROCESSED_INPUT +| ENABLE_VIRTUAL_TERMINAL_INPUT;
+ my $new-output-mode = $output-mode +| ENABLE_PROCESSED_OUTPUT +| ENABLE_VIRTUAL_TERMINAL_PROCESSING;
+
+ return (
+ ( $vt-input ?? SetConsoleMode($input-handle, $new-input-mode) !! 1 ) &&
+ ( $vt-output ?? SetConsoleMode($output-handle, $new-output-mode) !! 1 )
+ ).Bool;
+}
+
+sub vt-off() returns Bool is export(:MANDATORY) {
+ return (
+ $input-handle.defined && $output-handle.defined &&
+ SetConsoleMode($output-handle, $output-mode) &&
+ SetConsoleMode($input-handle, $input-mode)
+ ).Bool;
+}
+
+sub cls() is export(:cls) {
+ vt-on;
+ print chr(27) ~ '[2J' ~ chr(27) ~ '[;H';
+ vt-off;
+}
diff --git a/challenge-028/markus-holzer/perl6/lib/Win/VT/Auto.pm6 b/challenge-028/markus-holzer/perl6/lib/Win/VT/Auto.pm6
new file mode 100644
index 0000000000..d407e8012f
--- /dev/null
+++ b/challenge-028/markus-holzer/perl6/lib/Win/VT/Auto.pm6
@@ -0,0 +1,6 @@
+use Win::VT;
+
+unit module Win::VT::Auto;
+INIT { vt-on; }
+END { vt-off; }
+signal(SIGINT).tap({ vt-off; } ); \ No newline at end of file
diff --git a/challenge-028/markus-holzer/perl6/lib/Win/VT/Auto/I.pm6 b/challenge-028/markus-holzer/perl6/lib/Win/VT/Auto/I.pm6
new file mode 100644
index 0000000000..f75da7cff0
--- /dev/null
+++ b/challenge-028/markus-holzer/perl6/lib/Win/VT/Auto/I.pm6
@@ -0,0 +1,6 @@
+use Win::VT;
+
+unit module Win::VT::Auto::I;
+INIT { vt-on( :vt-output(False) ) }
+END { vt-off; }
+signal(SIGINT).tap({ vt-off; } ); \ No newline at end of file
diff --git a/challenge-028/markus-holzer/perl6/lib/Win/VT/Auto/O.pm6 b/challenge-028/markus-holzer/perl6/lib/Win/VT/Auto/O.pm6
new file mode 100644
index 0000000000..ae74ff3b1e
--- /dev/null
+++ b/challenge-028/markus-holzer/perl6/lib/Win/VT/Auto/O.pm6
@@ -0,0 +1,8 @@
+use Win::VT;
+
+unit module Win::VT::Auto::O;
+INIT { vt-on( :vt-input(False) ) }
+END { vt-off; }
+signal(SIGINT).tap({ vt-off; } );
+
+