]> git.99rst.org Git - irssi-scripts.irssi.org.git/commitdiff
pull request - graze.pl (#819)
authorMason Loring Bliss <redacted>
Mon, 8 Aug 2022 20:13:05 +0000 (16:13 -0400)
committerGitHub <redacted>
Mon, 8 Aug 2022 20:13:05 +0000 (22:13 +0200)
* initial

* s/my/our/

* do not let saved settings mutate needlessly

scripts/graze.pl [new file with mode: 0644]

diff --git a/scripts/graze.pl b/scripts/graze.pl
new file mode 100644 (file)
index 0000000..6172773
--- /dev/null
@@ -0,0 +1,132 @@
+use strict;
+use warnings;
+use Irssi;
+
+our $VERSION = '1.00';
+our %IRSSI = (
+    authors => 'Mason Loring Bliss',
+    contact => 'mason@blisses.org',
+    name => 'Graze',
+    description => 'Follow set order in seeing active channels.',
+    license => 'CC0'
+);
+
+Irssi::settings_add_str("graze", "graze_priority", "oftc libera magnet efnet");
+
+sub graze
+{
+    my ($command, $message) = split /\s/, shift, 2;
+    $command //= "";
+    $message //= "";
+
+    if ($command eq "next") {
+        my %by_refnum;
+        for (Irssi::windows()) {
+            $by_refnum{$$_{refnum}}{data_level} = $$_{data_level};
+            if ($by_refnum{$$_{refnum}}{data_level} > 2) {
+                $by_refnum{$$_{refnum}}{data_level}
+                  += ($$_{hilight_color} eq '' ? 1 : 0);
+            }
+            $by_refnum{$$_{refnum}}{chatnet} = $$_{active_server}{chatnet};
+            $by_refnum{$$_{refnum}}{window} = $_;
+        }
+
+        my @windowkeys;
+        for (sort {$a <=> $b} keys %by_refnum) {
+            push @windowkeys, $_;
+        }
+
+        my @priorities = split(/ /, Irssi::settings_get_str('graze_priority'));
+
+        # Find a priority highlight.
+        my $found = 0;
+        for (@windowkeys) {
+            if ($by_refnum{$_}{data_level} == 4) {
+                $by_refnum{$_}{window}->set_active;
+                $found = 1;
+                last;
+            }
+        }
+        
+        # Find a non-priority highlight.
+        if (! $found) {
+            for (@windowkeys) {
+                if ($by_refnum{$_}{data_level} == 3) {
+                    $by_refnum{$_}{window}->set_active;
+                    $found = 1;
+                    last;
+                }
+            }
+        }
+
+        # Find an active window in a preferred network.
+        if (! $found) {
+            PREFERRED: for (@windowkeys) {
+                if ($by_refnum{$_}{data_level} == 2) {
+                    for my $priority (@priorities) {
+                          if ($by_refnum{$_}{chatnet} eq $priority) {
+                              $by_refnum{$_}{window}->set_active;
+                              $found = 1;
+                              last PREFERRED;
+                          }
+                    }
+                }
+            }
+        }
+
+        # Find an active window.
+        if (! $found) {
+            for (@windowkeys) {
+                if ($by_refnum{$_}{data_level} == 2) {
+                    $by_refnum{$_}{window}->set_active;
+                    $found = 1;
+                    last;
+                }
+            }
+        }
+
+    } elsif ($command eq "prioritize") {
+        my %prio;
+        for (split(/ /, Irssi::settings_get_str('graze_priority'))) {
+            $prio{$_} = 1;
+        }
+        $prio{$message} = 1;
+        Irssi::settings_set_str("graze_priority", join(' ', sort keys %prio));
+        print "priorities:  ${\Irssi::settings_get_str('graze_priority')}";
+
+    } elsif ($command eq "deprioritize") {
+        my %prio;
+        for (split(/ /, Irssi::settings_get_str('graze_priority'))) {
+            $prio{$_} = 1;
+        }
+        delete $prio{$message};
+        Irssi::settings_set_str("graze_priority", join(' ', sort keys %prio));
+        print "priorities:  ${\Irssi::settings_get_str('graze_priority')}";
+
+    } else {
+        help("graze");
+        return;
+    }
+}
+
+sub help
+{
+    if ($_[0] =~ /^graze\b/i) {
+        print "-" x 60;
+        print "graze next";
+        print "    move to next interesting window";
+        print "graze prioritize foo";
+        print "    add chatnet foo to priority chatnet list";
+        print "graze deprioritize foo";
+        print "    remove chatnet foo from priority chatnet list";
+        print "(Active channels in priority chatnets will show up first.)";
+        print "";
+        print "priorities:  ${\Irssi::settings_get_str('graze_priority')}";
+    }
+}
+
+Irssi::command_bind "graze" => \&graze;
+Irssi::command_bind "graze next" => \&graze;
+Irssi::command_bind "graze prioritize" => \&graze;
+Irssi::command_bind "graze deprioritize" => \&graze;
+Irssi::command_bind "help" => \&help;
git clone https://git.99rst.org/PROJECT