aboutsummaryrefslogtreecommitdiff
path: root/bin/status-bar
diff options
context:
space:
mode:
authorEuAndreh <eu@euandre.org>2022-08-15 16:27:27 -0300
committerEuAndreh <eu@euandre.org>2022-08-15 16:27:27 -0300
commit1e01672dba034f7e9155392772d8da49bf3dfc3b (patch)
treebbb10d28350476d69ad0c6e789e9c13e16879daa /bin/status-bar
parentbin/check: Add perlcritic (diff)
downloaddotfiles-1e01672dba034f7e9155392772d8da49bf3dfc3b.tar.gz
dotfiles-1e01672dba034f7e9155392772d8da49bf3dfc3b.tar.xz
bin/status-bar: Add working version
Diffstat (limited to 'bin/status-bar')
-rwxr-xr-xbin/status-bar99
1 files changed, 99 insertions, 0 deletions
diff --git a/bin/status-bar b/bin/status-bar
new file mode 100755
index 0000000..65cef1c
--- /dev/null
+++ b/bin/status-bar
@@ -0,0 +1,99 @@
+#!/usr/bin/env perl
+
+#
+# Derived from:
+# https://github.com/i3/i3status/blob/28399bf84693a03eb772be647d5927011c1d2619/contrib/wrapper.pl
+#
+
+use v5.34;
+use strict;
+use warnings;
+use feature 'signatures';
+no warnings 'experimental';
+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;
+
+ @blocks = ({
+ full_text => "$mpris",
+ name => 'mpris',
+ }, @blocks);
+
+ # Output the line as JSON.
+ print JSON::encode_json(\@blocks) . ",\n";
+}