Last active
August 9, 2017 13:31
Star
You must be signed in to star a gist
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env perl6 | |
| use Readline; | |
| use v6.c; | |
| my $readline = Readline.new; | |
| sub MAIN(Int $port) { | |
| await IO::Socket::Async.connect('127.0.0.1', $port).then( -> $p { | |
| if $p.status { | |
| given $p.result { | |
| say "CONNECT OK => ", $p.result; | |
| sub read-from-user() { | |
| while $readline.readline("==>") -> $line { | |
| $p.result.print($line ~ "\n"); | |
| { $p.result.close(); return } if $line eq 'exit'; | |
| } | |
| } | |
| supply { | |
| whenever .Supply() -> $v { | |
| printf("\r%s==>", $v); | |
| } | |
| }.tap; | |
| &read-from-user(); | |
| .close; | |
| } | |
| } | |
| }); | |
| } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env perl6 | |
| class Shell { | |
| has Proc::Async $.proc handles <say print write stdout stderr started ready>; | |
| has Promise $.promise handles <Supply>; | |
| method new($bash = 'zsh', :$enc = 'utf8') { | |
| self.bless(proc => Proc::Async.new($bash, :w, :$enc)); | |
| } | |
| method start() { | |
| unless $!promise { | |
| $!promise = $!proc.start; | |
| } | |
| $!promise; | |
| } | |
| } | |
| multi sub await(Shell:D $shell) { | |
| await $shell.promise; | |
| } | |
| sub processOutput(Supply $supply, $conn) { | |
| supply { | |
| whenever $supply -> $msg { | |
| emit $msg; | |
| } | |
| } | |
| } | |
| sub start-server($port) { | |
| react { | |
| whenever IO::Socket::Async.listen('0.0.0.0', $port) -> $conn { | |
| my $shell = Shell.new; | |
| sub tapOutput($v) { print "GOT => \n|", $v, "|\n"; $conn.print: $v; } | |
| &processOutput($shell.stdout, $conn).tap(&tapOutput); | |
| &processOutput($shell.stderr, $conn).tap(&tapOutput); | |
| "GOT A CONNECTION ".say; | |
| $shell.start; | |
| whenever $conn.Supply(:bin) -> $buf { | |
| await $shell.write($buf); | |
| LAST { | |
| default { | |
| $conn.close(); | |
| "CONNECT CLOSED".say; | |
| } | |
| } | |
| } | |
| } | |
| } | |
| } | |
| sub MAIN(Int $port) { | |
| start-server($port); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
under bash on win10: