snips/dwltags.pl

156 lines
4.1 KiB
Perl
Executable File

#!/usr/bin/env perl
use strict;
use warnings;
use Data::Dumper;
use Term::ANSIColor;
use FileHandle;
use Getopt::Long;
my $info = {};
my $tags_file = $ENV{'DWL_TAGS_FILE'};
my $dwl_log_h = FileHandle->new(">> " . $ENV{'DWL_LOG_FILE'});
die "Could not open $ENV{'DWL_LOG_FILE'} to append" unless (defined $dwl_log_h);
$dwl_log_h->autoflush();
my @mons = map { /^([\w-]+)/; $1 } grep { /^([\w-]+)/ } `wlr-randr`;
GetOptions("file=s" => \$tags_file)
or die("Command line usage error");
# Color scheme is only used in ANSI mode
# <class>_f/_g: class is used in JSON mode to be used in style.css for styling
my $colorscheme = {
'occ_f' => '#ffffff',
'occ_b' => '#111111',
'occsel_f' => '#ffffff',
'occsel_b' => '#aa4444',
'unocc_f' => '#ffffff',
'unocc_b' => '#888888',
'unoccsel_f' => '#ffffff',
'unoccsel_b' => '#aa8888',
'mon_f' => '#000000',
'mon_b' => '#777777',
'lay_f' => '#111111',
'lay_b' => '#77aaaa',
'title_f' => '#ffcccc',
'title_b' => '#111111'
# 'occ_f' => 'white',
# 'occ_b' => 'black',
# 'sel_f' => 'black',
# 'sel_b' => 'red',
# 'mon_f' => 'black',
# 'mon_b' => 'grey23',
# 'lay_f' => 'black',
# 'lay_b' => 'cyan',
# 'title_f' => 'red',
# 'title_b' => 'black'
};
print_log("DWLTAGS: $tags_file");
for my $mon (@mons) {
print_tags({
'selmon' => '1',
'mon' => $mon,
'tags' => '0 1',
'title' => '',
'layout' => '[]='
}, $tags_file . "." . $mon);
}
while (<>) {
my $line = $_;
chomp $line;
print STDOUT "$_";
if (exists $ENV{'DWL_LOG_VERBOSE'}) {
print_log("DWLTAGS [VV] : $line");
}
if (my ($mon, $key, $val) = $line =~ /^(\S+)\s+(\S+)\s(.*)$/) {
$info->{$key} = $val;
if ($key eq "layout") {
# Commit a status
# Assumption: layout is the last piece of information dumped by DWL
$info->{'mon'} = $mon;
print_tags($info, $tags_file.".".$mon);
$info = {};
}
} else {
print_log("DWL: Printed out of format - $line");
}
}
sub print_tags {
my ($info, $tags_file) = @_;
my $numtags = 10;
return if ($info->{'selmon'} ne "1");
if (my ($tuse, $tsel) = $info->{'tags'} =~ /(\d+)\s+(\d+)/) {
my $tags = "";
for (my $i = 0; $i < ($numtags-1); $i++) {
my $sel = "unocc";
if (($tuse & (1 << $i))) {
$sel = "occ";
}
if ($tsel & (1 << $i)) {
$sel .= "sel";
}
$tags .= getformatted($sel, " ". ($i+1) . " ");
}
if (exists $ENV{'DWLTAGS_SHOW_MON'}) {
$tags .= getformatted("mon", " $info->{'mon'} ");
}
$tags .= getformatted("lay", " $info->{'layout'} ");
$tags .= getformatted("title", " $info->{'title'} ");
my $fh = FileHandle->new("> " . $tags_file);
if (defined $fh) {
print $fh $tags . "\n";
$fh->close;
}
} else {
print_log("DWL: Printed out of format - tags");
}
}
sub getformatted {
my ($c, $txt) = @_;
if ((exists $ENV{'DWLTAGS_FMT'}) && ($ENV{'DWLTAGS_FMT'} eq "DZEN")) {
return dzen_colored($c, $txt);
} elsif ((exists $ENV{'DWLTAGS_FMT'}) && ($ENV{'DWLTAGS_FMT'} eq "ANSI")) {
return colored([$colorscheme->{$c . '_f'} . " on_" . $colorscheme->{$c . '_b'}], $txt);
} elsif ((exists $ENV{'DWLTAGS_FMT'}) && ($ENV{'DWLTAGS_FMT'} eq "JSON")) {
return '{"text": "' . $txt . '", "class": "dwltags-' . $c . '"}';
} else {
if ($c eq "sel") {
$txt =~ s/\s+//g;
$txt = "[$txt]";
}
return $txt;
}
}
sub dzen_colored {
my ($class, $txt) = @_;
my $fc = $colorscheme->{$class . '_f'};
my $bc = $colorscheme->{$class . '_b'};
# return colored([$colorscheme->{$c . '_f'} . " on_" . $colorscheme->{$c . '_b'}], $txt);
return "^fg(" . $fc . ")^bg(" . $bc . ")" . $txt . "^fg()^bg()";
}
sub print_log {
my ($line) = @_;
if (defined $dwl_log_h) {
print $dwl_log_h $line . "\n";
}
}