Skip to content

Instantly share code, notes, and snippets.

@raboof
Created May 21, 2016 17:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save raboof/11dab45e9f6300d7459e0d6cad435dd5 to your computer and use it in GitHub Desktop.
Save raboof/11dab45e9f6300d7459e0d6cad435dd5 to your computer and use it in GitHub Desktop.
Set terminal font size based on screen DPI
#!/usr/bin/perl
use strict;
use warnings;
my $windows = `wmctrl -l -G`;
my $processes = `ps faux`;
my $xrandr = `xrandr | grep " connected"`;
my @displays = ();
while ($xrandr =~ /(\w+) connected (\d+)x(\d+)\+(\d+)\+(\d+).*?(\d+)mm x (\d+)mm/g) {
my %data = (
'name', $1,
'width', $2,
'height', $3,
'x', $4,
'y', $5,
'fontsize', int(6 + (1.10 * $2 / $6))
);
print "Font size on $data{'name'}: $data{'fontsize'}\n";
push @displays, (\%data);
}
while ($windows =~ /(\w+)\s+\d+\s+(\d+)\s+(\d+).*/g) {
my ($wid, $x, $y) = ($1, $2, $3, $4, $5);
my $pid = getpid($wid);
if ($pid && $processes =~ /$pid.*terminology\n.*(pts\/\d+)/m) {
my $pts = $1;
for my $display (@displays) {
if ($$display{'x'} <= $x && $x < ($$display{'x'} + $$display{'width'})
&& $$display{'y'} <= $y && $y < ($$display{'y'} + $$display{'height'})) {
setfontsize($pts, $$display{'fontsize'});
}
}
}
}
sub getpid {
my ($wid) = @_;
my $wininfo = `xprop -id $wid -notype _NET_WM_PID`;
if ($wininfo =~ /_NET_WM_PID = (\d+)/) {
return $1
}
}
sub setfontsize {
my ($pts, $fontsize) = @_;
open PTS, ">/dev/$1";
print PTS "\033[?35h\033]50;#+1 Source Code Pro:size=$fontsize\033\\";
close PTS
}
@raboof
Copy link
Author

raboof commented May 21, 2016

Goes well together with borisfaure/terminology#46 :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment