|
1 | 1 | from __future__ import annotations
|
| 2 | +from textual.widgets import RichLog |
2 | 3 |
|
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 |
11 | 4 |
|
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): |
21 | 6 | COMPONENT_CLASSES = {
|
22 | 7 | "box", # Class for the main text container
|
23 | 8 | "rich-text--line", # Class for each line of text
|
24 | 9 | }
|
25 | 10 | DEFAULT_CSS = """
|
26 | 11 | TextScrollView {
|
27 | 12 | height: 100%;
|
| 13 | + scrollbar-size: 1 1; |
28 | 14 | }
|
29 | 15 | """
|
30 | 16 |
|
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) |
33 | 19 | 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 |
| - |
41 | 20 | if component_id:
|
42 | 21 | self.id = component_id
|
43 | 22 |
|
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 |
| - |
65 | 23 | 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)) |
73 | 25 |
|
74 | 26 | 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