aboutsummaryrefslogtreecommitdiff
path: root/bin/status-bar
blob: bd55b05704fd37ef80ab5cf3afcc58c674ee7377 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
#!/usr/bin/env perl

#
# Derived from:
# https://github.com/i3/i3status/blob/28399bf84693a03eb772be647d5927011c1d2619/contrib/wrapper.pl
#

use v5.34;
use warnings;
use feature 'signatures';
no warnings ('experimental::signatures');
use Getopt::Std ();
use JSON ();

sub usage($fh) {
	print $fh <<~'EOF'
		Usage:
		  status-bar
		  status-bar -h
		EOF
}

sub help($fh) {
	print $fh <<~'EOF'

		Options:
		  -h, --help    show this message


		Process the output of i3status and add custom information for
		showing in the i3 bar.


		Examples:

		  Process i3status:

		    $ i3status | status-bar


		  Configure i3 to use status-bar:

		    # In $XDG_CONFIG_HOME/i3/config
		    bar {
			status_command i3status | status-bar
		    }
		EOF
}

for (@ARGV) {
	last if $_ eq '--';
	if ($_ eq '--help') {
		usage *STDOUT;
		help *STDOUT;
		exit;
	}
}

my %opts;
if (!Getopt::Std::getopts('h', \%opts)) {
	usage *STDERR;
	exit 2;
}

if ($opts{h}) {
	usage *STDOUT;
	help *STDOUT;
	exit;
}

# Don't buffer any output
$| = 1;

# Skip the first line which contains the version header.
print scalar <STDIN>;

# The second line contains the start of the infinite array.
print scalar <STDIN>;

# Read lines forever, ignore a comma at the beginning if it exists.
while (my ($statusline) = (<STDIN> =~ /^,?(.*)/)) {
	# Decode the JSON-encoded line.
	my @blocks = @{JSON::decode_json($statusline)};

	# Prefix our own information (you coud also suffix or insert in the
	# middle).

	my $mpris = `player current`;
	chomp $mpris;

	my $vms = `vm status | grep :up\$ | wc -l`;
	chomp $vms;

	@blocks = ({
		full_text => $vms,
		name => 'vms',
	}, {
		full_text => $mpris,
		name => 'mpris',
	}, @blocks);

	# Output the line as JSON.
	print JSON::encode_json(\@blocks) . ",\n";
}