Skip to content
Browse files

separate notifier from writer

  • Loading branch information...
1 parent 557bd83 commit 83adc8026ce3e5a91127165844094d513d7d6082 @yoshikaw committed Apr 23, 2011
Showing with 11 additions and 16 deletions.
  1. +11 −16 clipboard_text_listener.pl
View
27 clipboard_text_listener.pl
@@ -23,22 +23,22 @@ package WriterFactory;
);
sub create {
my ($self, $type) = @_;
- my $notifier = _createNotifier();
if ($type eq 'clipboard') {
if ($^O =~ /^(MSWin32|cygwin)$/) {
eval { require Win32::Clipboard; };
unless ($@) {
- return Writer::Clipboard::Win32->new($notifier);
+ return Writer::Clipboard::Win32->new;
}
}
if (my $cmdline = _get_cmdline($command{$^O}->{clipboard})) {
- return Writer::Clipboard::Cmd->new($notifier, $cmdline);
+ return Writer::Clipboard::Cmd->new($cmdline);
}
}
print "Platform: $^O is not supported yet. echo received text only.\n";
- return Writer->new($notifier);
+ return Writer->new;
}
- sub _createNotifier {
+ sub createNotifier {
+ my ($self, $type) = @_;
if ($^O =~ /^(MSWin32|cygwin)$/) {
eval { require Win32::GUI; };
unless ($@) {
@@ -105,26 +105,21 @@ package Command;
package Writer;
{
sub new {
- my ($class, $notifier) = @_;
- return bless { notifier => $notifier }, $class;
+ my ($class) = @_;
+ return bless {}, $class;
}
sub write {
my ($self, $text) = @_;
print "$text\n";
}
- sub notify {
- my ($self, $info) = @_;
- $self->{notifier}->notify($info);
- }
}
package Writer::Clipboard::Win32;
use base qw(Writer);
{
sub new {
- my ($class, $notifier) = @_;
+ my ($class) = @_;
my $self = bless {
- notifier => $notifier,
clipboard => Win32::Clipboard(),
}, $class;
$self
@@ -139,9 +134,8 @@ package Writer::Clipboard::Cmd;
use base qw(Writer);
{
sub new {
- my ($class, $notifier, $cmdline) = @_;
+ my ($class, $cmdline) = @_;
my $self = bless {
- notifier => $notifier,
command => Command->new($cmdline),
}, $class;
$self
@@ -202,6 +196,7 @@ package TextWriter;
my ($class, $opts) = @_;
my $self = bless {
writer => {},
+ notifier => WriterFactory->createNotifier(),
encoding => $opts->{encoding},
verbose => $opts->{verbose},
nlength => $opts->{nlength} || 40,
@@ -227,7 +222,7 @@ package TextWriter;
print encode($self->{termenc}, decode($text_encoding, "$text\n"))
if ref $writer ne 'Writer';
}
- $writer->notify({
+ $self->{notifier}->notify({
title => sprintf('(%s) %s', length($enc_text), ref $writer),
text => substr($text, 0, $self->{nlength}),
enc_text => substr($enc_text, 0, $self->{nlength}), # for Win32

0 comments on commit 83adc80

Please sign in to comment.
Something went wrong with that request. Please try again.