Changes in perl/lib/BarnOwl.pm [104a4eb:374089a]
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
perl/lib/BarnOwl.pm
r104a4eb r374089a 18 18 new_command 19 19 new_variable_int new_variable_bool new_variable_string 20 new_variable_enum21 20 quote redisplay); 22 21 our %EXPORT_TAGS = (all => \@EXPORT_OK); … … 403 402 =head2 new_variable_string NAME [{ARGS}] 404 403 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 404 Add a new owl variable, either an int, a bool, or a string, with the 408 405 specified name. 409 406 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: 407 ARGS can optionally contain the following keys: 413 408 414 409 =over 4 … … 428 423 =back 429 424 430 In addition, new_variable_string optionally accepts a string validsettings431 parameter, in case people want to set it to "<path>".432 433 425 =cut 434 426 435 427 sub new_variable_int { 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 }); 428 unshift @_, \&BarnOwl::Internal::new_variable_int, 0; 429 goto \&_new_variable; 448 430 } 449 431 450 432 sub new_variable_bool { 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 }); 433 unshift @_, \&BarnOwl::Internal::new_variable_bool, 0; 434 goto \&_new_variable; 463 435 } 464 436 465 437 sub new_variable_string { 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 { 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; 525 445 my $name = shift; 526 446 my $args = shift || {}; 527 447 my %args = ( 528 summary => "",448 summary => "", 529 449 description => "", 530 takes_on_off => 0, 531 validsettings => "<string>", 450 default => $default_default, 532 451 %{$args}); 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); 452 $func->($name, $args{default}, $args{summary}, $args{description}); 552 453 } 553 454
Note: See TracChangeset
for help on using the changeset viewer.