Skip Menu |

This queue is for tickets about the Term-ShellUI CPAN distribution.

Report information
The Basics
Id: 63501
Status: resolved
Priority: 0/
Queue: Term-ShellUI

People
Owner: Nobody in particular
Requestors: Lester Hightower (no email address)
Cc:
AdminCc:

Bug Information
Severity: Important
Broken in: (no value)
Fixed in: (no value)



Subject: Request for an API hook into Term::ShellUI's exit on Ctrl-D
My program has functionality to protect the user from accidentally exiting without first saving their work if the database has changed. If the user types "quit" or "exit" that works fine, but Term::ShellUI exits with the press of Ctrl-D without any way (that I can find) for me to hook into that process. I've looked at the code and it seems that providing an API hook into the exit-on-EOF functionality would be pretty easy. Please consider adding such a hook, or, if you'd prefer that I code it and send a patch, please reply to this bug to that effect and I will be glad to.
On Wed Dec 01 08:38:13 2010, lester.hightower@gmail.com wrote: Show quoted text
> My program has functionality to protect the user from accidentally > exiting without first saving their work if the database has changed.
If Show quoted text
> the user types "quit" or "exit" that works fine, but Term::ShellUI
exits Show quoted text
> with the press of Ctrl-D without any way (that I can find) for me to
hook Show quoted text
> into that process. I've looked at the code and it seems that providing
an Show quoted text
> API hook into the exit-on-EOF functionality would be pretty easy.
Please Show quoted text
> consider adding such a hook, or, if you'd prefer that I code it and
send Show quoted text
> a patch, please reply to this bug to that effect and I will be glad
to. That sounds very useful. If you code a patch, I'll happily integrate it!
From: lester.hightower [...] gmail.com
Show quoted text
> On Wed Dec 01 08:38:13 2010, lester.hightower@gmail.com wrote: >
> > My program has functionality to protect the user from accidentally > > exiting without first saving their work if the database has > > changed. If the user types "quit" or "exit" that works fine, but > > Term::ShellUI exits with the press of Ctrl-D without any way (that > > I can find) for me to hook into that process. I've looked at the > > code and it seems that providing an API hook into the exit-on-EOF > > functionality would be pretty easy. Please consider adding such a > > hook, or, if you'd prefer that I code it and send a patch, please > > reply to this bug to that effect and I will be glad to.
On Fri Dec 03 17:59:56 2010, BRONSON wrote: Show quoted text
> That sounds very useful. If you code a patch, I'll happily > integrate it!
Scott, A unified diff of the patch is attached. In addition to the new feature, I fixed a few minor POD errors that perldoc was complaining about. If I did what I intended to do, you should be able to apply this patch and immediately ship a 0.87 version. Sincerely, Lester Hightower
Subject: Term-ShellUI-eof-exit-hooks-patch.diff
--- /usr/share/perl5/Term/ShellUI-orig.pm 2010-12-03 20:13:35.000000000 -0500 +++ /usr/share/perl5/Term/ShellUI.pm 2010-12-03 20:46:42.000000000 -0500 @@ -12,7 +12,7 @@ use Text::Shellwords::Cursor; use vars qw($VERSION); -$VERSION = '0.86'; +$VERSION = '0.87'; =head1 NAME @@ -858,7 +858,7 @@ my $OUT = $self->{'OUT'}; my $rawline = ""; - for(;;) { + INPUT_LOOP: for(;;) { my $prompt = $self->prompt(); $prompt = $prompt->[length $rawline ? 1 : 0] if ref $prompt eq 'ARRAY'; $prompt = $prompt->($self, $rawline) if ref $prompt eq 'CODE'; @@ -866,6 +866,14 @@ # EOF exits unless(defined $newline) { + # If we have eof_exit_hooks let them have a say + if (scalar(@{$self->{eof_exit_hooks}})) { + foreach my $sub (@{$self->{eof_exit_hooks}}) { + if (&$sub()) { + next INPUT_LOOP; + } + } + } print $OUT "\n"; $self->exit_requested(1); return undef; @@ -1047,6 +1055,30 @@ sub exit_requested { return shift->getset('done', shift); } +=item add_eof_exit_hook(subroutine_reference) + +Call this method to add a subroutine as a hook into Term::ShellUI's +"exit on EOF" (Ctrl-D) functionality. When a user enters Ctrl-D, +Term::ShellUI will call each function in this hook list, in order, +and will exit only if all of them return 0. The first function to +return a non-zero value will stop further processing of these hooks +and prevent the program from exiting. + +The return value of this method is the placement of the hook routine +in the hook list (1 is first) or 0 (zero) on failure. + +=cut + +sub add_eof_exit_hook { + my $self=shift @_; + my $refcode=shift @_; + if (ref($refcode) eq 'CODE') { + push (@{$self->{eof_exit_hooks}}, $refcode); + return scalar @{$self->{eof_exit_hooks}}; + } + return 0; +} + =item get_cname(cname) This is a tiny utility function that turns the cname (array ref @@ -1064,13 +1096,15 @@ return join(" ", @$cname); } - +=back =head1 OVERRIDES These are routines that probably already do the right thing. If not, however, they are designed to be overridden. +=over + =item blank_line() This routine is called when the user inputs a blank line. @@ -1112,7 +1146,7 @@ print STDERR @_; } - +=back =head1 WRITING A COMPLETION ROUTINE @@ -1216,6 +1250,8 @@ The following are utility routines that your completion function can call. +=over + =item completemsg(msg) your completion routine should call this to display text onscreen @@ -1327,12 +1363,16 @@ return $results; } +=back + =head1 INTERNALS These commands are internal to ShellUI. They are documented here only for completeness -- you should never need to call them. +=over + =item get_deep_command Looks up the supplied command line in a command hash. @@ -2022,4 +2062,12 @@ =cut +=head1 OTHER CONTRIBUTORS + +Lester Hightower E<lt>hightowe@cpan.orgE<gt> + +=cut + +1; + 1;
From: lester.hightower [...] gmail.com
After rt.cpan.org printed my patch back at me I noticed the double "1;" lines at the end... sorry about that. So, apply the patch and remove one of those -- no harm in leaving it, but it's surely not needed. -- Lester
From: lester.hightower [...] gmail.com
Scott, I found a small bug in my patch this AM. Please accept the attached file which fixes that and also removes the duplicate trailing "1;" ... -- Lester
Subject: Term-ShellUI-eof-exit-hooks-patch.diff
--- ShellUI-orig.pm 2010-12-03 20:13:35.000000000 -0500 +++ ShellUI.pm 2010-12-04 11:25:51.000000000 -0500 @@ -12,7 +12,7 @@ use Text::Shellwords::Cursor; use vars qw($VERSION); -$VERSION = '0.86'; +$VERSION = '0.87'; =head1 NAME @@ -839,6 +839,8 @@ $self->{OUT} = $self->{term}->OUT || \*STDOUT; $self->{prevcmd} = ""; # cmd to run again if user hits return + @{$self->{eof_exit_hooks}} = (); + return $self; } @@ -858,7 +860,7 @@ my $OUT = $self->{'OUT'}; my $rawline = ""; - for(;;) { + INPUT_LOOP: for(;;) { my $prompt = $self->prompt(); $prompt = $prompt->[length $rawline ? 1 : 0] if ref $prompt eq 'ARRAY'; $prompt = $prompt->($self, $rawline) if ref $prompt eq 'CODE'; @@ -866,6 +868,14 @@ # EOF exits unless(defined $newline) { + # If we have eof_exit_hooks let them have a say + if (scalar(@{$self->{eof_exit_hooks}})) { + foreach my $sub (@{$self->{eof_exit_hooks}}) { + if (&$sub()) { + next INPUT_LOOP; + } + } + } print $OUT "\n"; $self->exit_requested(1); return undef; @@ -1047,6 +1057,30 @@ sub exit_requested { return shift->getset('done', shift); } +=item add_eof_exit_hook(subroutine_reference) + +Call this method to add a subroutine as a hook into Term::ShellUI's +"exit on EOF" (Ctrl-D) functionality. When a user enters Ctrl-D, +Term::ShellUI will call each function in this hook list, in order, +and will exit only if all of them return 0. The first function to +return a non-zero value will stop further processing of these hooks +and prevent the program from exiting. + +The return value of this method is the placement of the hook routine +in the hook list (1 is first) or 0 (zero) on failure. + +=cut + +sub add_eof_exit_hook { + my $self=shift @_; + my $refcode=shift @_; + if (ref($refcode) eq 'CODE') { + push (@{$self->{eof_exit_hooks}}, $refcode); + return scalar @{$self->{eof_exit_hooks}}; + } + return 0; +} + =item get_cname(cname) This is a tiny utility function that turns the cname (array ref @@ -1064,13 +1098,15 @@ return join(" ", @$cname); } - +=back =head1 OVERRIDES These are routines that probably already do the right thing. If not, however, they are designed to be overridden. +=over + =item blank_line() This routine is called when the user inputs a blank line. @@ -1112,7 +1148,7 @@ print STDERR @_; } - +=back =head1 WRITING A COMPLETION ROUTINE @@ -1216,6 +1252,8 @@ The following are utility routines that your completion function can call. +=over + =item completemsg(msg) your completion routine should call this to display text onscreen @@ -1327,12 +1365,16 @@ return $results; } +=back + =head1 INTERNALS These commands are internal to ShellUI. They are documented here only for completeness -- you should never need to call them. +=over + =item get_deep_command Looks up the supplied command line in a command hash. @@ -2022,4 +2064,10 @@ =cut +=head1 OTHER CONTRIBUTORS + +Lester Hightower E<lt>hightowe@cpan.orgE<gt> + +=cut + 1;
From: lester.hightower [...] gmail.com
Scott, Is there a format other than unified diff that you would prefer to get this patch in or can I help in any other way to get 0.87 released? Thanks, Lester
Sorry Lester, I haven't been using Perl lately much less Term::ShellUI! Nice patch, merged. I also found some time to clean up some things that were bothering me and release a 0.9. It's now on Github so you can just fork it and go nuts. It might be years before I look at this module again. :) https://github.com/bronson/Term-ShellUI
On Sun Mar 20 04:52:42 2011, BRONSON wrote: Show quoted text
> Sorry Lester, I haven't been using Perl lately much less
Term::ShellUI! Show quoted text
> > Nice patch, merged. > > I also found some time to clean up some things that were bothering me
and Show quoted text
> release a 0.9. It's now on Github so you can just fork it and go
nuts. Show quoted text
> It might be years before I look at this module again. :) > > https://github.com/bronson/Term-ShellUI
Scott, I really like your work in Term::ShellUI. If you truly think that it will be years before you would work on this module again, I would not mind taking over maintainership of it, either entirely or in addition to you. I would personally prefer to not fork the code. I am not sure how CPAN/PAUSE handles changing or adding module owners (would need to investigate that), but I am willing if that interests you... Sincerely, Lester
Subject: Re: [rt.cpan.org #63501] Request for an API hook into Term::ShellUI's exit on Ctrl-D
Date: Tue, 5 Apr 2011 15:49:24 -0700
To: bug-Term-ShellUI [...] rt.cpan.org
From: Scott Bronson <bronson [...] rinspin.com>
Hi Lester. There are a number of cleanups I'd like to make but, since I don't have any projects actually using Term::ShellUI right now, it's rather hard to find the time. I'm happy to hand over the reins. There appears to be a full-on fork in the works with some excellent features... Not sure if they'll be rolled back into a massively improved Term::ShellUI or if it will just remain a fork. Hit me up by email and I'll get you in touch with the author: brons_cpan02@rinspin.com On Tue, Apr 5, 2011 at 3:44 PM, Lester Hightower via RT <bug-Term-ShellUI@rt.cpan.org> wrote: Show quoted text
>       Queue: Term-ShellUI >  Ticket <URL: https://rt.cpan.org/Ticket/Display.html?id=63501 > > > On Sun Mar 20 04:52:42 2011, BRONSON wrote:
>> Sorry Lester, I haven't been using Perl lately much less
> Term::ShellUI!
>> >> Nice patch, merged. >> >> I also found some time to clean up some things that were bothering me
> and
>> release a 0.9.  It's now on Github so you can just fork it and go
> nuts.
>> It might be years before I look at this module again.  :) >> >> https://github.com/bronson/Term-ShellUI
> > Scott, > > I really like your work in Term::ShellUI. If you truly think that it > will be years before you would work on this module again, I would not > mind taking over maintainership of it, either entirely or in addition to > you. I would personally prefer to not fork the code. I am not sure how > CPAN/PAUSE handles changing or adding module owners (would need to > investigate that), but I am willing if that interests you... > > Sincerely, > > Lester >
The new project is now public: Term::CiscoCLI http://ciscocli.sf.net/ However, it like there's enough interest in Term::ShellUI that it will stick around for a while yet. As long as patches are flowing in, I'll keep producing releases.