Permalink
Browse files

RoomFilter: treat filters as regular expressions

You can now do "camptail -r danger.room"" instead of "The
Danger Room" (or even just "-r danger"). Which is way
handier.
  • Loading branch information...
1 parent 3031d78 commit a04f0d89fa1fee21c4c883f7e873f7d023d5b63e @peff committed Mar 28, 2011
Showing with 22 additions and 2 deletions.
  1. +22 −2 RoomFilter.pm
View
@@ -3,13 +3,33 @@ use strict;
sub new {
my $self = bless {}, shift;
- $self->{index} = { map { $_ => 1 } @_ };
+ foreach (@_) {
+ if (/^[0-9]+$/) {
+ $self->{id}->{$_} = 1;
+ }
+ else {
+ push @{$self->{re}}, qr/$_/i;
+ }
+ }
return $self;
}
sub filter {
my $self = shift;
- return grep { $self->{index}->{$_->name} || $self->{index}->{$_->id} } @_;
+ return grep { $self->match($_) } @_;
+}
+
+sub match {
+ my ($self, $room) = @_;
+
+ return 1 if $self->{id}->{$room->id};
+
+ my $name = $room->name;
+ foreach my $re (@{$self->{re}}) {
+ return 1 if $name =~ $re;
+ }
+
+ return 0;
}
1;

0 comments on commit a04f0d8

Please sign in to comment.