]> git.99rst.org Git - irssi-scripts.irssi.org.git/commitdiff
[theme] v0.03, theme.yaml, xterm fg- & bg-color
authorbw1 <redacted>
Sat, 20 Jul 2019 14:18:46 +0000 (16:18 +0200)
committerbw1 <redacted>
Sat, 20 Jul 2019 14:18:46 +0000 (16:18 +0200)
scripts/theme.pl

index ff363d24d9ba52d19b66351cecd20ddb9292b90d..d15e4814ef4f2523411381ae57661612b347835e 100644 (file)
@@ -4,10 +4,12 @@ use File::Basename;
 use File::Fetch;
 use File::Glob ':bsd_glob';
 use Getopt::Long qw/GetOptionsFromString/;
+use Storable qw/store_fd fd_retrieve/;
+use YAML::XS;
 
 use Irssi;
 
-$VERSION = '0.02';
+$VERSION = '0.03';
 %IRSSI = (
     authors    => 'bw1',
     contact    => 'bw1@aol.at',
@@ -16,7 +18,7 @@ $VERSION = '0.02';
     license    => 'Public Domain',
     url                => 'https://scripts.irssi.org/',
     changed    => '2019-07-02',
-    modules => 'File::Basename File::Fetch File::Glob Getopt::Long',
+    modules => 'File::Basename File::Fetch File::Glob Getopt::Long Storable YAML::XS',
     commands=> 'theme',
 );
 
@@ -35,13 +37,24 @@ my $help = << "END";
   -reload|-r    reload the dir
   -get|-g       get a theme form a website
   -list|-l      list theme in dir
-  -help|-l
+  -update|-u    download themes.yaml
+  -info|-i      print info
+  -help|-h
 %9description%9
   $IRSSI{description}
+%9color%9
+  the script can set
+    VT100 text foreground color
+    VT100 text background color
+  tested with xterm, konsole, lxterm
+%9See also%9
+  https://irssi-import.github.io/themes/
+  https://invisible-island.net/xterm/ctlseqs/ctlseqs.html#h2-Operating-System-Commands
 END
 
+my (%themes, @dtl);
 my (@tl, $count);
-my ($show, $update, $get, $list, $phelp);
+my ($show, $update, $get, $list, $phelp, $info, $yupdate);
 my %options = (
        'n' => sub{ $count++; $update=1},
        'next' => sub{ $count++; $update=1},
@@ -57,6 +70,10 @@ my %options = (
        'list' => \$list,
        'h' => \$phelp,
        'help' => \$phelp,
+       'u' => \$yupdate,
+       'update' => \$yupdate,
+       'i:s' => \$info,
+       'info:s' => \$info,
 );
 
 my $lorem = << 'END';
@@ -71,6 +88,41 @@ sea takimata sanctus est Lorem ipsum dolor sit amet.
 END
 
 my ($theme_source, $theme_local);
+my %bg_process= ();
+
+sub background {
+       my ($cmd) =@_;
+       my ($fh_r, $fh_w);
+       pipe $fh_r, $fh_w;
+       my $pid = fork();
+       if ($pid ==0 ) {
+               my @res;
+               @res= &{$cmd->{cmd}}(@{$cmd->{args}});
+               store_fd \@res, $fh_w;
+               close $fh_w;
+               POSIX::_exit(1);
+       } else {
+               $cmd->{fh_r}=$fh_r;
+               Irssi::pidwait_add($pid);
+               $bg_process{$pid}=$cmd;
+       }
+}
+
+sub sig_pidwait {
+       my ($pid, $status) = @_;
+       if (exists $bg_process{$pid}) {
+               my @res= @{ fd_retrieve($bg_process{$pid}->{fh_r})};
+               $bg_process{$pid}->{res}=[@res];
+               if (exists $bg_process{$pid}->{last}) {
+                       foreach my $p (@{$bg_process{$pid}->{last}}) {
+                               &$p($bg_process{$pid});
+                       }
+               } else {
+                       Irssi::print(join(" ",@res), MSGLEVEL_CLIENTCRAP);
+               }
+               delete $bg_process{$pid};
+       }
+}
 
 sub cmd_show {
        my ($args, $server, $witem)=@_;
@@ -119,12 +171,48 @@ sub core_printformat_module_w {
   }
 }
 
+sub set_color {
+       my ($fg, $bg) = @_;
+       if ($ENV{'TERM'} =~ m/^xterm/) {
+               if ( defined $fg && defined $bg) {
+                       print STDERR "\033]10;$fg\a";
+                       print STDERR "\033]11;$bg\a";
+               } else {
+                       print STDERR "\033]110\a";
+                       print STDERR "\033]111\a";
+               }
+       }
+}
+
+sub get_theme {
+       my ($args)=@_;
+       local $File::Fetch::WARN=0;
+       $get.= '.theme' if $get !~ m/\.theme/;
+       my $ff= File::Fetch->new(uri => $theme_source.$get);
+       my $where = $ff->fetch( to => $theme_local ) or
+               return  "Error: $theme_source$get not found";
+       return "$get downloaded.";
+}
+
+sub get_yaml {
+       local $File::Fetch::WARN=0;
+       my $get='themes.yaml';
+       if (-e $theme_local.$get) {
+               unlink $theme_local.$get;
+       }
+       my $ff= File::Fetch->new(uri => $theme_source.$get);
+       my $where = $ff->fetch( to => $theme_local ) or
+               return  "Error: $theme_source$get not found";
+       return "$get downloaded.";
+}
+
 sub cmd_set {
        my ($args, $server, $witem)=@_;
        my $t =  $tl[$count];
        if (defined $t) {
                Irssi::settings_set_str('theme',$t);
                Irssi::signal_emit('setup changed');
+               set_color($themes{$t}->{fgColor}, $themes{$t}->{bgColor});
        }
 }
 
@@ -157,9 +245,26 @@ sub cmd {
                $show = undef;
        }
        if (defined $get) {
-               cmd_get($args, $server, $witem);
+               my $cmd;
+               $cmd->{cmd}=\&get_theme;
+               $cmd->{args}=[$args];
+               $cmd->{last}=[
+                       \&init,
+                       \&print_result,
+               ];
+               background( $cmd );
                $get = undef;
        }
+       if (defined $yupdate) {
+               my $cmd;
+               $cmd->{cmd}=\&get_yaml;
+               $cmd->{last}=[
+                       \&init,
+                       \&print_result,
+               ];
+               background( $cmd );
+               $yupdate = undef;
+       }
        if (defined $list) {
         my $c=0;
                foreach (@tl) {
@@ -172,12 +277,26 @@ sub cmd {
                }
                $list = undef;
        }
+       if (defined $info) {
+               cmd_info($args, $server, $witem);
+               $info = undef;
+       }
        if (defined $phelp || $args eq '' ) {
                cmd_help($IRSSI{name}, $server, $witem);
                $phelp = undef;
        }
 }
 
+sub cmd_info {
+       my ($args, $server, $witem)=@_;
+       Irssi::print("Info: $info", MSGLEVEL_CLIENTCRAP);
+       if (exists $themes{$info}) {
+               Irssi::print(Dump($themes{$info}), MSGLEVEL_CLIENTCRAP);
+       } elsif (exists $themes{$tl[$count]}) {
+               Irssi::print(Dump($themes{$tl[$count]}), MSGLEVEL_CLIENTCRAP);
+       }
+}
+
 sub cmd_help {
        my ($args, $server, $witem)=@_;
        $args=~ s/\s+//g;
@@ -187,17 +306,6 @@ sub cmd_help {
        }
 }
 
-sub cmd_get {
-       my ($args, $server, $witem)=@_;
-       local $File::Fetch::WARN=0;
-       $get.= '.theme' if $get !~ m/\.theme/;
-       my $ff= File::Fetch->new(uri => $theme_source.$get);
-       my $where = $ff->fetch( to => $theme_local ) or
-               Irssi::print("Error: $theme_source$get not found",
-                       MSGLEVEL_CLIENTCRAP);
-       init();
-}
-
 sub sig_setup_changed {
        $theme_source= Irssi::settings_get_str($IRSSI{name}.'_source');
        $theme_source.= '/' if $theme_source !~ m#/$#;
@@ -206,11 +314,22 @@ sub sig_setup_changed {
        $theme_local.= '/' if $theme_local !~ m#/$#;
 }
 
+sub print_result {
+       my ($cmd) = @_;
+       if (defined $cmd->{res}->[0]) {
+               Irssi::print($cmd->{res}->[0] , MSGLEVEL_CLIENTCRAP);
+       }
+}
+
 sub do_complete {
        my ($strings, $window, $word, $linestart, $want_space) = @_;
        return unless $linestart =~ m#^/$IRSSI{name}#;
        return if $word =~ m#^-#;
-       @$strings = grep { m/^$word/} @tl;
+       if ( $linestart !~ m/(-g|-get|-i|-info)/ ) {
+               @$strings = grep { m/^$word/} @tl;
+       } else {
+               @$strings = grep { m/^$word/} @dtl;
+       }
        Irssi::signal_stop;
 }
 
@@ -227,18 +346,47 @@ sub init {
                $c++;
        }
        $lorem =~ s/\n/ /g;
+       if (-e $p1.'/themes.yaml') {
+               @dtl=undef;
+               my @l;
+               open my $fi, '<',$p1.'/themes.yaml';
+               my $syml= do {local $/; <$fi>};
+               close $fi;
+               eval {
+                       @l = @{Load($syml)};
+               };
+               if (length($@) >0) {
+                       print $@;
+               } else {
+                       foreach my $e (@l) {
+                               $themes{$e->{name}}=$e;
+                               push @dtl, $e->{name};
+                       }
+               }
+       }
 }
 
 Irssi::signal_add_first('complete word',  \&do_complete);
 Irssi::signal_add('setup changed', \&sig_setup_changed);
+Irssi::signal_add('pidwait', \&sig_pidwait);
 
 Irssi::settings_add_str($IRSSI{name} ,$IRSSI{name}.'_source', 'https://irssi-import.github.io/themes/');
 Irssi::settings_add_str($IRSSI{name} ,$IRSSI{name}.'_local', Irssi::get_irssi_dir());
 
 Irssi::command_bind($IRSSI{name}, \&cmd);
-my @opt=map {s/=.*$//, $_}  keys %options;
+my @opt=map {s/[=:].*$//, $_}  keys %options;
 Irssi::command_set_options($IRSSI{name}, join(" ", @opt));
 Irssi::command_bind('help', \&cmd_help);
 
 init();
 sig_setup_changed();
+
+if (!(-e $theme_local.'themes.yaml')) {
+       my $cmd;
+       $cmd->{cmd}=\&get_yaml;
+       $cmd->{last}=[
+               \&init,
+               \&print_result,
+       ];
+       background( $cmd );
+}
git clone https://git.99rst.org/PROJECT