| 1 | #!/usr/bin/perl -w | ||
| 2 | # vim: ts=2 sw=2 expandtab | ||
| 3 | |||
| 4 | use strict; | ||
| 5 | |||
| 6 | use Test::More; | ||
| 7 | use POSIX qw(_exit); | ||
| 8 | use POE qw(Wheel::Run Filter::Line Filter::Stream Wheel::ReadWrite); | ||
| 9 | |||
| 10 | BEGIN { | ||
| 11 | my $why; | ||
| 12 | |||
| 13 | $why = "This test requires Term::Size" | ||
| 14 | if do { eval "use Term::Size"; $@ }; | ||
| 15 | |||
| 16 | plan skip_all => $why if $why; | ||
| 17 | } | ||
| 18 | |||
| 19 | plan tests => 4; | ||
| 20 | |||
| 21 | my $winsize = [85, 29, 100, 200]; | ||
| 22 | |||
| 23 | ### Handle the _start event. This sets things in motion. | ||
| 24 | sub handle_start { | ||
| 25 | my ($kernel, $heap) = @_[KERNEL, HEAP]; | ||
| 26 | |||
| 27 | # Set a signal handler. | ||
| 28 | $kernel->sig(CHLD => "got_sigchld"); | ||
| 29 | |||
| 30 | # Start the terminal reader/writer. | ||
| 31 | $heap->{stdio} = POE::Wheel::ReadWrite->new( | ||
| 32 | InputHandle => \*STDIN, | ||
| 33 | OutputHandle => \*STDOUT, | ||
| 34 | InputEvent => "got_terminal_stdin", | ||
| 35 | Filter => POE::Filter::Line->new(), | ||
| 36 | ); | ||
| 37 | |||
| 38 | # Start the asynchronous child process. | ||
| 39 | $heap->{program} = POE::Wheel::Run->new( | ||
| 40 | Program => [ | ||
| 41 | $^X, '-e', ( | ||
| 42 | 'use Term::Size qw(chars pixels); ' . | ||
| 43 | 'my ($c, $r, $px, $py) = (chars(\\*STDIN), pixels(\\*STDIN)); ' . | ||
| 44 | 'print "rows: $r, cols: $c, xpix: $px, ypix: $py\\n"; ' | ||
| 45 | ) | ||
| 46 | ], | ||
| 47 | Conduit => "pty", | ||
| 48 | Winsize => $winsize, | ||
| 49 | StdoutEvent => "got_child_stdout", | ||
| 50 | StdioFilter => POE::Filter::Line->new(), | ||
| 51 | ); | ||
| 52 | |||
| 53 | } | ||
| 54 | |||
| 55 | sub handle_terminal_stdin { | ||
| 56 | my ($heap, $input) = @_[HEAP, ARG0]; | ||
| 57 | $heap->{program}->put($input); | ||
| 58 | } | ||
| 59 | |||
| 60 | sub handle_child_stdout { | ||
| 61 | my ($heap, $input) = @_[HEAP, ARG0]; | ||
| 62 | if ($input =~ m/^rows: (\d+), cols: (\d+), xpix: (\d+), ypix: (\d+)$/) { | ||
| 63 | is($winsize->[0], $1, 'rows set correctly'); | ||
| 64 | is($winsize->[1], $2, 'cols set correctly'); | ||
| 65 | is($winsize->[2], $3, 'xpix set correctly'); | ||
| 66 | is($winsize->[3], $4, 'ypix set correctly'); | ||
| 67 | } | ||
| 68 | } | ||
| 69 | |||
| 70 | sub handle_sigchld { | ||
| 71 | my ($heap, $child_pid) = @_[HEAP, ARG1]; | ||
| 72 | if ($child_pid == $heap->{program}->PID) { | ||
| 73 | delete $heap->{program}; | ||
| 74 | delete $heap->{stdio}; | ||
| 75 | } | ||
| 76 | return 0; | ||
| 77 | } | ||
| 78 | |||
| 79 | ### Start a session to encapsulate the previous features. | ||
| 80 | POE::Session->create( | ||
| 81 | inline_states => { | ||
| 82 | _start => \&handle_start, | ||
| 83 | got_terminal_stdin => \&handle_terminal_stdin, | ||
| 84 | got_child_stdout => \&handle_child_stdout, | ||
| 85 | got_sigchld => \&handle_sigchld, | ||
| 86 | }, | ||
| 87 | ); | ||
| 88 | |||
| 89 | POE::Kernel->run(); | ||
| 90 | |||
| 91 | 1; |