tui: Preserve cursor position when placing images
This commit is contained in:
parent
5b98f15d9b
commit
ee3b052a50
@ -174,7 +174,6 @@ class DiffHandler(Handler):
|
|||||||
self.cmd.set_cursor_position(0, self.num_lines - 1)
|
self.cmd.set_cursor_position(0, self.num_lines - 1)
|
||||||
|
|
||||||
def place_images(self):
|
def place_images(self):
|
||||||
self.cmd.set_cursor_position(0, 0)
|
|
||||||
offset = self.scroll_pos
|
offset = self.scroll_pos
|
||||||
limit = len(self.diff_lines)
|
limit = len(self.diff_lines)
|
||||||
in_image = False
|
in_image = False
|
||||||
@ -203,11 +202,10 @@ class DiffHandler(Handler):
|
|||||||
num_visible_rows = image_height_in_rows - topmost_visible_row
|
num_visible_rows = image_height_in_rows - topmost_visible_row
|
||||||
visible_frac = min(num_visible_rows / image_height_in_rows, 1)
|
visible_frac = min(num_visible_rows / image_height_in_rows, 1)
|
||||||
if visible_frac > 0:
|
if visible_frac > 0:
|
||||||
self.cmd.set_cursor_position(xpos, row)
|
|
||||||
height = int(visible_frac * placement.image.height)
|
height = int(visible_frac * placement.image.height)
|
||||||
top = placement.image.height - height
|
top = placement.image.height - height
|
||||||
self.image_manager.hide_image(placement.image.image_id)
|
self.image_manager.hide_image(placement.image.image_id)
|
||||||
self.image_manager.show_image(placement.image.image_id, src_rect=(
|
self.image_manager.show_image(placement.image.image_id, xpos, row, src_rect=(
|
||||||
0, top, placement.image.width, height))
|
0, top, placement.image.width, height))
|
||||||
|
|
||||||
def draw_screen(self):
|
def draw_screen(self):
|
||||||
|
|||||||
@ -5,13 +5,13 @@
|
|||||||
import codecs
|
import codecs
|
||||||
import os
|
import os
|
||||||
import sys
|
import sys
|
||||||
from collections import defaultdict, deque
|
|
||||||
from base64 import standard_b64encode
|
from base64 import standard_b64encode
|
||||||
|
from collections import defaultdict, deque
|
||||||
|
from itertools import count
|
||||||
|
|
||||||
from kitty.utils import fit_image, screen_size_function
|
from kitty.utils import fit_image, screen_size_function
|
||||||
|
|
||||||
from .operations import serialize_gr_command
|
from .operations import cursor
|
||||||
from itertools import count
|
|
||||||
|
|
||||||
try:
|
try:
|
||||||
fsenc = sys.getfilesystemencoding() or 'utf-8'
|
fsenc = sys.getfilesystemencoding() or 'utf-8'
|
||||||
@ -128,7 +128,7 @@ class ImageManager:
|
|||||||
self.tdir = tempfile.mkdtemp(prefix='kitten-images-')
|
self.tdir = tempfile.mkdtemp(prefix='kitten-images-')
|
||||||
with tempfile.NamedTemporaryFile(dir=self.tdir, delete=False) as f:
|
with tempfile.NamedTemporaryFile(dir=self.tdir, delete=False) as f:
|
||||||
f.write(b'abcd')
|
f.write(b'abcd')
|
||||||
self.handler.write(serialize_gr_command(dict(a='q', s=1, v=1, i=1, t='f'), standard_b64encode(f.name.encode(fsenc))))
|
self.handler.cmd.gr_command(dict(a='q', s=1, v=1, i=1, t='f'), standard_b64encode(f.name.encode(fsenc)))
|
||||||
|
|
||||||
def __exit__(self, *a):
|
def __exit__(self, *a):
|
||||||
import shutil
|
import shutil
|
||||||
@ -139,7 +139,7 @@ class ImageManager:
|
|||||||
|
|
||||||
def delete_all_sent_images(self):
|
def delete_all_sent_images(self):
|
||||||
for img_id in self.transmission_status:
|
for img_id in self.transmission_status:
|
||||||
self.handler.write(serialize_gr_command({'a': 'd', 'i': img_id}))
|
self.handler.cmd.gr_command({'a': 'd', 'i': img_id})
|
||||||
self.transmission_status.clear()
|
self.transmission_status.clear()
|
||||||
|
|
||||||
def handle_response(self, apc):
|
def handle_response(self, apc):
|
||||||
@ -175,7 +175,9 @@ class ImageManager:
|
|||||||
image_data = self.image_id_to_image_data[image_id]
|
image_data = self.image_id_to_image_data[image_id]
|
||||||
skey = self.image_id_to_converted_data[image_id]
|
skey = self.image_id_to_converted_data[image_id]
|
||||||
self.transmit_image(image_data, image_id, *skey)
|
self.transmit_image(image_data, image_id, *skey)
|
||||||
self.handler.write(serialize_gr_command(pl['cmd']))
|
with cursor(self.handler.write):
|
||||||
|
self.handler.cmd.set_cursor_position(pl['x'], pl['y'])
|
||||||
|
self.handler.cmd.gr_command(pl['cmd'])
|
||||||
|
|
||||||
def send_image(self, path, max_cols=None, max_rows=None, scale_up=False):
|
def send_image(self, path, max_cols=None, max_rows=None, scale_up=False):
|
||||||
path = os.path.abspath(path)
|
path = os.path.abspath(path)
|
||||||
@ -216,14 +218,16 @@ class ImageManager:
|
|||||||
return image_id, skey[1], skey[2]
|
return image_id, skey[1], skey[2]
|
||||||
|
|
||||||
def hide_image(self, image_id):
|
def hide_image(self, image_id):
|
||||||
self.handler.write(serialize_gr_command({'a': 'd', 'i': image_id}))
|
self.handler.cmd.gr_command({'a': 'd', 'i': image_id})
|
||||||
|
|
||||||
def show_image(self, image_id, src_rect=None):
|
def show_image(self, image_id, x, y, src_rect=None):
|
||||||
cmd = {'a': 'p', 'i': image_id}
|
cmd = {'a': 'p', 'i': image_id}
|
||||||
if src_rect is not None:
|
if src_rect is not None:
|
||||||
cmd['x'], cmd['y'], cmd['w'], cmd['h'] = map(int, src_rect)
|
cmd['x'], cmd['y'], cmd['w'], cmd['h'] = map(int, src_rect)
|
||||||
self.placements_in_flight[image_id].append({'cmd': cmd})
|
self.placements_in_flight[image_id].append({'cmd': cmd, 'x': x, 'y': y})
|
||||||
self.handler.write(serialize_gr_command(cmd))
|
with cursor(self.handler.write):
|
||||||
|
self.handler.cmd.set_cursor_position(x, y)
|
||||||
|
self.handler.cmd.gr_command(cmd)
|
||||||
|
|
||||||
def convert_image(self, path, available_width, available_height, image_data, scale_up=False):
|
def convert_image(self, path, available_width, available_height, image_data, scale_up=False):
|
||||||
try:
|
try:
|
||||||
@ -238,8 +242,8 @@ class ImageManager:
|
|||||||
cmd = {'a': 't', 'f': image_data.transmit_fmt, 's': width, 'v': height, 'i': image_id}
|
cmd = {'a': 't', 'f': image_data.transmit_fmt, 's': width, 'v': height, 'i': image_id}
|
||||||
if self.filesystem_ok:
|
if self.filesystem_ok:
|
||||||
cmd['t'] = 'f'
|
cmd['t'] = 'f'
|
||||||
self.handler.write(serialize_gr_command(
|
self.handler.cmd.gr_command(
|
||||||
cmd, standard_b64encode(rgba_path.encode(fsenc))))
|
cmd, standard_b64encode(rgba_path.encode(fsenc)))
|
||||||
else:
|
else:
|
||||||
import zlib
|
import zlib
|
||||||
data = open(rgba_path, 'rb').read()
|
data = open(rgba_path, 'rb').read()
|
||||||
@ -251,6 +255,6 @@ class ImageManager:
|
|||||||
chunk, data = data[:4096], data[4096:]
|
chunk, data = data[:4096], data[4096:]
|
||||||
m = 1 if data else 0
|
m = 1 if data else 0
|
||||||
cmd['m'] = m
|
cmd['m'] = m
|
||||||
self.handler.write(serialize_gr_command(cmd, chunk))
|
self.handler.cmd.gr_command(cmd, chunk)
|
||||||
cmd.clear()
|
cmd.clear()
|
||||||
return image_id
|
return image_id
|
||||||
|
|||||||
@ -161,6 +161,10 @@ def serialize_gr_command(cmd, payload=None):
|
|||||||
return b''.join(ans)
|
return b''.join(ans)
|
||||||
|
|
||||||
|
|
||||||
|
def gr_command(cmd, payload=None) -> str:
|
||||||
|
return serialize_gr_command(cmd, payload)
|
||||||
|
|
||||||
|
|
||||||
def clear_images_on_screen(delete_data=False) -> str:
|
def clear_images_on_screen(delete_data=False) -> str:
|
||||||
return serialize_gr_command({'a': 'd', 'd': 'A' if delete_data else 'a'})
|
return serialize_gr_command({'a': 'd', 'd': 'A' if delete_data else 'a'})
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user