Changeset f3ac1ae for perl/lib/BarnOwl.pm
- Timestamp:
- Feb 23, 2013, 7:29:16 PM (12 years ago)
- Parents:
- bbe7d4a (diff), 104a4eb (diff)
Note: this is a merge changeset, the changes displayed below correspond to the merge itself.
Use the (diff) links above to see all the changes relative to each parent. - File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
perl/lib/BarnOwl.pm
r374089a r104a4eb 18 18 new_command 19 19 new_variable_int new_variable_bool new_variable_string 20 new_variable_enum 20 21 quote redisplay); 21 22 our %EXPORT_TAGS = (all => \@EXPORT_OK); … … 402 403 =head2 new_variable_string NAME [{ARGS}] 403 404 404 Add a new owl variable, either an int, a bool, or a string, with the 405 =head2 new_variable_enum NAME [{ARGS}] 406 407 Add a new owl variable, either an int, a bool, a string, or an enum with the 405 408 specified name. 406 409 407 ARGS can optionally contain the following keys: 410 For new_variable_enum, ARGS is required to contain a validsettings key pointing 411 to an array reference. For all four, it can optionally contain the following 412 keys: 408 413 409 414 =over 4 … … 423 428 =back 424 429 430 In addition, new_variable_string optionally accepts a string validsettings 431 parameter, in case people want to set it to "<path>". 432 425 433 =cut 426 434 427 435 sub new_variable_int { 428 unshift @_, \&BarnOwl::Internal::new_variable_int, 0; 429 goto \&_new_variable; 436 my ($name, $args) = @_; 437 my $storage = defined($args->{default}) ? $args->{default} : 0; 438 BarnOwl::new_variable_full($name, { 439 %{$args}, 440 get_tostring => sub { "$storage" }, 441 set_fromstring => sub { 442 die "Expected integer" unless $_[0] =~ /^-?[0-9]+$/; 443 $storage = 0 + $_[0]; 444 }, 445 validsettings => "<int>", 446 takes_on_off => 0, 447 }); 430 448 } 431 449 432 450 sub new_variable_bool { 433 unshift @_, \&BarnOwl::Internal::new_variable_bool, 0; 434 goto \&_new_variable; 451 my ($name, $args) = @_; 452 my $storage = defined($args->{default}) ? $args->{default} : 0; 453 BarnOwl::new_variable_full($name, { 454 %{$args}, 455 get_tostring => sub { $storage ? "on" : "off" }, 456 set_fromstring => sub { 457 die "Valid settings are on/off" unless $_[0] eq "on" || $_[0] eq "off"; 458 $storage = $_[0] eq "on"; 459 }, 460 validsettings => "on,off", 461 takes_on_off => 1, 462 }); 435 463 } 436 464 437 465 sub new_variable_string { 438 unshift @_, \&BarnOwl::Internal::new_variable_string, ""; 439 goto \&_new_variable; 440 } 441 442 sub _new_variable { 443 my $func = shift; 444 my $default_default = shift; 466 my ($name, $args) = @_; 467 my $storage = defined($args->{default}) ? $args->{default} : ""; 468 BarnOwl::new_variable_full($name, { 469 # Allow people to override this one if they /reaaally/ want to for 470 # some reason. Though we still reserve the right to interpret this 471 # value in interesting ways for tab-completion purposes. 472 validsettings => "<string>", 473 %{$args}, 474 get_tostring => sub { $storage }, 475 set_fromstring => sub { $storage = $_[0]; }, 476 takes_on_off => 0, 477 }); 478 } 479 480 sub new_variable_enum { 481 my ($name, $args) = @_; 482 483 # Gather the valid settings. 484 die "validsettings is required" unless defined($args->{validsettings}); 485 my %valid; 486 map { $valid{$_} = 1 } @{$args->{validsettings}}; 487 488 my $storage = (defined($args->{default}) ? 489 $args->{default} : 490 $args->{validsettings}->[0]); 491 BarnOwl::new_variable_full($name, { 492 %{$args}, 493 get_tostring => sub { $storage }, 494 set_fromstring => sub { 495 die "Invalid input" unless $valid{$_[0]}; 496 $storage = $_[0]; 497 }, 498 validsettings => join(",", @{$args->{validsettings}}) 499 }); 500 } 501 502 =head2 new_variable_full NAME {ARGS} 503 504 Create a variable, in full generality. The keyword arguments have types below: 505 506 get_tostring : () -> string 507 set_fromstring : string -> int 508 -- optional -- 509 summary : string 510 description : string 511 validsettings : string 512 takes_on_off : int 513 514 The get/set functions are required. Note that the caller manages storage for the 515 variable. get_tostring/set_fromstring both convert AND store the value. 516 set_fromstring dies on failure. 517 518 If the variable takes parameters 'on' and 'off' (i.e. is boolean-looking), set 519 takes_on_off to 1. This makes :set VAR and :unset VAR work. set_fromstring will 520 be called with those arguments. 521 522 =cut 523 524 sub new_variable_full { 445 525 my $name = shift; 446 526 my $args = shift || {}; 447 527 my %args = ( 448 summary 528 summary => "", 449 529 description => "", 450 default => $default_default, 530 takes_on_off => 0, 531 validsettings => "<string>", 451 532 %{$args}); 452 $func->($name, $args{default}, $args{summary}, $args{description}); 533 534 die "get_tostring required" unless $args{get_tostring}; 535 die "set_fromstring required" unless $args{set_fromstring}; 536 537 # Strip off the bogus dummy argument. Aargh perl-Glib. 538 my $get_tostring_fn = sub { $args{get_tostring}->() }; 539 my $set_fromstring_fn = sub { 540 my ($dummy, $val) = @_; 541 # Translate from user-supplied die-on-failure callback to expected 542 # non-zero on error. Less of a nuisance than interacting with ERRSV. 543 eval { $args{set_fromstring}->($val) }; 544 # TODO: Consider changing B::I::new_variable to expect string|NULL with 545 # string as the error message. That can then be translated to a GError in 546 # owl_variable_set_fromstring. For now the string is ignored. 547 return ($@ ? -1 : 0); 548 }; 549 550 BarnOwl::Internal::new_variable($name, $args{summary}, $args{description}, $args{validsettings}, 551 $args{takes_on_off}, $get_tostring_fn, $set_fromstring_fn, undef); 453 552 } 454 553
Note: See TracChangeset
for help on using the changeset viewer.