Skip to content

Instantly share code, notes, and snippets.

@nobrowser
Created July 9, 2019 01:23
Show Gist options
  • Save nobrowser/1b54c42e2910005a5db3850cc01e80bd to your computer and use it in GitHub Desktop.
Save nobrowser/1b54c42e2910005a5db3850cc01e80bd to your computer and use it in GitHub Desktop.
Script to feed lemonbar, with as few external processes as possible
#! /bin/sh localperl
# This is now a -*-Perl-*- script
use strict;
use warnings;
use File::Slurp;
use POSIX qw(strftime);
use X11::Xlib;
use X11::Xlib::Display;
$main::display = X11::Xlib::Display->new;
$main::atomname = X11::Xlib::XInternAtom($main::display, 'WM_NAME', 0);
$main::atomstring = X11::Xlib::XInternAtom($main::display, 'STRING', 0);
sub get_window_name {
our ($display, $atomname, $atomstring);
my $xid = $_[0];
my @atoms = X11::Xlib::XListProperties($display, $xid);
return undef unless grep { $_ == $atomname } @atoms;
my ($xtype, $xformat, $xitems, $xafter, $xdata) = (undef, undef, undef, undef, undef);
my $retval = X11::Xlib::XGetWindowProperty($display, $xid, $atomname, 0, 32, 0, $atomstring,
$xtype, $xformat, $xitems, $xafter, $xdata);
return undef if !defined $retval || $retval || !$xdata;
return $xdata;
}
sub safe_pipe {
my @argv = @_;
open(my $fh, "-|") || exec @argv;
my @lines;
while (<$fh>) {
push @lines, $_;
}
close $fh;
return @lines;
}
sub focused_title {
my @query = safe_pipe('bspc', 'query', '-N', '-n', 'focused');
return 'none' unless @query;
my $windowid = $query[0];
chomp $windowid;
return 'none' unless $windowid =~ m(^0x[0-9a-f]+$)i;
my $nameline = get_window_name(hex($windowid));
return 'none' unless $nameline;
return 'none' unless $nameline =~ m(^\s*(\S(.*\S)?)\s*$) ;
return $1;
}
sub loadavg {
my @values = split " ", read_file('/proc/loadavg');
return join(' ', $values[0], $values[1], $values[2]);
}
sub read_sensor {
my ($node, $scale) = @_;
my $raw = read_file("/sys/class/hwmon/hwmon0/${node}_input");
chomp $raw;
my $iraw = $raw + 0;
if (($iraw % $scale) == 0) {
return $iraw / $scale;
} else {
return sprintf('%.2f', ($raw + 0.0) / ($scale + 0.0));
}
}
sub focused_desktop {
my @query = safe_pipe('bspc', 'query', '--names', '-D', '-d', 'focused');
return 'none' unless @query;
my $desktop = $query[0];
chomp $desktop;
return $desktop;
}
sub construct_line {
my $now_string = strftime '%H:%M', localtime;
my $maybe_cron = '';
$maybe_cron = 'CRON' if stat('/tmp/CRON_WAS_HERE');
my $out_line = join(' ',
'time:', $now_string,
'vcore:', read_sensor('in0', 1000),
'fan:', read_sensor('fan1', 1),
'temp:', read_sensor('temp1', 1000),
'loadavg:', &loadavg,
$maybe_cron,
q(%{c}),
'window:', &focused_title,
q(%{r}),
'desktop:', &focused_desktop,
qq(\n));
}
$| = 1;
while (1) {
print &construct_line;
sleep 5;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment