Changeset f3ac1ae for perl


Ignore:
Timestamp:
Feb 23, 2013, 7:29:16 PM (11 years ago)
Author:
David Benjamin <davidben@mit.edu>
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.
Message:
Merge 104a4eb952a68c48da10585deab68e021a648ca6 into bbe7d4a8ffa8c6c6b49ff192f54df4c385dfc1cb
File:
1 edited

Legend:

Unmodified
Added
Removed
  • perl/lib/BarnOwl.pm

    r374089a r104a4eb  
    1818                    new_command
    1919                    new_variable_int new_variable_bool new_variable_string
     20                    new_variable_enum
    2021                    quote redisplay);
    2122our %EXPORT_TAGS = (all => \@EXPORT_OK);
     
    402403=head2 new_variable_string NAME [{ARGS}]
    403404
    404 Add a new owl variable, either an int, a bool, or a string, with the
     405=head2 new_variable_enum NAME [{ARGS}]
     406
     407Add a new owl variable, either an int, a bool, a string, or an enum with the
    405408specified name.
    406409
    407 ARGS can optionally contain the following keys:
     410For new_variable_enum, ARGS is required to contain a validsettings key pointing
     411to an array reference. For all four, it can optionally contain the following
     412keys:
    408413
    409414=over 4
     
    423428=back
    424429
     430In addition, new_variable_string optionally accepts a string validsettings
     431parameter, in case people want to set it to "<path>".
     432
    425433=cut
    426434
    427435sub 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        });
    430448}
    431449
    432450sub 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        });
    435463}
    436464
    437465sub 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
     480sub 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
     504Create 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
     514The get/set functions are required. Note that the caller manages storage for the
     515variable. get_tostring/set_fromstring both convert AND store the value.
     516set_fromstring dies on failure.
     517
     518If the variable takes parameters 'on' and 'off' (i.e. is boolean-looking), set
     519takes_on_off to 1. This makes :set VAR and :unset VAR work. set_fromstring will
     520be called with those arguments.
     521
     522=cut
     523
     524sub new_variable_full {
    445525    my $name = shift;
    446526    my $args = shift || {};
    447527    my %args = (
    448         summary     => "",
     528        summary => "",
    449529        description => "",
    450         default     => $default_default,
     530        takes_on_off => 0,
     531        validsettings => "<string>",
    451532        %{$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);
    453552}
    454553
Note: See TracChangeset for help on using the changeset viewer.