Skip to content

Commit 9952c1c

Browse files
using RichLog widget instead of ScrollView for diff_areas (TextScrollView) (#32)
This change lets use remove the code related to rendering each line of text manually as it is now handled by `RichLog`. This also enables horizontal scroll.
1 parent 7232667 commit 9952c1c

File tree

1 file changed

+8
-61
lines changed

1 file changed

+8
-61
lines changed

ui/scrollable_area.py

Lines changed: 8 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -1,81 +1,28 @@
11
from __future__ import annotations
2+
from textual.widgets import RichLog
23

3-
from textual.reactive import Reactive
4-
from textual.widgets import Static
5-
from textual.scroll_view import ScrollView
6-
from textual.app import App, ComposeResult
7-
from textual.geometry import Size
8-
from rich.console import RenderableType
9-
from textual.widgets import Markdown
10-
from rich.text import Text
114

12-
13-
from textual.app import App, ComposeResult
14-
from textual.geometry import Size
15-
from textual.strip import Strip
16-
from textual.scroll_view import ScrollView
17-
18-
from rich.segment import Segment
19-
20-
class TextScrollView(ScrollView):
5+
class TextScrollView(RichLog):
216
COMPONENT_CLASSES = {
227
"box", # Class for the main text container
238
"rich-text--line", # Class for each line of text
249
}
2510
DEFAULT_CSS = """
2611
TextScrollView {
2712
height: 100%;
13+
scrollbar-size: 1 1;
2814
}
2915
"""
3016

31-
def __init__(self, title = "", lines: list[str] = None, component_id: str = None) -> None:
32-
super().__init__()
17+
def __init__(self, title: str = "", component_id: str = None) -> None:
18+
super().__init__(name=title, auto_scroll=True, markup=True)
3319
self.border_title = title
34-
self.lines = lines if lines is not None else []
35-
self.lines = lines
36-
if lines:
37-
self.virtual_size = Size(0, len(lines))
38-
else:
39-
self.virtual_size = Size(0, 5)
40-
4120
if component_id:
4221
self.id = component_id
4322

44-
def update_virtual_size(self) -> None:
45-
"""Update the virtual size based on the number of lines."""
46-
self.virtual_size = Size(0, len(self.lines))
47-
48-
def render_line(self, y: int) -> Strip:
49-
"""Render a line of the widget. y is relative to the top of the widget."""
50-
scroll_x, scroll_y = self.scroll_offset
51-
y += scroll_y
52-
53-
if self.lines:
54-
if y >= len(self.lines):
55-
return Strip.blank(self.size.width)
56-
57-
rich_text = Text.from_markup(self.lines[y])
58-
segments = list(rich_text.render(self.app.console))
59-
60-
strip = Strip(segments, sum(segment.cell_length for segment in segments))
61-
strip = strip.crop(scroll_x, scroll_x + self.size.width)
62-
return strip
63-
return Strip.blank(self.size.width)
64-
6523
def append(self, lines: list[str]):
66-
if not self.lines:
67-
self.lines = []
68-
69-
self.lines += lines
70-
self.update_virtual_size()
71-
self.refresh(layout=True)
72-
self.scroll_to(y = self.max_scroll_y, speed=300)
24+
self.write("\n".join(lines))
7325

7426
def text(self, lines: list[str]):
75-
if not self.lines:
76-
self.lines = []
77-
78-
self.lines = lines
79-
self.update_virtual_size()
80-
self.refresh(layout=True)
81-
self.scroll_to(y = self.max_scroll_y, speed=300)
27+
self.clear()
28+
self.append(lines)

0 commit comments

Comments
 (0)