Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor the Tk event loop to allow for other event loops
Term::ReadLine supports any event loop, including unpubished ones and simple IO::Select loops without the need to rewrite existing code for any particular framework.
- Loading branch information
Max Maischein
committed
Feb 19, 2012
1 parent
ac076a5
commit fc013e9
Showing
4 changed files
with
161 additions
and
148 deletions.
There are no files selected for viewing
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
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
#!perl | ||
|
||
use Test::More; | ||
|
||
eval "use Tk; 1" or | ||
plan skip_all => "Tk is not installed."; | ||
|
||
# seeing as the entire point of this test is to test the event handler, | ||
# we need to mock as little as possible. To keep things tightly controlled, | ||
# we'll use the Stub directly. | ||
BEGIN { | ||
$ENV{PERL_RL} = 'Stub o=0'; | ||
} | ||
|
||
my $mw; | ||
eval { | ||
use File::Spec; | ||
$mw = MainWindow->new(); $mw->withdraw(); | ||
1; | ||
} or plan skip_all => "Tk can't start. DISPLAY not set?"; | ||
|
||
# need to delay this so that Tk is loaded first. | ||
require Term::ReadLine; | ||
|
||
plan tests => 3; | ||
|
||
my $t = Term::ReadLine->new('Tk'); | ||
ok($t, "Created object"); | ||
is($t->ReadLine, 'Term::ReadLine::Stub', 'Correct type'); | ||
|
||
# This test will dispatch Tk events externally. | ||
$t->tkRunning(0); | ||
|
||
my $text = 'some text'; | ||
my $T = $text . "\n"; | ||
|
||
my $w = Tk::after($mw,0, | ||
sub { | ||
pass("Event loop called"); | ||
exit 0; | ||
}); | ||
|
||
my $result = tk_readline($t, 'Do not press enter>'); | ||
fail("Should not get here."); | ||
|
||
# A Tk-dispatching readline that doesn't require Tk (or any other | ||
# event loop) support to be hard-coded into Term::ReadLine. | ||
|
||
sub tk_readline { | ||
my ($term, $prompt) = @_; | ||
|
||
$term->print_prompt($prompt); | ||
|
||
my $got_input; | ||
Tk->fileevent($term->IN, 'readable', sub { $got_input = 1 }); | ||
Tk::DoOneEvent(0) until $got_input; | ||
|
||
return $term->get_line(); | ||
} |