diff --git a/confluent_client/bin/nodeconsole b/confluent_client/bin/nodeconsole index 62ec8b8a..abfb959c 100755 --- a/confluent_client/bin/nodeconsole +++ b/confluent_client/bin/nodeconsole @@ -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 diff --git a/confluent_client/confluent/screensqueeze.py b/confluent_client/confluent/screensqueeze.py index 60aecc21..13d6049e 100644 --- a/confluent_client/confluent/screensqueeze.py +++ b/confluent_client/confluent/screensqueeze.py @@ -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):