summaryrefslogtreecommitdiff
path: root/plugins
diff options
context:
space:
mode:
authorPřemysl Janouch <p.janouch@gmail.com>2016-02-29 03:11:33 +0100
committerPřemysl Janouch <p.janouch@gmail.com>2016-02-29 03:11:33 +0100
commitfb0b0c4cf068f243982cc650521b16e170f4d9ca (patch)
tree0ce7d93273c8343ce23fe6127e96951145e3a7d6 /plugins
parentf4925927352d37ac7e1d31c1350edf951f3889ae (diff)
downloadxK-fb0b0c4cf068f243982cc650521b16e170f4d9ca.tar.gz
xK-fb0b0c4cf068f243982cc650521b16e170f4d9ca.tar.xz
xK-fb0b0c4cf068f243982cc650521b16e170f4d9ca.zip
factoids: allow querying definitions by number
Diffstat (limited to 'plugins')
-rwxr-xr-xplugins/zyklonb/factoids42
1 files changed, 30 insertions, 12 deletions
diff --git a/plugins/zyklonb/factoids b/plugins/zyklonb/factoids
index 3b6be64..00a9fbf 100755
--- a/plugins/zyklonb/factoids
+++ b/plugins/zyklonb/factoids
@@ -72,9 +72,11 @@ my %db = %{db_load $db_path};
sub learn {
my ($respond, $input) = @_;
return &$respond("usage: <name> = <definition>")
- unless $input =~ /^([^=]+?)\s*=\s*(.+?)\s*$/;
+ unless $input =~ /^([^=]+?)(?:\s+(\d+))?\s*=\s*(.+?)\s*$/;
- my ($name, $definition) = ($1, $2);
+ my ($name, $number, $definition) = ($1, $2, $3);
+ return &$respond("trailing numbers in names are disallowed")
+ if defined $2;
$db{$name} = [] unless exists $db{$name};
my $entries = $db{$name};
@@ -86,6 +88,19 @@ sub learn {
db_save $db_path, \%db;
}
+sub check_number {
+ my ($respond, $name, $number) = @_;
+ my $entries = $db{$name};
+ if ($number > @$entries) {
+ &$respond(qq/"$name" has only ${\scalar @$entries} definitions/);
+ } elsif (not $number) {
+ &$respond("number must not be zero");
+ } else {
+ return 1;
+ }
+ return 0;
+}
+
sub forget {
my ($respond, $input) = @_;
return &$respond("usage: <name> <number>")
@@ -96,10 +111,7 @@ sub forget {
unless exists $db{$name};
my $entries = $db{$name};
- return &$respond(qq/"$name" has only ${\scalar @$entries} definitions/)
- if $number > @$entries;
- return &$respond("number must not be zero")
- unless $number;
+ return unless check_number $respond, $name, $number;
splice @$entries, --$number, 1;
&$respond("forgotten");
@@ -108,16 +120,22 @@ sub forget {
sub whatis {
my ($respond, $input) = @_;
- return &$respond("usage: <name>")
- unless $input =~ /^([^=]+?)\s*$/;
+ return &$respond("usage: <name> [<number>]")
+ unless $input =~ /^([^=]+?)(?:\s+(\d+))?\s*$/;
- my ($name) = ($1);
+ my ($name, $number) = ($1, $2);
return &$respond(qq/"$name" is undefined/)
unless exists $db{$name};
- my $i = 1;
- my $definition = join ", ", map { "#${\$i++} $_" } @{$db{$name}};
- &$respond(qq/"$name" is $definition/);
+ my $entries = $db{$name};
+ if (defined $number) {
+ return unless check_number $respond, $name, $number;
+ &$respond(qq/"$name" is #$number $entries->[$number - 1]/);
+ } else {
+ my $i = 1;
+ my $definition = join ", ", map { "#${\$i++} $_" } @{$entries};
+ &$respond(qq/"$name" is $definition/);
+ }
}
sub wildcard {