Skip to content

Commit cfe898f

Browse files
committed
TUI: restore cursor after text is changed
1 parent ffa237b commit cfe898f

File tree

6 files changed

+30
-18
lines changed

6 files changed

+30
-18
lines changed

lira/tui/__init__.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
from lira.app import LiraApp
99
from lira.tui.themes import style, theme
1010
from lira.tui.utils import exit_app, set_title
11-
from lira.tui.widgets import BooksList
1211
from lira.tui.windows import ContentArea, SidebarMenu, StatusBar
1312

1413

@@ -19,9 +18,7 @@ def __init__(self):
1918

2019
self.content = ContentArea(self)
2120
self.status = StatusBar(self)
22-
2321
self.menu = SidebarMenu(self)
24-
self.menu.reset(BooksList(self))
2522

2623
self.container = HSplit(
2724
[

lira/tui/render.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -189,12 +189,12 @@ def _render_test_block(self, node):
189189
def _reset_action(self, node, mouse_event):
190190
if mouse_event.event_type == MouseEventType.MOUSE_UP:
191191
node.reset()
192-
self.tui.content.render_section(self.section)
192+
self.tui.content.update_section(self.section)
193193

194194
def _edit_action(self, node, mouse_event):
195195
if mouse_event.event_type == MouseEventType.MOUSE_UP:
196196
self._open_editor(node)
197-
self.tui.content.render_section(self.section)
197+
self.tui.content.update_section(self.section)
198198

199199
def _open_editor(self, node):
200200
"""

lira/tui/widgets.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ def mouse_handler(self, mouse_event):
122122
def select(self, mouse_event):
123123
fragments = self.formatted_lines[mouse_event.position.y]
124124
# Find position in the fragment list.
125-
xpos = mouse_event.position.x
125+
xpos = mouse_event.position.x + 1
126126

127127
# Find mouse handler for this character.
128128
count = 0
@@ -150,12 +150,13 @@ def __init__(
150150
dont_extend_height=True,
151151
dont_extend_width=False,
152152
read_only=True,
153+
initial_position=0,
153154
):
154155
self.read_only = read_only
155156
formatted_text = to_formatted_text(text)
156157
plain_text = fragment_list_to_text(formatted_text)
157158
self.buffer = Buffer(
158-
document=Document(plain_text, 0),
159+
document=Document(plain_text, initial_position),
159160
read_only=Condition(lambda: self.read_only),
160161
)
161162
self.control = FormattedBufferControl(
@@ -201,7 +202,8 @@ def text(self, text):
201202
formatted_text = to_formatted_text(text)
202203
self.control.update_formatted_text(formatted_text)
203204
plain_text = fragment_list_to_text(formatted_text)
204-
self.document = Document(plain_text, 0)
205+
current_position = min(self.document.cursor_position, len(plain_text))
206+
self.document = Document(plain_text, current_position)
205207

206208
def get_key_bindings(self):
207209
keys = KeyBindings()

lira/tui/windows.py

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
from lira import __version__
1919
from lira.tui.render import Renderer
2020
from lira.tui.utils import exit_app, set_title
21-
from lira.tui.widgets import Button, FormattedTextArea
21+
from lira.tui.widgets import BooksList, Button, FormattedTextArea
2222

2323

2424
class WindowContainer:
@@ -28,7 +28,7 @@ class WindowContainer:
2828
def __init__(self, tui):
2929
self.tui = tui
3030
self.lira = self.tui.lira
31-
self.pages = []
31+
self.pages = [self._get_default_container()]
3232
self.container = DynamicContainer(self.get_container)
3333

3434
def _get_default_container(self):
@@ -103,6 +103,15 @@ def _get_default_container(self):
103103
return text_area
104104

105105
def render_section(self, section):
106+
renderer = Renderer(tui=self.tui, section=section)
107+
self.text_area = FormattedTextArea(
108+
merge_formatted_text(renderer.render()),
109+
scrollbar=True,
110+
focusable=True,
111+
)
112+
self.reset(self.text_area)
113+
114+
def update_section(self, section):
106115
renderer = Renderer(tui=self.tui, section=section)
107116
self.text_area.text = merge_formatted_text(renderer.render())
108117
self.reset(self.text_area)
@@ -131,6 +140,9 @@ def __init__(self, tui):
131140
key_bindings=self.get_key_bindings(),
132141
)
133142

143+
def _get_default_container(self):
144+
return BooksList(tui=self.tui)
145+
134146
def get_key_bindings(self):
135147
keys = KeyBindings()
136148

noxfile.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ def tests(session):
1919
@nox.session
2020
def coverage(session):
2121
session.install("coverage")
22-
session.run("coverage", "report", "--fail-under", "93")
22+
session.run("coverage", "report", "--fail-under", "92")
2323
session.run("coverage", "html")
2424

2525

tests/tui/test_windows.py

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
from prompt_toolkit.layout.containers import to_container
55
from prompt_toolkit.widgets import Button, Label
66

7+
from lira.tui.widgets import BooksList
78
from lira.tui.windows import SidebarMenu, StatusBar
89

910
from .utils import to_widget
@@ -15,15 +16,15 @@ def setup_method(self):
1516
self.window = SidebarMenu(tui=tui)
1617

1718
def test_default_menu(self):
18-
"""The default menu has 2 buttons and a label."""
19+
"""The default menu has 2 buttons and a BooksList."""
1920
children = to_container(self.window).get_children()
2021
assert len(children) == 3
2122

22-
empty_label = children[0].get_container().content
23+
books_list = children[0].get_container()
2324
back_button = children[1]
2425
exit_button = to_widget(children[2])
2526

26-
assert empty_label.text() == "Empty container"
27+
assert isinstance(books_list, BooksList)
2728

2829
# Back button is not visible
2930
assert to_widget(back_button.content).text == "Back"
@@ -38,7 +39,7 @@ def test_toggle_back_button(self):
3839
first_label = Label("First")
3940
second_label = Label("Second")
4041

41-
self.window.push(first_label)
42+
self.window.reset(first_label)
4243
self.window.push(second_label)
4344

4445
children = to_container(self.window).get_children()
@@ -65,7 +66,7 @@ def test_toggle_back_button(self):
6566

6667
def test_pop_all_pages(self):
6768
first_label = Label("First")
68-
self.window.push(first_label)
69+
self.window.reset(first_label)
6970
assert len(self.window.pages) == 1
7071

7172
# We can't pop the last page.
@@ -81,8 +82,8 @@ def test_pop_all_pages(self):
8182
self.window.reset()
8283
assert len(self.window.pages) == 1
8384
children = to_container(self.window).get_children()
84-
label = children[0].get_container()
85-
assert label.content.text() == "Empty container"
85+
books_list = children[0].get_container()
86+
assert isinstance(books_list, BooksList)
8687

8788

8889
class TestStatusBar:

0 commit comments

Comments
 (0)