Skip to content

Commit

Permalink
Graphics rendering fixup
Browse files Browse the repository at this point in the history
  • Loading branch information
joouha committed Aug 31, 2022
1 parent 176fb6e commit 78bb307
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 34 deletions.
9 changes: 6 additions & 3 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,18 @@ Changelog

Notable changes to this project will be documented in this file.

********
Upcoming
********
*******************
v2.0.7 (2022-08-31)
*******************

Fixed
=====

- Fix various graphics rendering glitches
- Disable line wrapping before probing terminal to prevent unrecognised APCs moving the cursor to the next line

----

*******************
v2.0.6 (2022-08-30)
*******************
Expand Down
2 changes: 1 addition & 1 deletion euporie/core/__init__.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"""This package defines the euporie application and its components."""

__app_name__ = "euporie"
__version__ = "2.0.6"
__version__ = "2.0.7"
__logo__ = "⚈"
__strapline__ = "Jupyter in the terminal"
__author__ = "Josiah Outram Halstead"
Expand Down
16 changes: 8 additions & 8 deletions euporie/core/convert/formats/sixel.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,13 @@
from PIL import Image


register(
from_=("png", "jpeg", "svg", "pdf"),
to="ansi",
filter_=commands_exist("chafa"),
)(partial(chafa_convert, "sixel"))


@register(
from_=("png", "jpeg"),
to="sixel",
Expand All @@ -30,7 +37,7 @@ def png_to_sixel_img2sixel(
) -> "str":
"""Converts PNG data to sixels :command:`img2sixel`."""
bg = bg or get_app().color_palette.bg.base_hex
cmd: "list[Any]" = ["img2sixel"]
cmd: "list[Any]" = ["img2sixel", "-I"]
if bg:
cmd += [f"--bgcolor={bg}"]
if cols is not None:
Expand All @@ -39,13 +46,6 @@ def png_to_sixel_img2sixel(
return call_subproc(data, cmd).decode()


register(
from_=("png", "jpeg", "svg", "pdf"),
to="ansi",
filter_=commands_exist("chafa"),
)(partial(chafa_convert, "sixel"))


register(
from_=("png", "jpeg", "svg", "pdf"),
to="sixel",
Expand Down
73 changes: 53 additions & 20 deletions euporie/core/widgets/display.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
from prompt_toolkit.layout.controls import GetLinePrefixCallable, UIContent, UIControl
from prompt_toolkit.layout.margins import ConditionalMargin
from prompt_toolkit.layout.mouse_handlers import MouseHandlers
from prompt_toolkit.layout.screen import WritePosition
from prompt_toolkit.layout.screen import Char, WritePosition
from prompt_toolkit.mouse_events import MouseEvent, MouseEventType
from prompt_toolkit.utils import Event

Expand Down Expand Up @@ -392,13 +392,20 @@ def render_lines() -> "list[StyleAndTextTuples]":
split_lines(
to_formatted_text(
[
("", "\n".join([" " * width] * (height))),
# Move cursor down and across by image height and width
(
f"class:render-{get_app().render_counter}",
"\n".join((height) * [" " * (width)]),
),
# Save position, then move back
(
"[ZeroWidthEscape]",
tmuxify(
f"\x1b[s\x1b[{height-1}A\x1b[{width}D{cmd}\x1b[u"
),
f"\x1b[s\x1b[{height-1}A\x1b[{width}D",
),
# Place the image without moving cursor
("[ZeroWidthEscape]", tmuxify(cmd)),
# Restore the last known cursor position (at the bottom)
("[ZeroWidthEscape]", "\x1b[u"),
]
)
)
Expand Down Expand Up @@ -443,15 +450,20 @@ def render_lines() -> "list[StyleAndTextTuples]":
split_lines(
to_formatted_text(
[
("", "\n".join([" " * width] * (height))),
# Move cursor down and across by image height and width
(
f"class:render-{get_app().render_counter}",
"\n".join((height) * [" " * (width)]),
),
# Save position, then move back
(
"[ZeroWidthEscape]",
tmuxify(
f"\x1b[s\x1b[{height-1}A\x1b[{width}D{cmd}\x1b[u"
),
f"\x1b[s\x1b[{height-1}A\x1b[{width}D",
),
# Add zero-width no-break space to work around PTK issue #1651
("", "\uFEFF\n"),
# Place the image without moving cursor
("[ZeroWidthEscape]", tmuxify(cmd)),
# Restore the last known cursor position (at the bottom)
("[ZeroWidthEscape]", "\x1b[u"),
]
)
)
Expand Down Expand Up @@ -594,16 +606,20 @@ def render_lines() -> "list[StyleAndTextTuples]":
split_lines(
to_formatted_text(
[
# Move cursor down by image height
("", "\n" * (height - 1)),
# Save position, then move back up
("[ZeroWidthEscape]", f"\x1b[s\x1b[{height-1}A"),
# Move cursor down and acoss by image height and width
(
f"class:render-{get_app().render_counter}",
"\n".join((height) * [" " * (width)]),
),
# Save position, then move back
(
"[ZeroWidthEscape]",
f"\x1b[s\x1b[{height-1}A\x1b[{width}D",
),
# Place the image without moving cursor
("[ZeroWidthEscape]", tmuxify(cmd)),
# Restore the last known cursor position (at the bottom)
("[ZeroWidthEscape]", "\x1b[u "),
# Add zero-width no-break space to work around PTK issue #1651
("", " \uFEFF"),
("[ZeroWidthEscape]", "\x1b[u"),
]
)
)
Expand Down Expand Up @@ -678,6 +694,7 @@ def write_to_screen(
if filter_value and target_wp and self.target_window.render_info is not None:
rendered_height = self.target_window.render_info.window_height
# Only draw if the target window is fully visible
log.debug((target_wp.height, rendered_height))
if target_wp.height == rendered_height:
cpos = screen.get_menu_position(self.target_window)
new_write_position = WritePosition(
Expand All @@ -690,8 +707,6 @@ def write_to_screen(
screen,
MouseHandlers(), # Do not let the float add mouse events
new_write_position,
# Ensure the float is always updated by constantly changing style
# parent_style + f" class:graphic-{get_app().render_counter}",
parent_style,
erase_bg=True,
z_index=z_index,
Expand All @@ -701,6 +716,23 @@ def write_to_screen(
if not filter_value or not get_app().leave_graphics():
self.content.hide()

def _fill_bg(
self, screen: Screen, write_position: WritePosition, erase_bg: bool
) -> None:
"""Erase/fill the background."""
char: Optional[str]
if callable(self.char):
char = self.char()
else:
char = self.char
if erase_bg or char:
wp = write_position
char_obj = Char(char or " ", f"class:render-{get_app().render_counter}")
for y in range(wp.ypos, wp.ypos + wp.height):
row = screen.data_buffer[y]
for x in range(wp.xpos, wp.xpos + wp.width):
row[x] = char_obj


class GraphicFloat(Float):
"""A :py:class:`Float` which displays a graphic."""
Expand Down Expand Up @@ -859,6 +891,7 @@ def __init__(
always_hide_cursor=always_hide_cursor,
dont_extend_height=dont_extend_height,
style=self.style,
char=" ",
)

# Add graphic
Expand Down
2 changes: 1 addition & 1 deletion euporie/notebook/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ def __init__(self, **kwargs: "Any") -> "None":
"""Create a new euporie text user interface application instance."""
kwargs.setdefault("title", "euporie-notebook")
kwargs.setdefault("full_screen", True)
kwargs.setdefault("leave_graphics", True)
kwargs.setdefault("leave_graphics", False)
super().__init__(**kwargs)
self.search_bar = SearchBar()
self.bindings_to_load.append("euporie.notebook.app.NotebookApp")
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "euporie"
version = "2.0.6"
version = "2.0.7"
description = "Euporie is a suite of terminal applications for interacting with Jupyter kernels"
authors = ["Josiah Outram Halstead <[email protected]>"]
license = "MIT"
Expand Down

0 comments on commit 78bb307

Please sign in to comment.