Skip to content

Commit

Permalink
Made BarnOwl::start_* wrappers around a new BarnOwl::start_edit
Browse files Browse the repository at this point in the history
A new function, BarnOwl::start_edit, was created as the preferred
interface for starting the edit_win from perl.

The perl interface to the edit_win is backwards compatible.

This is mostly preparation for the next commit, which generalizes the
syntax of starting an edit_win to allow callbacks on cancel.
  • Loading branch information
JasonGross committed Sep 30, 2011
1 parent c737503 commit e89ec48
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 67 deletions.
76 changes: 65 additions & 11 deletions perl/lib/BarnOwl.pm
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ our @EXPORT_OK = qw(command getcurmsg getnumcols getidletime
zephyr_getsender zephyr_getrealm zephyr_zwrite
zephyr_stylestrip zephyr_smartstrip_user zephyr_getsubs
queue_message admin_message
start_edit
start_question start_password start_edit_win
get_data_dir get_config_dir popless_text popless_ztext
error debug
Expand Down Expand Up @@ -104,29 +105,82 @@ separated by newlines.
Enqueue a message in the BarnOwl message list, logging it and
processing it appropriately. C<MESSAGE> should be an instance of
BarnOwl::Message or a subclass. Returns the queued message. This
is useful for, e.g., deleting a message from the message list.
BarnOwl::Message or a subclass.
=head2 admin_message HEADER BODY
Display a BarnOwl B<Admin> message, with the given header and body.
=head2 start_question PROMPT CALLBACK
=head2 start_edit %ARGS
Displays C<PROMPT> on the screen and lets the user enter a line of
text, and calls C<CALLBACK>, which must be a perl subroutine
reference, with the text the user entered
Displays a prompt on the screen and lets the user enter text,
and calls a callback when the editwin is closed.
=head2 start_password PROMPT CALLBACK
C<%ARGS> must contain the following keys:
=over 4
=item prompt
The line to display on the screen
=item type
One of:
=over 4
=item edit_win
Displays the prompt on a line of its own and opens the edit_win.
=item question
Displays prompt on the screen and lets the user enter a line of
text.
Like C<start_question>, but echoes the user's input as C<*>s when they
=item password
Like question, but echoes the user's input as C<*>s when they
input.
=back
=item callback
A Perl subroutine that is called when the user closes the edit_win.
=back
=head2 start_question PROMPT CALLBACK
=head2 start_password PROMPT CALLBACK
=head2 start_edit_win PROMPT CALLBACK
Like C<start_question>, but displays C<PROMPT> on a line of its own
and opens the editwin. If the user cancels the edit win, C<CALLBACK>
is not invoked.
Equivalent to C<start_edit> called with the appropriate parameters.
=cut

sub start_edit {
my %args = (@_);
BarnOwl::Internal::start_edit($args{type}, $args{prompt}, $args{callback});
}

sub start_question {
my ($prompt, $callback) = @_;
BarnOwl::start_edit(type => 'question', prompt => $prompt, callback => $callback);
}

sub start_password {
my ($prompt, $callback) = @_;
BarnOwl::start_edit(type => 'password', prompt => $prompt, callback => $callback);
}

sub start_edit_win {
my ($prompt, $callback) = @_;
BarnOwl::start_edit(type => 'edit_win', prompt => $prompt, callback => $callback);
}

=head2 get_data_dir
Expand Down
81 changes: 25 additions & 56 deletions perlglue.xs
Original file line number Diff line number Diff line change
Expand Up @@ -166,62 +166,6 @@ admin_message(header, body)
owl_function_adminmsg(header, body);
}

void
start_question(line, callback)
const char *line
SV *callback
PREINIT:
owl_editwin *e;
CODE:
{
if(!SV_IS_CODEREF(callback))
croak("Callback must be a subref");

e = owl_function_start_question(line);

owl_editwin_set_cbdata(e,
newSVsv(callback),
owl_perlconfig_dec_refcnt);
owl_editwin_set_callback(e, owl_perlconfig_edit_callback);
}

void
start_password(line, callback)
const char *line
SV *callback
PREINIT:
owl_editwin *e;
CODE:
{
if(!SV_IS_CODEREF(callback))
croak("Callback must be a subref");

e = owl_function_start_password(line);

owl_editwin_set_cbdata(e,
newSVsv(callback),
owl_perlconfig_dec_refcnt);
owl_editwin_set_callback(e, owl_perlconfig_edit_callback);
}

void
start_edit_win(line, callback)
const char *line
SV *callback
PREINIT:
owl_editwin *e;
CODE:
{
if(!SV_IS_CODEREF(callback))
croak("Callback must be a subref");

e = owl_function_start_edit_win(line);
owl_editwin_set_cbdata(e,
newSVsv(callback),
owl_perlconfig_dec_refcnt);
owl_editwin_set_callback(e, &owl_perlconfig_edit_callback);
}


const char *
get_data_dir ()
Expand Down Expand Up @@ -498,6 +442,31 @@ new_variable_bool(name, ival, summ, desc)
desc,
ival);

void
start_edit(edit_type, line, callback)
const char *edit_type
const char *line
SV *callback
PREINIT:
owl_editwin *e;
CODE:
{
if (!SV_IS_CODEREF(callback))
croak("Callback must be a subref");

if (!strcmp(edit_type, "question"))
e = owl_function_start_question(line);
else if (!strcmp(edit_type, "password"))
e = owl_function_start_password(line);
else if (!strcmp(edit_type, "edit_win"))
e = owl_function_start_edit_win(line);
else
croak("edit_type must be one of 'password', 'question', 'edit_win', not '%s'", edit_type);

owl_editwin_set_cbdata(e, newSVsv(callback), owl_perlconfig_dec_refcnt);
owl_editwin_set_callback(e, owl_perlconfig_edit_callback);
}

MODULE = BarnOwl PACKAGE = BarnOwl::Editwin

int
Expand Down

0 comments on commit e89ec48

Please sign in to comment.