mirror of
https://github.com/xcat2/confluent.git
synced 2026-06-18 09:30:50 +00:00
Implement fallback for screen geometry
Ideally, we can do TIOCGWINSZ. Unfortunately, in some cases this breaks, resort to escape codes.
This commit is contained in:
@@ -134,7 +134,9 @@ def determine_tile_size(numnodes):
|
||||
# from kitty by omitting, but:
|
||||
# then we don't know how much to move the cursor left after draw_image
|
||||
# Konsole won't scale at all with only partial scaling specified
|
||||
cheight, cwidth, pixwidth, pixheight = sq.get_screengeom()
|
||||
direct_console()
|
||||
cheight, cwidth, pixwidth, pixheight = sq.get_screengeom(escfallback=True)
|
||||
indirect_console()
|
||||
# 16:12 is to roughly account for the 'titles' of the tiles
|
||||
ratio = (pixwidth / 16) / (pixheight / 12)
|
||||
bestdeviation = None
|
||||
@@ -214,7 +216,9 @@ def cursor_show():
|
||||
sys.stdout.write('\x1b[?25h')
|
||||
|
||||
def get_pix_dimensions(width, height):
|
||||
cheight, cwidth, pixwidth, pixheight = sq.get_screengeom()
|
||||
direct_console()
|
||||
cheight, cwidth, pixwidth, pixheight = sq.get_screengeom(escfallback=True)
|
||||
indirect_console()
|
||||
imgwidth = int(pixwidth / cwidth * width)
|
||||
imgheight = int(pixheight / cheight * height)
|
||||
return imgwidth, imgheight
|
||||
|
||||
@@ -16,11 +16,55 @@ import fcntl
|
||||
import sys
|
||||
import struct
|
||||
import termios
|
||||
import select
|
||||
|
||||
def get_screengeom():
|
||||
def get_screengeom(escfallback=False):
|
||||
# returns height in cells, width in cells, width in pixels, height in pixels
|
||||
return struct.unpack('hhhh', fcntl.ioctl(sys.stdout, termios.TIOCGWINSZ,
|
||||
b'........'))
|
||||
geom = list(struct.unpack('hhhh', fcntl.ioctl(sys.stdout, termios.TIOCGWINSZ,
|
||||
b'........')))
|
||||
if escfallback and (geom[0] == 0 or geom[1] == 0):
|
||||
cellgeom = get_cell_geometry_using_esc_18t()
|
||||
geom[0], geom[1] = cellgeom
|
||||
if escfallback and (geom[2] == 0 or geom[3] == 0):
|
||||
pixgeom = get_pixel_geometry_using_esc_14t()
|
||||
geom[2], geom[3] = pixgeom
|
||||
return geom
|
||||
|
||||
def get_pixel_geometry_using_esc_14t():
|
||||
sys.stdout.write('\x1b[14t')
|
||||
sys.stdout.flush()
|
||||
rlist, _, _ = select.select([sys.stdin], [], [], 1)
|
||||
if not rlist:
|
||||
return 0, 0
|
||||
response = ''
|
||||
while True:
|
||||
c = sys.stdin.read(1)
|
||||
if c == 't':
|
||||
break
|
||||
response += c
|
||||
if not response.startswith('\x1b[4;'):
|
||||
return 0, 0
|
||||
pixgeom = response[4:].split(';')
|
||||
return int(pixgeom[0]), int(pixgeom[1])
|
||||
|
||||
def get_cell_geometry_using_esc_18t():
|
||||
sys.stdout.write('\x1b[18t')
|
||||
sys.stdout.flush()
|
||||
rlist, _, _ = select.select([sys.stdin], [], [], 1)
|
||||
if not rlist:
|
||||
return 0, 0
|
||||
response = ''
|
||||
while True:
|
||||
c = sys.stdin.read(1)
|
||||
if c == 't':
|
||||
break
|
||||
response += c
|
||||
if not response.startswith('\x1b[8;'):
|
||||
return 0, 0
|
||||
cellgeom = response[4:].split(';')
|
||||
return int(cellgeom[0]), int(cellgeom[1])
|
||||
|
||||
|
||||
class ScreenPrinter(object):
|
||||
|
||||
def __init__(self, noderange, client, textlen=4):
|
||||
|
||||
Reference in New Issue
Block a user