Changes in perl/lib/BarnOwl.pm [a130fc5:ecd4edf]
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
perl/lib/BarnOwl.pm
ra130fc5 recd4edf 5 5 6 6 use base qw(Exporter); 7 our @EXPORT_OK = qw(command getcurmsg getnumcols getidletime 7 our @EXPORT_OK = qw(command getcurmsg getnumcols getnumlines getidletime 8 register_idle_watcher unregister_idle_watcher 8 9 zephyr_getsender zephyr_getrealm zephyr_zwrite 9 10 zephyr_stylestrip zephyr_smartstrip_user zephyr_getsubs 10 11 queue_message admin_message 12 start_edit 11 13 start_question start_password start_edit_win 12 14 get_data_dir get_config_dir popless_text popless_ztext 13 15 error debug 14 16 create_style getnumcolors wordwrap 17 message_matches_filter 15 18 add_dispatch remove_dispatch 16 19 add_io_dispatch remove_io_dispatch 17 20 new_command 18 21 new_variable_int new_variable_bool new_variable_string 22 new_variable_enum 19 23 quote redisplay); 20 24 our %EXPORT_TAGS = (all => \@EXPORT_OK); … … 43 47 44 48 use List::Util qw(max); 49 use Tie::RefHash; 45 50 46 51 =head1 NAME … … 92 97 command line, and C<MESSAGE> is the zephyr body to send. 93 98 99 =cut 100 101 sub zephyr_zwrite { 102 my ($command, $message) = @_; 103 my $ret = BarnOwl::Internal::zephyr_zwrite($command, $message); 104 die "Error sending zephyr" unless $ret == 0; 105 } 106 94 107 =head2 ztext_stylestrip STRING 95 108 … … 105 118 Enqueue a message in the BarnOwl message list, logging it and 106 119 processing it appropriately. C<MESSAGE> should be an instance of 107 BarnOwl::Message or a subclass. Returns the queued message. This 108 is useful for, e.g., deleting a message from the message list. 120 BarnOwl::Message or a subclass. 109 121 110 122 =head2 admin_message HEADER BODY … … 112 124 Display a BarnOwl B<Admin> message, with the given header and body. 113 125 126 =head2 start_edit %ARGS 127 128 Displays a prompt on the screen and lets the user enter text, 129 and calls a callback when the editwin is closed. 130 131 C<%ARGS> must contain the following keys: 132 133 =over 4 134 135 =item prompt 136 137 The line to display on the screen 138 139 =item type 140 141 One of: 142 143 =over 4 144 145 =item edit_win 146 147 Displays the prompt on a line of its own and opens the edit_win. 148 149 =item question 150 151 Displays prompt on the screen and lets the user enter a line of 152 text. 153 154 =item password 155 156 Like question, but echoes the user's input as C<*>s when they 157 input. 158 159 =back 160 161 =item callback 162 163 A Perl subroutine that is called when the user closes the edit_win. 164 C<CALLBACK> gets called with two parameters: the text the user entered, 165 and a C<SUCCESS> boolean parameter which is false if the user canceled 166 the edit_win and true otherwise. 167 168 =back 169 114 170 =head2 start_question PROMPT CALLBACK 115 171 116 Displays C<PROMPT> on the screen and lets the user enter a line of117 text, and calls C<CALLBACK>, which must be a perl subroutine118 reference, with the text the user entered119 120 172 =head2 start_password PROMPT CALLBACK 121 173 122 Like C<start_question>, but echoes the user's input as C<*>s when they123 input.124 125 174 =head2 start_edit_win PROMPT CALLBACK 126 175 127 Like C<start_question>, but displays C<PROMPT> on a line of its own 128 and opens the editwin. If the user cancels the edit win, C<CALLBACK> 129 is not invoked. 176 Roughly equivalent to C<start_edit> called with the appropriate parameters. 177 C<CALLBACK> is only called on success, for compatibility. 178 179 These are deprecated wrappers around L<BarnOwl::start_edit>, and should not 180 be uesd in new code. 181 182 =cut 183 184 sub start_edit { 185 my %args = (@_); 186 BarnOwl::Internal::start_edit($args{type}, $args{prompt}, $args{callback}); 187 } 188 189 sub start_question { 190 my ($prompt, $callback) = @_; 191 BarnOwl::start_edit(type => 'question', prompt => $prompt, callback => sub { 192 my ($text, $success) = @_; 193 $callback->($text) if $success; 194 }); 195 } 196 197 sub start_password { 198 my ($prompt, $callback) = @_; 199 BarnOwl::start_edit(type => 'password', prompt => $prompt, callback => sub { 200 my ($text, $success) = @_; 201 $callback->($text) if $success; 202 }); 203 } 204 205 sub start_edit_win { 206 my ($prompt, $callback) = @_; 207 BarnOwl::start_edit(type => 'edit_win', prompt => $prompt, callback => sub { 208 my ($text, $success) = @_; 209 $callback->($text) if $success; 210 }); 211 } 130 212 131 213 =head2 get_data_dir … … 160 242 161 243 Returns the number of colors this BarnOwl is capable of displaying 244 245 =head2 message_matches_filter MESSAGE FILTER_NAME [QUIET = 0] 246 247 Returns 1 if C<FILTER_NAME> is the name of a valid filter, and 248 C<MESSAGE> matches that filter. Returns 0 otherwise. If 249 C<QUIET> is false, this method displays an error message if 250 if C<FILTER_NAME> does not name a valid filter. 162 251 163 252 =head2 add_dispatch FD CALLBACK … … 259 348 our @all_commands; 260 349 261 if(!$configfile && -f $ENV{HOME} . "/.barnowlconf") { 262 $configfile = $ENV{HOME} . "/.barnowlconf"; 263 } 264 $configfile ||= $ENV{HOME}."/.owlconf"; 350 if(!$configfile) { 351 if (-f get_config_dir() . "/init.pl") { 352 $configfile = get_config_dir() . "/init.pl"; 353 } elsif (-f $ENV{HOME} . "/.barnowlconf") { 354 $configfile = $ENV{HOME} . "/.barnowlconf"; 355 } else { 356 $configfile = $ENV{HOME}."/.owlconf"; 357 } 358 } 265 359 266 360 # populate global variable space for legacy owlconf files … … 319 413 =head2 new_variable_string NAME [{ARGS}] 320 414 321 Add a new owl variable, either an int, a bool, or a string, with the 415 =head2 new_variable_enum NAME [{ARGS}] 416 417 Add a new owl variable, either an int, a bool, a string, or an enum with the 322 418 specified name. 323 419 324 ARGS can optionally contain the following keys: 420 For new_variable_enum, ARGS is required to contain a validsettings key pointing 421 to an array reference. For all four, it can optionally contain the following 422 keys: 325 423 326 424 =over 4 … … 340 438 =back 341 439 440 In addition, new_variable_string optionally accepts a string validsettings 441 parameter, in case people want to set it to "<path>". 442 342 443 =cut 343 444 344 445 sub new_variable_int { 345 unshift @_, \&BarnOwl::Internal::new_variable_int, 0; 346 goto \&_new_variable; 446 my ($name, $args) = @_; 447 my $storage = defined($args->{default}) ? $args->{default} : 0; 448 BarnOwl::new_variable_full($name, { 449 %{$args}, 450 get_tostring => sub { "$storage" }, 451 set_fromstring => sub { 452 die "Expected integer" unless $_[0] =~ /^-?[0-9]+$/; 453 $storage = 0 + $_[0]; 454 }, 455 validsettings => "<int>", 456 takes_on_off => 0, 457 }); 347 458 } 348 459 349 460 sub new_variable_bool { 350 unshift @_, \&BarnOwl::Internal::new_variable_bool, 0; 351 goto \&_new_variable; 461 my ($name, $args) = @_; 462 my $storage = defined($args->{default}) ? $args->{default} : 0; 463 BarnOwl::new_variable_full($name, { 464 %{$args}, 465 get_tostring => sub { $storage ? "on" : "off" }, 466 set_fromstring => sub { 467 die "Valid settings are on/off" unless $_[0] eq "on" || $_[0] eq "off"; 468 $storage = $_[0] eq "on"; 469 }, 470 validsettings => "on,off", 471 takes_on_off => 1, 472 }); 352 473 } 353 474 354 475 sub new_variable_string { 355 unshift @_, \&BarnOwl::Internal::new_variable_string, ""; 356 goto \&_new_variable; 357 } 358 359 sub _new_variable { 360 my $func = shift; 361 my $default_default = shift; 476 my ($name, $args) = @_; 477 my $storage = defined($args->{default}) ? $args->{default} : ""; 478 BarnOwl::new_variable_full($name, { 479 # Allow people to override this one if they /reaaally/ want to for 480 # some reason. Though we still reserve the right to interpret this 481 # value in interesting ways for tab-completion purposes. 482 validsettings => "<string>", 483 %{$args}, 484 get_tostring => sub { $storage }, 485 set_fromstring => sub { $storage = $_[0]; }, 486 takes_on_off => 0, 487 }); 488 } 489 490 sub new_variable_enum { 491 my ($name, $args) = @_; 492 493 # Gather the valid settings. 494 die "validsettings is required" unless defined($args->{validsettings}); 495 my %valid; 496 map { $valid{$_} = 1 } @{$args->{validsettings}}; 497 498 my $storage = (defined($args->{default}) ? 499 $args->{default} : 500 $args->{validsettings}->[0]); 501 BarnOwl::new_variable_full($name, { 502 %{$args}, 503 get_tostring => sub { $storage }, 504 set_fromstring => sub { 505 die "Invalid input" unless $valid{$_[0]}; 506 $storage = $_[0]; 507 }, 508 validsettings => join(",", @{$args->{validsettings}}) 509 }); 510 } 511 512 =head2 new_variable_full NAME {ARGS} 513 514 Create a variable, in full generality. The keyword arguments have types below: 515 516 get_tostring : () -> string 517 set_fromstring : string -> int 518 -- optional -- 519 summary : string 520 description : string 521 validsettings : string 522 takes_on_off : int 523 524 The get/set functions are required. Note that the caller manages storage for the 525 variable. get_tostring/set_fromstring both convert AND store the value. 526 set_fromstring dies on failure. 527 528 If the variable takes parameters 'on' and 'off' (i.e. is boolean-looking), set 529 takes_on_off to 1. This makes :set VAR and :unset VAR work. set_fromstring will 530 be called with those arguments. 531 532 =cut 533 534 sub new_variable_full { 362 535 my $name = shift; 363 536 my $args = shift || {}; 364 537 my %args = ( 365 summary 538 summary => "", 366 539 description => "", 367 default => $default_default, 540 takes_on_off => 0, 541 validsettings => "<string>", 368 542 %{$args}); 369 $func->($name, $args{default}, $args{summary}, $args{description}); 543 544 die "get_tostring required" unless $args{get_tostring}; 545 die "set_fromstring required" unless $args{set_fromstring}; 546 547 # Strip off the bogus dummy argument. Aargh perl-Glib. 548 my $get_tostring_fn = sub { $args{get_tostring}->() }; 549 my $set_fromstring_fn = sub { 550 my ($dummy, $val) = @_; 551 # Translate from user-supplied die-on-failure callback to expected 552 # non-zero on error. Less of a nuisance than interacting with ERRSV. 553 eval { $args{set_fromstring}->($val) }; 554 # TODO: Consider changing B::I::new_variable to expect string|NULL with 555 # string as the error message. That can then be translated to a GError in 556 # owl_variable_set_fromstring. For now the string is ignored. 557 return ($@ ? -1 : 0); 558 }; 559 560 BarnOwl::Internal::new_variable($name, $args{summary}, $args{description}, $args{validsettings}, 561 $args{takes_on_off}, $get_tostring_fn, $set_fromstring_fn, undef); 370 562 } 371 563 … … 603 795 } 604 796 797 =head3 register_idle_watcher %ARGS 798 799 Call a callback whenever the amount of time the user becomes idle or comes 800 back from being idle. 801 802 You must include the following parameters: 803 804 =over 4 805 806 =item name 807 808 The name given to the idle watcher 809 810 =item after 811 812 How long the user must be idle, in seconds, before the callback is called. 813 If the value is too small, you may have spurious or inaccurate calls. 814 (The current lower limit is about 1 second.) 815 816 =item callback 817 818 The Perl subroutine that gets called when the user has been idle for C<AFTER> 819 seconds, or comes back from being idle. The subroutine is passed one parameter, 820 which is true if the user is currently idle, and false otherwise. 821 822 =back 823 824 This method returns a unique identifier which may be passed to 825 L<BarnOwl::unregister_idle_watcher>. 826 827 =cut 828 829 =head3 unregister_idle_watcher UNIQUE_ID [...] 830 831 Removed and returns the idle watcher specified by C<UNIQUE_ID>. 832 You may specify multiple unique ids. 833 834 =cut 835 836 my %idle_watchers; 837 tie %idle_watchers, 'Tie::RefHash'; 838 839 $BarnOwl::Hooks::wakeup->add(sub { 840 foreach my $idle_watcher (values %idle_watchers) { 841 _wakeup_idle_watcher($idle_watcher); 842 } 843 }); 844 845 sub _wakeup_idle_watcher { 846 my ($idle_watcher, $offset) = @_; 847 $offset = 0 unless defined $offset; 848 # go unidle 849 $idle_watcher->{idle_timer}->stop if $idle_watcher->{idle_timer}; 850 undef $idle_watcher->{idle_timer}; 851 $idle_watcher->{callback}->(0) if $idle_watcher->{is_idle}; 852 $idle_watcher->{is_idle} = 0; 853 854 # queue going idle 855 $idle_watcher->{idle_timer} = BarnOwl::Timer->new({ 856 name => $idle_watcher->{name}, 857 after => $idle_watcher->{after} - $offset, 858 cb => sub { 859 $idle_watcher->{is_idle} = 1; 860 $idle_watcher->{callback}->(1); 861 } 862 }); 863 } 864 865 sub register_idle_watcher { 866 my %args = (@_); 867 $idle_watchers{\%args} = \%args; 868 _wakeup_idle_watcher(\%args, BarnOwl::getidletime); # make sure to queue up the idle/unidle events from this idle watcher 869 return \%args; 870 } 871 872 sub unregister_idle_watcher { 873 my ($id) = @_; 874 $idle_watchers{$id}->{idle_timer}->stop if $idle_watchers{$id}->{idle_timer}; 875 return delete $idle_watchers{$id}; 876 } 877 605 878 # Stub for owl::startup / BarnOwl::startup, so it isn't bound to the 606 879 # startup command. This may be redefined in a user's configfile.
Note: See TracChangeset
for help on using the changeset viewer.