2021-05-16 18:31:19 +00:00
|
|
|
#!/usr/bin/env perl
|
|
|
|
|
|
|
|
use strict;
|
|
|
|
use warnings;
|
|
|
|
use Data::Dumper;
|
|
|
|
use Term::ANSIColor;
|
|
|
|
use FileHandle;
|
2021-05-26 05:23:50 +00:00
|
|
|
use Getopt::Long;
|
2021-05-16 18:31:19 +00:00
|
|
|
|
|
|
|
my $info = {};
|
2021-05-29 22:40:00 +00:00
|
|
|
my $tags_file = $ENV{'DWL_TAGS_FILE'};
|
2021-05-26 05:23:50 +00:00
|
|
|
|
2021-06-01 05:23:35 +00:00
|
|
|
my @mons = map { /^([\w-]+)/; $1 } grep { /^([\w-]+)/ } `wlr-randr`;
|
|
|
|
|
2021-05-26 05:23:50 +00:00
|
|
|
GetOptions("file=s" => \$tags_file)
|
|
|
|
or die("Command line usage error");
|
2021-05-16 18:31:19 +00:00
|
|
|
|
|
|
|
# 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 = {
|
|
|
|
'norm_f' => '#ffffff',
|
|
|
|
'norm_b' => '#111111',
|
|
|
|
'sel_f' => '#ffffff',
|
|
|
|
'sel_b' => '#aa4444',
|
|
|
|
'mon_f' => '#000000',
|
|
|
|
'mon_b' => '#777777',
|
|
|
|
'lay_f' => '#111111',
|
|
|
|
'lay_b' => '#77aaaa',
|
|
|
|
'title_f' => '#ffcccc',
|
|
|
|
'title_b' => '#111111'
|
|
|
|
# 'norm_f' => 'white',
|
|
|
|
# 'norm_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'
|
|
|
|
};
|
|
|
|
|
2021-05-26 05:23:50 +00:00
|
|
|
print_log("DWLTAGS: $tags_file");
|
|
|
|
|
2021-06-01 05:23:35 +00:00
|
|
|
for my $mon (@mons) {
|
|
|
|
print_tags({
|
|
|
|
'selmon' => '1',
|
|
|
|
'mon' => $mon,
|
|
|
|
'tags' => '1 1',
|
|
|
|
'title' => '',
|
|
|
|
'layout' => '[]='
|
|
|
|
}, $tags_file . "." . $mon);
|
|
|
|
}
|
2021-05-16 18:31:19 +00:00
|
|
|
|
|
|
|
while (<>) {
|
|
|
|
my $line = $_;
|
|
|
|
chomp $line;
|
|
|
|
|
2021-05-26 05:23:50 +00:00
|
|
|
# print_log("DWLTAGS [VV] : $line");
|
|
|
|
|
2021-05-16 18:31:19 +00:00
|
|
|
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;
|
2021-06-01 05:23:35 +00:00
|
|
|
print_tags($info, $tags_file.".".$mon);
|
2021-05-16 18:31:19 +00:00
|
|
|
|
|
|
|
$info = {};
|
|
|
|
}
|
|
|
|
} else {
|
2021-05-26 05:23:50 +00:00
|
|
|
print_log("DWL: Printed out of format - $line");
|
2021-05-16 18:31:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
sub print_tags {
|
2021-05-26 05:23:50 +00:00
|
|
|
my ($info, $tags_file) = @_;
|
2021-05-16 18:31:19 +00:00
|
|
|
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; $i++) {
|
|
|
|
my $sel = "norm";
|
|
|
|
if (($tuse & (1 << $i))) {
|
|
|
|
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'} ");
|
|
|
|
|
2021-05-26 05:23:50 +00:00
|
|
|
my $fh = FileHandle->new("> " . $tags_file);
|
2021-05-16 18:31:19 +00:00
|
|
|
if (defined $fh) {
|
|
|
|
print $fh $tags . "\n";
|
|
|
|
$fh->close;
|
|
|
|
}
|
|
|
|
} else {
|
2021-05-26 05:23:50 +00:00
|
|
|
print_log("DWL: Printed out of format - tags");
|
2021-05-16 18:31:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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()";
|
|
|
|
}
|
2021-05-26 05:23:50 +00:00
|
|
|
|
|
|
|
sub print_log {
|
|
|
|
my ($line) = @_;
|
2021-05-29 22:40:00 +00:00
|
|
|
my $fh = FileHandle->new(">> " . $ENV{'DWL_LOG_FILE'});
|
2021-05-26 05:23:50 +00:00
|
|
|
if (defined $fh) {
|
|
|
|
print $fh $line . "\n";
|
|
|
|
$fh->close;
|
|
|
|
}
|
|
|
|
}
|