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',
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',
);
-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},
'list' => \$list,
'h' => \$phelp,
'help' => \$phelp,
+ 'u' => \$yupdate,
+ 'update' => \$yupdate,
+ 'i:s' => \$info,
+ 'info:s' => \$info,
);
my $lorem = << 'END';
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)=@_;
}
}
+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});
}
}
$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) {
}
$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;
}
}
-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#/$#;
$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;
}
$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 );
+}