1

i3status-timers.json-io: use external config for buttons

This commit is contained in:
eleksir
2020-03-15 15:06:18 +03:00
parent 4724fd9533
commit cad1f400df
2 changed files with 111 additions and 57 deletions

72
config.json Normal file
View File

@@ -0,0 +1,72 @@
[
{
"full_text" : "u",
"name" : "urxvt",
"cmd" : "urxvtc"
},
{
"full_text" : "l",
"name" : "lxterm",
"cmd" : "lxterminal"
},
{
"full_text" : "f",
"name" : "firefox",
"cmd" : "firefox"
},
{
"full_text" : "v",
"name" : "vivaldi",
"cmd" : "vivaldi"
},
{
"full_text" : "c",
"name" : "chromium",
"cmd" : "chromium"
},
{
"full_text" : "o",
"name" : "opera",
"cmd" : "opera"
},
{
"full_text" : "t",
"name" : "thunderbird",
"cmd" : "thunderbird"
},
{
"full_text" : "t",
"name" : "telegram",
"cmd" : "Telegram"
},
{
"full_text" : "i",
"name" : "icq",
"cmd" : "icq"
},
{
"full_text" : "s",
"name" : "slack",
"cmd" : "slack"
},
{
"full_text" : "z",
"name" : "zim",
"cmd" : "zim"
},
{
"full_text" : "m",
"name" : "mousepad",
"cmd" : "mousepad"
},
{
"full_text" : "c",
"name" : "code",
"cmd" : "code"
},
{
"full_text" : "k",
"name" : "keepassxc",
"cmd" : "keepassxc"
}
]

View File

@@ -8,12 +8,13 @@ use 5.10.0;
use utf8; use utf8;
use JSON::XS; use JSON::XS;
use threads; use threads;
use Data::Dumper; #use Data::Dumper;
binmode STDOUT; binmode STDOUT;
binmode STDERR; binmode STDERR;
sub input_thread(); sub input_thread();
sub readconf();
sub gettimestr(); sub gettimestr();
sub getlastr(); sub getlastr();
sub getbatterychargestr2(); sub getbatterychargestr2();
@@ -33,6 +34,10 @@ $SIG{"STOP"} = sub { $read = 0 };
my $slow_timer = 360; # 0.333 * 360 ~= 2 min my $slow_timer = 360; # 0.333 * 360 ~= 2 min
my $onesec_timer = 3; # 0.333 * 3 ~= 1 sec my $onesec_timer = 3; # 0.333 * 3 ~= 1 sec
# read config
my ($C, %CMD) = readconf();
my $buttonsamount = @{$C};
my @months = ('Янв', 'Фев', 'Мар', 'Апр', 'Май', 'Июн', 'Июл', 'Авг', 'Сен', 'Окт', 'Ноя', 'Дек'); my @months = ('Янв', 'Фев', 'Мар', 'Апр', 'Май', 'Июн', 'Июл', 'Авг', 'Сен', 'Окт', 'Ноя', 'Дек');
my @wdays = ('Вс', 'Пн', 'Вт', 'Ср', 'Чт', 'Пт', 'Сб'); my @wdays = ('Вс', 'Пн', 'Вт', 'Ср', 'Чт', 'Пт', 'Сб');
@@ -88,6 +93,9 @@ syswrite STDOUT, '{"version": 1, "stop_signal": 10, "cont_signal": 12, "click_ev
'; ';
while (1) { while (1) {
# json message that will be spewed to i3bar
my $je;
if ($onesec_timer >= 3) { if ($onesec_timer >= 3) {
# $cpuspeed = cpuspeed(); # $cpuspeed = cpuspeed();
@@ -156,57 +164,11 @@ while (1) {
$jo->[6]->{full_text} = $caps; $jo->[6]->{full_text} = $caps;
$jo->[6]->{color} = '#3e78fd'; $jo->[6]->{color} = '#3e78fd';
$jo->[7]->{full_text} = "mousepad"; @{$je} = (@{$jo}, @{$C});
$jo->[7]->{color} = '#ff0000';
$jo->[7]->{name} = 'mousepad';
$jo->[7]->{instance} = 'mousepad';
$jo->[7]->{align} = 'left';
$jo->[8]->{full_text} = "vsc"; @{$je} = reverse(@{$je});
$jo->[8]->{color} = '#ff0000';
$jo->[8]->{name} = 'code';
$jo->[8]->{instance} = 'code';
$jo->[8]->{align} = 'left';
$jo->[9]->{full_text} = "v"; syswrite STDOUT, encode_json($je) . ",\n";
$jo->[9]->{color} = '#ff0000';
$jo->[9]->{name} = 'vivaldi';
$jo->[9]->{instance} = 'vivaldi';
$jo->[9]->{align} = 'left';
$jo->[10]->{full_text} = "o";
$jo->[10]->{color} = '#ff0000';
$jo->[10]->{name} = 'opera';
$jo->[10]->{instance} = 'opera';
$jo->[10]->{align} = 'left';
$jo->[11]->{full_text} = "ff";
$jo->[11]->{color} = '#ff0000';
$jo->[11]->{name} = 'firefox';
$jo->[11]->{instance} = 'firefox';
$jo->[11]->{align} = 'left';
$jo->[12]->{full_text} = "sl";
$jo->[12]->{color} = '#ff0000';
$jo->[12]->{name} = 'slack';
$jo->[12]->{instance} = 'slack';
$jo->[13]->{align} = 'left';
$jo->[13]->{full_text} = "t";
$jo->[13]->{color} = '#ff0000';
$jo->[13]->{name} = 'Telegram';
$jo->[13]->{instance} = 'Telegram';
$jo->[13]->{align} = 'left';
$jo->[14]->{full_text} = "icq";
$jo->[14]->{color} = '#ff0000';
$jo->[14]->{name} = 'icq';
$jo->[14]->{instance} = 'icq';
$jo->[14]->{align} = 'left';
@{$jo} = reverse(@{$jo});
syswrite STDOUT, encode_json($jo) . ",\n";
$jf = 0; $jf = 0;
} }
# tick timers # tick timers
@@ -217,6 +179,29 @@ while (1) {
select(undef, undef, undef, 0.333); select(undef, undef, undef, 0.333);
} }
sub readconf() {
open (CONF, "$ENV{HOME}/.i3/config.json") or die "Unable to open $ENV{HOME}/.i3/config.json: $!\n";
my $sep = $/;
$/ = '';
my $c = readline(*CONF);
close CONF;
$/ = $sep;
$c = decode_json($c) or die "Unable to decode $ENV{HOME}/.i3/config.json: $!\n";
my ($j, %cmd);
my $cnt = 0;
foreach (@{$c}) {
$j->[$cnt]->{full_text} = $_->{full_text} // 'no fucken name';
$j->[$cnt]->{name} = $_->{name} // $_->{full_text};
$j->[$cnt]->{instance} = $_->{instance} // $_->{full_text};
$j->[$cnt]->{color} = $_->{color} // '#00aa33';
$cmd{$_->{name}} = $_->{cmd} // '/usr/bin/true';
$cnt++
}
return ($j, %cmd);
}
sub input_thread() { sub input_thread() {
# on input of this script i3bar sends infinite json array # on input of this script i3bar sends infinite json array
# one special thing about it: all elements of this array presented as # one special thing about it: all elements of this array presented as
@@ -226,16 +211,13 @@ sub input_thread() {
readline(*STDIN); # throw away leading "[\n" readline(*STDIN); # throw away leading "[\n"
while (1) { while (1) {
next if (eof(STDIN));
my $msg = readline(*STDIN); my $msg = readline(*STDIN);
$msg = substr($msg, 1, -1) if (substr($msg, 0, 1) eq ','); # drop leading ',' if present $msg = substr($msg, 1, -1) if (substr($msg, 0, 1) eq ','); # drop leading ',' if present
my $j = decode_json($msg); my $j = decode_json($msg);
if ($j->{name} eq 'mousepad') { if (defined($CMD{$j->{name}})) {
spawn('mousepad'); spawn($CMD{$j->{name}});
}
if ($j->{name} eq 'ff') {
spawn(['firefox']);
} }
} }
} }
@@ -537,6 +519,6 @@ sub spawn (@) {
exec $prg; exec $prg;
} }
} else { # Unable to fork } else { # Unable to fork
die "ERROR: Could not fork new process: $!\n\n"; die "ERROR: Could not fork new process: $!\n";
} }
} }