]> git.sur5r.net Git - i3/i3status/blob - contrib/wrapper.pl
add a small perl example wrapper script for i3status’s JSON output
[i3/i3status] / contrib / wrapper.pl
1 #!/usr/bin/env perl
2 # vim:ts=4:sw=4:expandtab
3 # © 2012 Michael Stapelberg, Public Domain
4
5 # This script is a simple wrapper which prefixes each i3status line with custom
6 # information. To use it, ensure your ~/.i3status.conf contains this line:
7 #     output_format = "i3bar"
8 # in the 'general' section.
9 # Then, in your ~/.i3/config, use:
10 #     status_command i3status | ~/i3status/contrib/wrapper.pl
11 # In the 'bar' section.
12
13 use strict;
14 use warnings;
15 # You can install the JSON module with 'cpan JSON' or by using your
16 # distribution’s package management system, for example apt-get install
17 # libjson-perl on Debian/Ubuntu.
18 use JSON;
19
20 # Don’t buffer any output.
21 $| = 1;
22
23 # Skip the first line which contains the version header.
24 print scalar <STDIN>;
25
26 # The second line contains the start of the infinite array.
27 print scalar <STDIN>;
28
29 # Read lines forever, ignore a comma at the beginning if it exists.
30 while (my ($statusline) = (<STDIN> =~ /^,?(.*)/)) {
31     # Decode the JSON-encoded line.
32     my @blocks = @{decode_json($statusline)};
33
34     # Prefix our own information (you could also suffix or insert in the
35     # middle).
36     @blocks = ({
37         full_text => 'MPD: not running',
38         name => 'mpd'
39     }, @blocks);
40
41     # Output the line as JSON.
42     print encode_json(\@blocks) . ",\n";
43 }