Created
May 21, 2016 17:40
-
-
Save raboof/11dab45e9f6300d7459e0d6cad435dd5 to your computer and use it in GitHub Desktop.
Set terminal font size based on screen DPI
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Goes well together with borisfaure/terminology#46 :)