/[cvs]/joko/Scripts/psh/lib/POE/Component/Terminal.pm
ViewVC logotype

Annotation of /joko/Scripts/psh/lib/POE/Component/Terminal.pm

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.3 - (hide annotations)
Sat Jun 15 04:24:15 2002 UTC (22 years, 3 months ago) by cvsjoko
Branch: MAIN
Changes since 1.2: +21 -8 lines
+ added modifier for running in linux or win32
+ modified order of some commands in core key-polling

1 cvsjoko 1.1 #!/usr/bin/perl
2     ##
3     ## POE::Component::Terminal -- Terminal Component
4     ##
5 cvsjoko 1.3 ## $Id: Terminal.pm,v 1.2 2002/06/15 03:45:21 cvsjoko Exp $
6     ##
7     ## $Log: Terminal.pm,v $
8     ## Revision 1.2 2002/06/15 03:45:21 cvsjoko
9     ## + cvs id & log
10     ## + clearing inputbuffer just after getting the "enter"-key
11 cvsjoko 1.2 ##
12 cvsjoko 1.1 ##
13     package POE::Component::Terminal;
14    
15     use strict;
16     #use strict qw ( vars refs );
17     use warnings;
18    
19    
20     use Carp qw( croak carp );
21    
22     use POE;
23    
24     use Term::ReadKey;
25     use Data::Dumper;
26    
27     $POE::Component::Terminal::VERSION = 0.01;
28    
29     my @args_needed = qw (
30     Alias
31     Caption
32     PromptPrefix
33     PromptPostfix
34     RequestSession
35     RequestState
36     ResponseSession
37     ResponseState
38     PromptChangeState
39     );
40    
41     my $states = {
42     _start => 'start',
43     _stop => 'stop',
44     _signal => 'signal',
45     _default => 'default',
46     pollForKey => 'pollForKey',
47     recievePrompt => 'recievePrompt',
48     recieveResponse => 'recieveResponse',
49     };
50    
51 cvsjoko 1.3 sub RUNNING_IN_HELL () { $^O eq 'MSWin32' }
52    
53 cvsjoko 1.1 sub new {
54     my $class = shift;
55     $class = ref( $class ) || $class;
56     #my( %args ) = ( %default_args, @_ );
57     my $args = shift;
58    
59     # bless class into object ($self)
60     my $self = bless {}, $class;
61    
62     ## Check args
63     foreach my $arg_needed (@args_needed) {
64     croak "Need $arg_needed argument."
65     unless exists $args->{ $arg_needed };
66     }
67    
68     # store entries from args to object ($self)
69     map { $self->{$_} = $args->{$_}; } keys %{$args};
70    
71 cvsjoko 1.3 if ( RUNNING_IN_HELL () ) {
72     $self->{conf}{EnterKey} = "\r";
73     } else {
74     $self->{conf}{EnterKey} = "\n";
75     }
76    
77 cvsjoko 1.1 ## Make our session. See $states defined up above . . .
78     POE::Session->create( object_states => [ $self => $states, ], );
79    
80     return $self;
81     }
82    
83     ##
84     ## start -- Startup state
85     ##
86     sub start {
87     my( $self, $kernel, $heap, $session, $sender ) =
88     @_[ OBJECT, KERNEL, HEAP, SESSION, SENDER ];
89    
90     ## Pipe up if we're debugging
91     print STDERR "## ", __PACKAGE__, "::start\r\n" if $self->{Debug};
92    
93     ## Set the alias requested so we can be post'd to
94     $kernel->alias_set( $self->{Alias} );
95    
96     ## Remember session that created us for posting DispatchEvent
97     $self->{ _target } = $sender;
98    
99     $heap->{state}{InputBuffer} = '';
100     $heap->{state}{Startup} = 1;
101     $heap->{state}{Caption} = $self->{Caption};
102    
103     # initially call worker-state
104     $kernel->post($session, "pollForKey");
105    
106     return;
107     }
108    
109     ##
110     ## stop -- Shutdown state
111     ##
112     sub stop {
113     my( $self, $kernel, $heap ) = @_[ OBJECT, KERNEL, HEAP ];
114    
115     ## Just pipe up if we're debugging
116     print STDERR "## ", __PACKAGE__, "::stop\r\n" if $self->{_debug};
117    
118     return
119     }
120    
121     ##
122     ## signal -- Handle any signals received
123     ##
124     sub signal {
125     my( $self, $heap, $signal ) = @_[ OBJECT, HEAP, ARG0 ];
126    
127     ## Just pipe up if we're debugging
128     print STDERR "## ", __PACKAGE__, "::signal $signal\n" if $self->{_debug};
129    
130     ## shut things down on TERM, QUIT, or INT
131     if( $signal =~ /^TERM|QUIT|INT/ ) {
132     delete $heap->{wheel}; # toss our listening wheel reference
133    
134     ## Check for child wheels
135     my $live_wheels = scalar keys %{$heap->{wheels}};
136     if( $live_wheels ) {
137     print STDERR "Exiting with $live_wheels clients still active\n"
138     if $self->{_debug};
139     delete $heap->{wheels};
140     }
141     }
142    
143     return
144     }
145    
146     ##
147     ## default -- Catch any unhandled events for debugging
148     ##
149     sub default {
150     my( $self, $heap, $event ) = @_[ OBJECT, HEAP, ARG0 ];
151    
152     ## Just pipe up if we're debugging
153     print STDERR "## ", __PACKAGE__, "::default got $event\n"
154     if $self->{_debug};
155    
156     return
157     }
158    
159    
160    
161    
162    
163    
164    
165     # ================ POE stuff
166    
167     sub pollForKey {
168    
169     my ($self, $kernel, $heap, $session) = @_[OBJECT, KERNEL, HEAP, SESSION ];
170    
171     my $prompt_inlay = $heap->{state}{PromptInlay};
172     if ($prompt_inlay) { $prompt_inlay .= ' '; } else { $prompt_inlay = ''; }
173    
174     my $prompt = "#> ";
175     $prompt = $self->{PromptPrefix} . ' ' . $prompt_inlay . $self->{PromptPostfix};
176    
177     if ( ($heap->{state}{KeyPressed} && !$heap->{state}{RequestPending}) || ($heap->{state}{Startup}) ) {
178     $heap->{state}{Startup} = 0;
179     $heap->{state}{KeyPressed} = 0;
180     #ReadMode 4; # Turn off controls keys
181     #ReadMode 1;
182     #print $prompt;
183     my $outputline = $prompt . $heap->{state}{InputBuffer};
184     my $s = ' ' x (length($outputline) + 1);
185     print "\r", $s;
186     print "\r", $outputline;
187     }
188    
189     my $key = ReadKey(-1);
190     if ($key) {
191    
192     # normal key?
193     if ($key =~ m/[a-zA-Z? ]/) {
194     $heap->{state}{InputBuffer} .= $key;
195     #print $key;
196     }
197    
198     # enter pressed?
199 cvsjoko 1.3 if ($key eq $self->{conf}{EnterKey}) {
200 cvsjoko 1.1 # send command
201     my $buf = $heap->{state}{InputBuffer};
202     if ($buf) {
203 cvsjoko 1.2 $heap->{state}{InputBuffer} = '';
204 cvsjoko 1.1 $heap->{state}{RequestPending} = 1;
205     #print "ord: ", ord($buf), "\n";
206     my $response_target = {
207     ResponseSession => $self->{ResponseSession},
208     ResponseState => $self->{ResponseState},
209     };
210     $kernel->post( $self->{RequestSession} => $self->{RequestState} => $buf, $response_target);
211     }
212    
213     # prepare new loop for fresh prompt
214     print "\n";
215     }
216    
217     # backspace pressed?
218     if (ord($key) == 8) {
219     $heap->{state}{InputBuffer} = substr($heap->{state}{InputBuffer}, 0, -1);
220     }
221    
222 cvsjoko 1.3 # mark "KeyPressed" for next poll
223     $heap->{state}{KeyPressed} = 1;
224    
225 cvsjoko 1.1 }
226    
227 cvsjoko 1.3 #$kernel->post($session, "pollForKey");
228     #$kernel->delay("pollForKey", 0.1);
229     $kernel->delay("pollForKey", 0.01);
230 cvsjoko 1.1
231     }
232    
233    
234    
235    
236     sub recieveResponse {
237     my ($kernel, $heap, $session, $response) = @_[KERNEL, HEAP, SESSION, ARG0];
238     if ($response) { print $response, "\n"; }
239     $heap->{state}{RequestPending} = 0;
240     }
241     sub recievePrompt {
242     my ($kernel, $heap, $session, $prompt_inlay) = @_[KERNEL, HEAP, SESSION, ARG0];
243     $heap->{state}{PromptInlay} = $prompt_inlay;
244     }
245    
246    
247     1;

MailToCvsAdmin">MailToCvsAdmin
ViewVC Help
Powered by ViewVC 1.1.26 RSS 2.0 feed