braid-tui 0.1.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- braid_tui/__init__.py +72 -0
- braid_tui/components/__init__.py +27 -0
- braid_tui/components/card.py +139 -0
- braid_tui/components/container.py +37 -0
- braid_tui/components/fixed_height.py +44 -0
- braid_tui/components/input.py +186 -0
- braid_tui/components/loader.py +58 -0
- braid_tui/components/scroll_view.py +104 -0
- braid_tui/components/status_bar.py +44 -0
- braid_tui/components/text.py +77 -0
- braid_tui/layout.py +454 -0
- braid_tui/renderer.py +443 -0
- braid_tui/screen.py +141 -0
- braid_tui-0.1.0.dist-info/METADATA +7 -0
- braid_tui-0.1.0.dist-info/RECORD +17 -0
- braid_tui-0.1.0.dist-info/WHEEL +4 -0
- braid_tui-0.1.0.dist-info/licenses/LICENSE +21 -0
braid_tui/__init__.py
ADDED
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
"""braid_tui:差分渲染终端 UI。
|
|
2
|
+
|
|
3
|
+
本包零第三方依赖,也不依赖 braid-ai / braid-agent / braid-coding-agent:渲染库
|
|
4
|
+
必须能被任何层无害引入。
|
|
5
|
+
"""
|
|
6
|
+
|
|
7
|
+
from __future__ import annotations
|
|
8
|
+
|
|
9
|
+
from typing import Final
|
|
10
|
+
|
|
11
|
+
from braid_tui.components import (
|
|
12
|
+
Card,
|
|
13
|
+
Container,
|
|
14
|
+
FixedHeight,
|
|
15
|
+
Input,
|
|
16
|
+
Loader,
|
|
17
|
+
ScrollView,
|
|
18
|
+
StatusBar,
|
|
19
|
+
Text,
|
|
20
|
+
)
|
|
21
|
+
from braid_tui.layout import (
|
|
22
|
+
SEGMENT_RESET,
|
|
23
|
+
TAB_WIDTH,
|
|
24
|
+
ansi_code_length,
|
|
25
|
+
char_width,
|
|
26
|
+
pad_to_width,
|
|
27
|
+
slice_by_column,
|
|
28
|
+
strip_ansi,
|
|
29
|
+
truncate_to_width,
|
|
30
|
+
visible_width,
|
|
31
|
+
wrap_text,
|
|
32
|
+
)
|
|
33
|
+
from braid_tui.renderer import (
|
|
34
|
+
CURSOR_MARKER,
|
|
35
|
+
Component,
|
|
36
|
+
FakeTerminal,
|
|
37
|
+
Renderer,
|
|
38
|
+
StdoutTerminal,
|
|
39
|
+
Terminal,
|
|
40
|
+
)
|
|
41
|
+
from braid_tui.screen import Screen
|
|
42
|
+
|
|
43
|
+
__version__: Final[str] = "0.1.0"
|
|
44
|
+
|
|
45
|
+
__all__ = [
|
|
46
|
+
"CURSOR_MARKER",
|
|
47
|
+
"SEGMENT_RESET",
|
|
48
|
+
"TAB_WIDTH",
|
|
49
|
+
"Card",
|
|
50
|
+
"Component",
|
|
51
|
+
"Container",
|
|
52
|
+
"FakeTerminal",
|
|
53
|
+
"FixedHeight",
|
|
54
|
+
"Input",
|
|
55
|
+
"Loader",
|
|
56
|
+
"Renderer",
|
|
57
|
+
"Screen",
|
|
58
|
+
"ScrollView",
|
|
59
|
+
"StatusBar",
|
|
60
|
+
"StdoutTerminal",
|
|
61
|
+
"Terminal",
|
|
62
|
+
"Text",
|
|
63
|
+
"__version__",
|
|
64
|
+
"ansi_code_length",
|
|
65
|
+
"char_width",
|
|
66
|
+
"pad_to_width",
|
|
67
|
+
"slice_by_column",
|
|
68
|
+
"strip_ansi",
|
|
69
|
+
"truncate_to_width",
|
|
70
|
+
"visible_width",
|
|
71
|
+
"wrap_text",
|
|
72
|
+
]
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
"""组件:Container / Text / Loader / Input / ScrollView / Card / StatusBar / FixedHeight。
|
|
2
|
+
|
|
3
|
+
所有组件实现 :class:`braid_tui.renderer.Component` 协议(``render(width) -> list[str]`` +
|
|
4
|
+
``invalidate()``)。
|
|
5
|
+
"""
|
|
6
|
+
|
|
7
|
+
from __future__ import annotations
|
|
8
|
+
|
|
9
|
+
from braid_tui.components.card import Card
|
|
10
|
+
from braid_tui.components.container import Container
|
|
11
|
+
from braid_tui.components.fixed_height import FixedHeight
|
|
12
|
+
from braid_tui.components.input import Input
|
|
13
|
+
from braid_tui.components.loader import Loader
|
|
14
|
+
from braid_tui.components.scroll_view import ScrollView
|
|
15
|
+
from braid_tui.components.status_bar import StatusBar
|
|
16
|
+
from braid_tui.components.text import Text
|
|
17
|
+
|
|
18
|
+
__all__ = [
|
|
19
|
+
"Card",
|
|
20
|
+
"Container",
|
|
21
|
+
"FixedHeight",
|
|
22
|
+
"Input",
|
|
23
|
+
"Loader",
|
|
24
|
+
"ScrollView",
|
|
25
|
+
"StatusBar",
|
|
26
|
+
"Text",
|
|
27
|
+
]
|
|
@@ -0,0 +1,139 @@
|
|
|
1
|
+
"""卡片:带标题的边框小卡,支持折叠、行尾滚动与定宽截断。
|
|
2
|
+
|
|
3
|
+
思考过程与工具调用都用卡片呈现:内容可能很长,但卡片只占固定行数并滚动显示
|
|
4
|
+
末尾几行——这样界面不会被一次长思考或大段工具输出顶满。所有行都按可见宽度
|
|
5
|
+
对齐到视口宽,中文、emoji 与 ANSI 颜色都不会越界。
|
|
6
|
+
"""
|
|
7
|
+
|
|
8
|
+
from __future__ import annotations
|
|
9
|
+
|
|
10
|
+
from collections import deque
|
|
11
|
+
from collections.abc import Sequence
|
|
12
|
+
|
|
13
|
+
from braid_tui.layout import pad_to_width, truncate_to_width, visible_width
|
|
14
|
+
from braid_tui.renderer import Component
|
|
15
|
+
|
|
16
|
+
__all__ = ["Card"]
|
|
17
|
+
|
|
18
|
+
_TOP_LEFT = "╭"
|
|
19
|
+
_TOP_RIGHT = "╮"
|
|
20
|
+
_BOTTOM_LEFT = "╰"
|
|
21
|
+
_BOTTOM_RIGHT = "╯"
|
|
22
|
+
_H_LINE = "─"
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
class Card(Component):
|
|
26
|
+
"""带边框的小卡片。
|
|
27
|
+
|
|
28
|
+
- ``collapsed=True`` 时只渲染一行(标题 + 首行摘要);
|
|
29
|
+
- 展开时渲染顶边、``max_lines`` 行正文与底边;
|
|
30
|
+
- ``append_line`` 只保留末尾 ``max_lines`` 行,形成滚动窗口。
|
|
31
|
+
"""
|
|
32
|
+
|
|
33
|
+
def __init__(
|
|
34
|
+
self,
|
|
35
|
+
title: str = "",
|
|
36
|
+
*,
|
|
37
|
+
lines: Sequence[str] | None = None,
|
|
38
|
+
collapsed: bool = True,
|
|
39
|
+
max_lines: int = 6,
|
|
40
|
+
) -> None:
|
|
41
|
+
self._title = title
|
|
42
|
+
self._lines: deque[str] = deque(lines or [], maxlen=max(1, max_lines))
|
|
43
|
+
self._collapsed = collapsed
|
|
44
|
+
self._max_lines = max(1, max_lines)
|
|
45
|
+
self._cache_key: tuple[str, tuple[str, ...], bool, int] | None = None
|
|
46
|
+
self._cached_lines: list[str] = []
|
|
47
|
+
|
|
48
|
+
# ---------------------------------------------------------------- 状态
|
|
49
|
+
|
|
50
|
+
@property
|
|
51
|
+
def title(self) -> str:
|
|
52
|
+
return self._title
|
|
53
|
+
|
|
54
|
+
@property
|
|
55
|
+
def collapsed(self) -> bool:
|
|
56
|
+
return self._collapsed
|
|
57
|
+
|
|
58
|
+
@property
|
|
59
|
+
def max_lines(self) -> int:
|
|
60
|
+
return self._max_lines
|
|
61
|
+
|
|
62
|
+
def lines(self) -> list[str]:
|
|
63
|
+
return list(self._lines)
|
|
64
|
+
|
|
65
|
+
def set_title(self, title: str) -> None:
|
|
66
|
+
if title != self._title:
|
|
67
|
+
self._title = title
|
|
68
|
+
self._cache_key = None
|
|
69
|
+
|
|
70
|
+
def set_lines(self, lines: Sequence[str]) -> None:
|
|
71
|
+
self._lines = deque(lines, maxlen=self._max_lines)
|
|
72
|
+
self._cache_key = None
|
|
73
|
+
|
|
74
|
+
def append_line(self, line: str) -> None:
|
|
75
|
+
self._lines.append(line)
|
|
76
|
+
self._cache_key = None
|
|
77
|
+
|
|
78
|
+
def set_collapsed(self, collapsed: bool) -> None:
|
|
79
|
+
if collapsed != self._collapsed:
|
|
80
|
+
self._collapsed = collapsed
|
|
81
|
+
self._cache_key = None
|
|
82
|
+
|
|
83
|
+
def toggle(self) -> None:
|
|
84
|
+
self.set_collapsed(not self._collapsed)
|
|
85
|
+
|
|
86
|
+
def invalidate(self) -> None:
|
|
87
|
+
self._cache_key = None
|
|
88
|
+
|
|
89
|
+
# ---------------------------------------------------------------- 渲染
|
|
90
|
+
|
|
91
|
+
def render(self, width: int) -> list[str]:
|
|
92
|
+
width = max(1, width)
|
|
93
|
+
body = list(self._lines)
|
|
94
|
+
key = (self._title, tuple(body), self._collapsed, width)
|
|
95
|
+
if self._cache_key == key:
|
|
96
|
+
return self._cached_lines
|
|
97
|
+
lines = self._render_uncached(width, body)
|
|
98
|
+
self._cache_key = key
|
|
99
|
+
self._cached_lines = lines
|
|
100
|
+
return lines
|
|
101
|
+
|
|
102
|
+
def _render_uncached(self, width: int, body: list[str]) -> list[str]:
|
|
103
|
+
if self._collapsed:
|
|
104
|
+
summary = body[0] if body else ""
|
|
105
|
+
if self._title and summary:
|
|
106
|
+
label = f"{self._title} · {summary}"
|
|
107
|
+
else:
|
|
108
|
+
label = self._title or summary
|
|
109
|
+
return [self._border_row(width, label)]
|
|
110
|
+
top = self._border_row(width, self._title, left=_TOP_LEFT, right=_TOP_RIGHT)
|
|
111
|
+
bottom = self._border_row(width, "", left=_BOTTOM_LEFT, right=_BOTTOM_RIGHT, fill=_H_LINE)
|
|
112
|
+
shown = body[: self._max_lines]
|
|
113
|
+
middle = [self._content_row(line, width) for line in shown]
|
|
114
|
+
return [top, *middle, bottom]
|
|
115
|
+
|
|
116
|
+
def _border_row(
|
|
117
|
+
self,
|
|
118
|
+
width: int,
|
|
119
|
+
label: str,
|
|
120
|
+
*,
|
|
121
|
+
left: str = _TOP_LEFT,
|
|
122
|
+
right: str = _TOP_RIGHT,
|
|
123
|
+
fill: str = _H_LINE,
|
|
124
|
+
) -> str:
|
|
125
|
+
inner = max(0, width - 2)
|
|
126
|
+
if not label:
|
|
127
|
+
return left + fill * inner + right
|
|
128
|
+
text = f" {label} "
|
|
129
|
+
if visible_width(text) >= inner:
|
|
130
|
+
text = truncate_to_width(text, max(1, inner - 1))
|
|
131
|
+
fill_width = max(0, inner - visible_width(text))
|
|
132
|
+
# 折叠行左端用三角标记,展开边框保持方框。
|
|
133
|
+
prefix = "▸" if self._collapsed and left == _TOP_LEFT else left
|
|
134
|
+
return prefix + text + fill * fill_width + right
|
|
135
|
+
|
|
136
|
+
def _content_row(self, line: str, width: int) -> str:
|
|
137
|
+
inner = max(0, width - 2)
|
|
138
|
+
body = truncate_to_width(line, inner) if visible_width(line) > inner else line
|
|
139
|
+
return "│" + pad_to_width(body, inner) + "│"
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
"""纵向堆叠容器。"""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
from braid_tui.renderer import Component
|
|
6
|
+
|
|
7
|
+
__all__ = ["Container"]
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
class Container:
|
|
11
|
+
"""按注册顺序纵向堆叠子组件,行数组直接拼接。
|
|
12
|
+
|
|
13
|
+
``width`` 原样传给每个子组件。
|
|
14
|
+
"""
|
|
15
|
+
|
|
16
|
+
def __init__(self, children: list[Component] | None = None) -> None:
|
|
17
|
+
self.children: list[Component] = list(children) if children else []
|
|
18
|
+
|
|
19
|
+
def add_child(self, component: Component) -> None:
|
|
20
|
+
self.children.append(component)
|
|
21
|
+
|
|
22
|
+
def remove_child(self, component: Component) -> None:
|
|
23
|
+
if component in self.children:
|
|
24
|
+
self.children.remove(component)
|
|
25
|
+
|
|
26
|
+
def clear(self) -> None:
|
|
27
|
+
self.children.clear()
|
|
28
|
+
|
|
29
|
+
def render(self, width: int) -> list[str]:
|
|
30
|
+
lines: list[str] = []
|
|
31
|
+
for child in self.children:
|
|
32
|
+
lines.extend(child.render(width))
|
|
33
|
+
return lines
|
|
34
|
+
|
|
35
|
+
def invalidate(self) -> None:
|
|
36
|
+
for child in self.children:
|
|
37
|
+
child.invalidate()
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
"""定高视口:把任意子组件裁成固定行数(不足补空行,超出截断)。
|
|
2
|
+
|
|
3
|
+
全屏布局里活动卡片区与选择器浮层都占固定行数,这样历史区高度只由终端行数
|
|
4
|
+
决定,差分渲染的行号才稳定。不缓存:子组件自己负责缓存,固定高度每次直接
|
|
5
|
+
读取最新内容,避免动态卡片内容被视口缓存挡住。
|
|
6
|
+
"""
|
|
7
|
+
|
|
8
|
+
from __future__ import annotations
|
|
9
|
+
|
|
10
|
+
from braid_tui.layout import truncate_to_width
|
|
11
|
+
from braid_tui.renderer import Component
|
|
12
|
+
|
|
13
|
+
__all__ = ["FixedHeight"]
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
class FixedHeight(Component):
|
|
17
|
+
"""把子组件输出钳制到 ``height`` 行。"""
|
|
18
|
+
|
|
19
|
+
def __init__(self, child: Component, height: int) -> None:
|
|
20
|
+
self._child = child
|
|
21
|
+
self._height = max(0, height)
|
|
22
|
+
|
|
23
|
+
@property
|
|
24
|
+
def height(self) -> int:
|
|
25
|
+
return self._height
|
|
26
|
+
|
|
27
|
+
def set_height(self, height: int) -> None:
|
|
28
|
+
self._height = max(0, height)
|
|
29
|
+
|
|
30
|
+
def child(self) -> Component:
|
|
31
|
+
return self._child
|
|
32
|
+
|
|
33
|
+
def invalidate(self) -> None:
|
|
34
|
+
self._child.invalidate()
|
|
35
|
+
|
|
36
|
+
def render(self, width: int) -> list[str]:
|
|
37
|
+
raw = list(self._child.render(width))
|
|
38
|
+
if len(raw) < self._height:
|
|
39
|
+
return [*raw, *("" for _ in range(self._height - len(raw)))]
|
|
40
|
+
if len(raw) > self._height:
|
|
41
|
+
raw = raw[: self._height]
|
|
42
|
+
if raw:
|
|
43
|
+
raw[-1] = truncate_to_width(raw[-1], width)
|
|
44
|
+
return raw
|
|
@@ -0,0 +1,186 @@
|
|
|
1
|
+
"""单行输入组件。
|
|
2
|
+
|
|
3
|
+
支持:插入/退格/前向删除、左右移动、Home/End、回车提交、转义回调。
|
|
4
|
+
光标用 ``CURSOR_MARKER``(APC 零宽序列)标记,渲染器负责把硬件光标定位过去。
|
|
5
|
+
|
|
6
|
+
字符宽度按 codepoint 处理(Python 无 grapheme 分段器,组合输入法输入的附标字符
|
|
7
|
+
会被当作独立 codepoint 编辑,显示宽度仍是 0——列对齐不受影响)。
|
|
8
|
+
"""
|
|
9
|
+
|
|
10
|
+
from __future__ import annotations
|
|
11
|
+
|
|
12
|
+
from collections.abc import Callable
|
|
13
|
+
|
|
14
|
+
from braid_tui.layout import (
|
|
15
|
+
ansi_code_length,
|
|
16
|
+
char_width,
|
|
17
|
+
pad_to_width,
|
|
18
|
+
truncate_to_width,
|
|
19
|
+
visible_width,
|
|
20
|
+
)
|
|
21
|
+
from braid_tui.renderer import CURSOR_MARKER
|
|
22
|
+
|
|
23
|
+
__all__ = ["Input"]
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
class Input:
|
|
27
|
+
"""单行文本输入,聚焦时在光标位置输出 CURSOR_MARKER。"""
|
|
28
|
+
|
|
29
|
+
def __init__(
|
|
30
|
+
self,
|
|
31
|
+
*,
|
|
32
|
+
prompt: str = "> ",
|
|
33
|
+
placeholder: str = "",
|
|
34
|
+
focused: bool = False,
|
|
35
|
+
) -> None:
|
|
36
|
+
self._value = ""
|
|
37
|
+
#: 光标位置(codepoint 索引,0 ≤ cursor ≤ len(value))。
|
|
38
|
+
self._cursor = 0
|
|
39
|
+
self._prompt = prompt
|
|
40
|
+
self._placeholder = placeholder
|
|
41
|
+
self.focused = focused
|
|
42
|
+
self.on_submit: Callable[[str], None] | None = None
|
|
43
|
+
self.on_escape: Callable[[], None] | None = None
|
|
44
|
+
|
|
45
|
+
# ------------------------------------------------------------------ 状态
|
|
46
|
+
|
|
47
|
+
@property
|
|
48
|
+
def value(self) -> str:
|
|
49
|
+
return self._value
|
|
50
|
+
|
|
51
|
+
@property
|
|
52
|
+
def cursor(self) -> int:
|
|
53
|
+
return self._cursor
|
|
54
|
+
|
|
55
|
+
def set_value(self, value: str, *, cursor: int | None = None) -> None:
|
|
56
|
+
"""整体赋值。
|
|
57
|
+
|
|
58
|
+
``cursor=None``(默认)沿用旧语义:把旧光标钳到新长度,不主动移动;
|
|
59
|
+
显式给 ``cursor`` 时按给定 codepoint 索引落位并钳制到 ``[0, len]``。
|
|
60
|
+
补全这类"文本与光标必须一起变"的场景应传 ``cursor``,或直接用
|
|
61
|
+
:meth:`replace_before_cursor`。
|
|
62
|
+
"""
|
|
63
|
+
self._value = value
|
|
64
|
+
if cursor is None:
|
|
65
|
+
self._cursor = min(self._cursor, len(value))
|
|
66
|
+
else:
|
|
67
|
+
self._cursor = max(0, min(cursor, len(value)))
|
|
68
|
+
|
|
69
|
+
def replace_before_cursor(self, count: int, text: str) -> None:
|
|
70
|
+
"""替换光标前 ``count`` 个字符为 ``text``,光标落在插入文本末尾。
|
|
71
|
+
|
|
72
|
+
语义取自 prompt_toolkit 的 ``Completion(start_position=-count)``:
|
|
73
|
+
"先删后插"让光标位置由替换结果推导,而不是调用方手算。``count``
|
|
74
|
+
超过实际可用字符时按可用数钳制,不会对光标之前的行首内容越界删除。
|
|
75
|
+
"""
|
|
76
|
+
count = max(0, min(count, self._cursor))
|
|
77
|
+
before = self._value[: self._cursor - count]
|
|
78
|
+
after = self._value[self._cursor :]
|
|
79
|
+
self._value = before + text + after
|
|
80
|
+
self._cursor = len(before) + len(text)
|
|
81
|
+
|
|
82
|
+
def clear(self) -> None:
|
|
83
|
+
self._value = ""
|
|
84
|
+
self._cursor = 0
|
|
85
|
+
|
|
86
|
+
# ------------------------------------------------------------------ 键盘
|
|
87
|
+
|
|
88
|
+
def handle_input(self, data: str) -> None:
|
|
89
|
+
"""消费一段原始终端输入(raw mode 下逐键调用)。
|
|
90
|
+
|
|
91
|
+
返回键(``\\r``)提交、裸 ``\\x1b`` 走 escape 回调、``\\x7f``/``\\x08``
|
|
92
|
+
退格、``\\x1b[3~`` 前向删除、方向/Home/End 移动光标;其余转义序列
|
|
93
|
+
忽略;可打印字符插入。
|
|
94
|
+
"""
|
|
95
|
+
if data in ("\r", "\n"):
|
|
96
|
+
if self.on_submit is not None:
|
|
97
|
+
self.on_submit(self._value)
|
|
98
|
+
return
|
|
99
|
+
if data == "\x1b":
|
|
100
|
+
if self.on_escape is not None:
|
|
101
|
+
self.on_escape()
|
|
102
|
+
return
|
|
103
|
+
if data in ("\x7f", "\x08"): # backspace
|
|
104
|
+
if self._cursor > 0:
|
|
105
|
+
self._value = self._value[: self._cursor - 1] + self._value[self._cursor :]
|
|
106
|
+
self._cursor -= 1
|
|
107
|
+
return
|
|
108
|
+
if data == "\x1b[3~": # delete(前向)
|
|
109
|
+
if self._cursor < len(self._value):
|
|
110
|
+
self._value = self._value[: self._cursor] + self._value[self._cursor + 1 :]
|
|
111
|
+
return
|
|
112
|
+
if data == "\x1b[D": # left
|
|
113
|
+
self._cursor = max(0, self._cursor - 1)
|
|
114
|
+
return
|
|
115
|
+
if data == "\x1b[C": # right
|
|
116
|
+
self._cursor = min(len(self._value), self._cursor + 1)
|
|
117
|
+
return
|
|
118
|
+
if data in ("\x1b[H", "\x1b[1~"): # home
|
|
119
|
+
self._cursor = 0
|
|
120
|
+
return
|
|
121
|
+
if data in ("\x1b[F", "\x1b[4~"): # end
|
|
122
|
+
self._cursor = len(self._value)
|
|
123
|
+
return
|
|
124
|
+
if data.startswith("\x1b"):
|
|
125
|
+
return # 其余转义序列(功能键、修饰键)不消费
|
|
126
|
+
if data.strip() or data == " ":
|
|
127
|
+
self._value = self._value[: self._cursor] + data + self._value[self._cursor :]
|
|
128
|
+
self._cursor += len(data)
|
|
129
|
+
|
|
130
|
+
# ------------------------------------------------------------------ 渲染
|
|
131
|
+
|
|
132
|
+
def render(self, width: int) -> list[str]:
|
|
133
|
+
if self._value:
|
|
134
|
+
display = self._prompt + self._value
|
|
135
|
+
cursor_index = len(self._prompt) + self._cursor
|
|
136
|
+
else:
|
|
137
|
+
placeholder = self._placeholder
|
|
138
|
+
styled = f"\x1b[90m{placeholder}\x1b[39m" if placeholder else ""
|
|
139
|
+
display = self._prompt + styled
|
|
140
|
+
cursor_index = len(self._prompt)
|
|
141
|
+
line = self._render_line(display, cursor_index, width)
|
|
142
|
+
return [pad_to_width(line, width)]
|
|
143
|
+
|
|
144
|
+
def invalidate(self) -> None:
|
|
145
|
+
return # 无缓存
|
|
146
|
+
|
|
147
|
+
def _render_line(self, display: str, cursor_index: int, width: int) -> str:
|
|
148
|
+
"""带水平滚动的单行渲染:窗口保证光标可见,marker 插在光标前。
|
|
149
|
+
|
|
150
|
+
滚动按可见列计算,起点对齐到字符边界:2 列的宽字符不会被切成
|
|
151
|
+
两半(那会让后半以 1 列错位显示)。
|
|
152
|
+
"""
|
|
153
|
+
cursor_col = visible_width(display[:cursor_index])
|
|
154
|
+
if cursor_col < width:
|
|
155
|
+
dropped = 0
|
|
156
|
+
else:
|
|
157
|
+
# 光标在窗口外:右对齐窗口,让光标落在最后一列。
|
|
158
|
+
dropped = _window_start_index(display, cursor_col - (width - 1))
|
|
159
|
+
prefix = display[dropped:cursor_index]
|
|
160
|
+
suffix = display[cursor_index:]
|
|
161
|
+
line = prefix + (CURSOR_MARKER if self.focused else "") + suffix
|
|
162
|
+
if visible_width(line) > width:
|
|
163
|
+
line = truncate_to_width(line, width)
|
|
164
|
+
return line
|
|
165
|
+
|
|
166
|
+
|
|
167
|
+
def _window_start_index(display: str, target_col: int) -> int:
|
|
168
|
+
"""累计可见宽度首次越过 ``target_col`` 的 codepoint 索引(字符边界)。
|
|
169
|
+
|
|
170
|
+
ANSI 序列零宽直接跳过;宽字符跨越边界时整个保留(从它开始)。
|
|
171
|
+
"""
|
|
172
|
+
col = 0
|
|
173
|
+
i = 0
|
|
174
|
+
while i < len(display):
|
|
175
|
+
ch = display[i]
|
|
176
|
+
if ch == "\x1b":
|
|
177
|
+
length = ansi_code_length(display, i)
|
|
178
|
+
if length > 0:
|
|
179
|
+
i += length
|
|
180
|
+
continue
|
|
181
|
+
w = char_width(ch)
|
|
182
|
+
if col + w > target_col:
|
|
183
|
+
return i
|
|
184
|
+
col += w
|
|
185
|
+
i += 1
|
|
186
|
+
return i
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
"""加载指示器。
|
|
2
|
+
|
|
3
|
+
动画由调用方通过 ``advance()`` 驱动(事件循环 / 测试手动 tick),保证确定性:
|
|
4
|
+
渲染库不拥有定时器,谁拥有帧率谁负责推进。
|
|
5
|
+
"""
|
|
6
|
+
|
|
7
|
+
from __future__ import annotations
|
|
8
|
+
|
|
9
|
+
from collections.abc import Sequence
|
|
10
|
+
|
|
11
|
+
from braid_tui.components.text import Text
|
|
12
|
+
|
|
13
|
+
__all__ = ["DEFAULT_FRAMES", "Loader"]
|
|
14
|
+
|
|
15
|
+
#: 盲文 spinner(十帧)。
|
|
16
|
+
DEFAULT_FRAMES: tuple[str, ...] = ("⠋", "⠙", "⠹", "⠸", "⠼", "⠴", "⠦", "⠧", "⠇", "⠏")
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
class Loader(Text):
|
|
20
|
+
"""spinner + 消息的单行指示器;渲染时上方多留一个空行。"""
|
|
21
|
+
|
|
22
|
+
def __init__(
|
|
23
|
+
self,
|
|
24
|
+
message: str = "Loading...",
|
|
25
|
+
*,
|
|
26
|
+
frames: Sequence[str] = DEFAULT_FRAMES,
|
|
27
|
+
padding_x: int = 1,
|
|
28
|
+
padding_y: int = 0,
|
|
29
|
+
) -> None:
|
|
30
|
+
# Loader 自身管理文本,padding_y 固定 0(上方空行由 render 补)。
|
|
31
|
+
super().__init__("", padding_x=padding_x, padding_y=padding_y)
|
|
32
|
+
self._frames: tuple[str, ...] = tuple(frames) if frames else DEFAULT_FRAMES
|
|
33
|
+
self._frame_index = 0
|
|
34
|
+
self._message = message
|
|
35
|
+
self._update()
|
|
36
|
+
|
|
37
|
+
def set_message(self, message: str) -> None:
|
|
38
|
+
if message == self._message:
|
|
39
|
+
return
|
|
40
|
+
self._message = message
|
|
41
|
+
self._update()
|
|
42
|
+
|
|
43
|
+
def advance(self) -> None:
|
|
44
|
+
"""推进到下一帧(由调用方驱动)。"""
|
|
45
|
+
self._frame_index = (self._frame_index + 1) % len(self._frames)
|
|
46
|
+
self._update()
|
|
47
|
+
|
|
48
|
+
def invalidate(self) -> None:
|
|
49
|
+
super().invalidate()
|
|
50
|
+
self._update()
|
|
51
|
+
|
|
52
|
+
def render(self, width: int) -> list[str]:
|
|
53
|
+
return ["", *super().render(width)]
|
|
54
|
+
|
|
55
|
+
def _update(self) -> None:
|
|
56
|
+
frame = self._frames[self._frame_index]
|
|
57
|
+
prefix = f"{frame} " if frame else ""
|
|
58
|
+
self.set_text(prefix + self._message)
|
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
"""滚动视口。
|
|
2
|
+
|
|
3
|
+
视口高度是构造参数,``update_layout`` 保留给 resize 场景。滚动语义:
|
|
4
|
+
|
|
5
|
+
- ``scroll_to`` 钳制到 ``[0, content - viewport]``;
|
|
6
|
+
- ``follow_end=True`` 时内容增长自动贴底;
|
|
7
|
+
- render 输出**恒定** viewport_height 行(不足补空行)——这正是
|
|
8
|
+
"滚动不触发全量重绘"的前提:改 scrollTop 只改变视口内的行,
|
|
9
|
+
帧行数不变,渲染器走纯行 diff。
|
|
10
|
+
"""
|
|
11
|
+
|
|
12
|
+
from __future__ import annotations
|
|
13
|
+
|
|
14
|
+
from braid_tui.renderer import Component
|
|
15
|
+
|
|
16
|
+
__all__ = ["ScrollView"]
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
class ScrollView:
|
|
20
|
+
"""单子组件的垂直滚动视口。"""
|
|
21
|
+
|
|
22
|
+
def __init__(
|
|
23
|
+
self,
|
|
24
|
+
child: Component,
|
|
25
|
+
*,
|
|
26
|
+
viewport_height: int = 10,
|
|
27
|
+
follow_end: bool = False,
|
|
28
|
+
) -> None:
|
|
29
|
+
self._child = child
|
|
30
|
+
self._viewport_height = viewport_height
|
|
31
|
+
self._follow_end = follow_end
|
|
32
|
+
self._scroll_top = 0
|
|
33
|
+
self._content_height = 0
|
|
34
|
+
|
|
35
|
+
# ------------------------------------------------------------------ 状态
|
|
36
|
+
|
|
37
|
+
@property
|
|
38
|
+
def scroll_top(self) -> int:
|
|
39
|
+
return self._scroll_top
|
|
40
|
+
|
|
41
|
+
@property
|
|
42
|
+
def viewport_height(self) -> int:
|
|
43
|
+
return self._viewport_height
|
|
44
|
+
|
|
45
|
+
@property
|
|
46
|
+
def content_height(self) -> int:
|
|
47
|
+
return self._content_height
|
|
48
|
+
|
|
49
|
+
@property
|
|
50
|
+
def follow_end(self) -> bool:
|
|
51
|
+
return self._follow_end
|
|
52
|
+
|
|
53
|
+
def scroll_to(self, top: int) -> None:
|
|
54
|
+
"""滚动到 ``top``(钳制到合法区间)。"""
|
|
55
|
+
self._scroll_top = self._clamp(top)
|
|
56
|
+
|
|
57
|
+
def scroll_by(self, lines: int) -> int:
|
|
58
|
+
"""相对滚动;返回实际滚动的行数。"""
|
|
59
|
+
target = self._clamp(self._scroll_top + lines)
|
|
60
|
+
moved = target - self._scroll_top
|
|
61
|
+
self._scroll_top = target
|
|
62
|
+
return moved
|
|
63
|
+
|
|
64
|
+
def scroll_to_start(self) -> None:
|
|
65
|
+
self._scroll_top = 0
|
|
66
|
+
|
|
67
|
+
def scroll_to_end(self) -> None:
|
|
68
|
+
self._scroll_top = self._max_scroll_top()
|
|
69
|
+
|
|
70
|
+
def update_layout(self, *, viewport_height: int | None = None) -> None:
|
|
71
|
+
"""resize / 视口变化时调整;follow_end 模式下重新贴底。"""
|
|
72
|
+
if viewport_height is not None:
|
|
73
|
+
self._viewport_height = max(1, viewport_height)
|
|
74
|
+
if self._follow_end:
|
|
75
|
+
self._scroll_top = self._max_scroll_top()
|
|
76
|
+
else:
|
|
77
|
+
self._scroll_top = self._clamp(self._scroll_top)
|
|
78
|
+
|
|
79
|
+
# ------------------------------------------------------------------ 渲染
|
|
80
|
+
|
|
81
|
+
def render(self, width: int) -> list[str]:
|
|
82
|
+
lines = self._child.render(width)
|
|
83
|
+
self._content_height = len(lines)
|
|
84
|
+
if self._follow_end:
|
|
85
|
+
self._scroll_top = self._max_scroll_top()
|
|
86
|
+
else:
|
|
87
|
+
self._scroll_top = self._clamp(self._scroll_top)
|
|
88
|
+
top = self._scroll_top
|
|
89
|
+
visible = lines[top : top + self._viewport_height]
|
|
90
|
+
# 恒定行数:不足视口补空行(帧 diff 的稳定性契约)。
|
|
91
|
+
while len(visible) < self._viewport_height:
|
|
92
|
+
visible.append("")
|
|
93
|
+
return visible
|
|
94
|
+
|
|
95
|
+
def invalidate(self) -> None:
|
|
96
|
+
self._child.invalidate()
|
|
97
|
+
|
|
98
|
+
# ------------------------------------------------------------------ 内部
|
|
99
|
+
|
|
100
|
+
def _max_scroll_top(self) -> int:
|
|
101
|
+
return max(0, self._content_height - self._viewport_height)
|
|
102
|
+
|
|
103
|
+
def _clamp(self, top: int) -> int:
|
|
104
|
+
return max(0, min(self._max_scroll_top(), top))
|