From 501309887a7499b91c2b9fa8f76180c2738d7990 Mon Sep 17 00:00:00 2001 From: Tom Feist Date: Sat, 24 Jul 2010 17:35:43 +0100 Subject: something confusing going on here, have I got nested repos? --- history-search/frog_reload.pl | 99 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 99 insertions(+) create mode 100644 history-search/frog_reload.pl (limited to 'history-search/frog_reload.pl') diff --git a/history-search/frog_reload.pl b/history-search/frog_reload.pl new file mode 100644 index 0000000..678c87d --- /dev/null +++ b/history-search/frog_reload.pl @@ -0,0 +1,99 @@ +use strict; +use warnings; + +use Irssi; +use File::ChangeNotify(); +use File::Spec (); +use File::Basename qw(basename); + +#my $THIS_SCRIPT = basename __FILE__; + +my $irssi_dir = Irssi::get_irssi_dir(); +my @watch_dirs = ($irssi_dir, $irssi_dir . '/scripts', + $irssi_dir . '/scripts/autorun'); + +my $watcher = File::ChangeNotify->instantiate_watcher + ( + directories => \@watch_dirs, + filter => qr/\.(?:pl|py)$/, + ); + +my @watchers = File::ChangeNotify->usable_classes(); +print "Started reloader watching: ", join(", ", @watch_dirs), " using $watchers[0]"; + +my %action_for = ( + create => sub { + my ($path) = @_; + Irssi::print ("CREATE: Loading $path"); + load_script($path); + }, + + modify => sub { + my ($path) = @_; + Irssi::print ("MODIFY: reloading $path"); + reload($path); + }, + + delete => sub { + my ($path) = @_; + Irssi::print ("DELETE: Unloading $path"); + unload_script($path); + }, +); + +#TODO: change me back. +Irssi::timeout_add(3000, \&timer_sub, undef); + +sub timer_sub { + print "Timer sub called"; + my @new_events = $watcher->new_events; + for my $event (@new_events) { + + print "Handling event: ", $event->type, " path: ", $event->path; + + if (my $callback = $action_for{$event->type}) { + $callback->($event->path); + } + } +} + +sub reload { + my ($path) = @_; + + unload_script($path); + load_script($path); +} + +sub unload_script { + my ($script_path) = @_; + my $name = filepath_to_script($script_path); + + if (script_is_loaded($name)) { + Irssi::print ("unloading $name..."); + Irssi::command("script unload $name"); + } +} + +sub load_script { + my ($script_path) = @_; + Irssi::command("script load \"$script_path\""); +} + +sub filepath_to_script { + my ($path) = @_; + + my $name = basename $path; + $name =~ s/\.pl$//i; + + return $name; +} + +sub script_is_loaded { + my $name = shift; + print "Checking if $name is loaded"; + no strict 'refs'; + my $retval = defined %{ "Irssi::Script::${name}::" }; + use strict 'refs'; + + return $retval; +} -- cgit v1.2.3