Enliven the conversation list and keep section headings in view
Color-code the chat picker rows: a contact's dot takes that person's chat hue (filled once talked-to, hollow before), previews light their sender names and @mentions in-hue, and a relative age column sits between the unread badge and the message text. Pin a grouped list's section heading to the top row once it scrolls off (sticky header in the frame), add PageUp/PageDown to SelectScreen, and drop arrow wraparound in the conversation picker so it doesn't jump across the section dividers. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
1 parent
aa537d5503
commit
8906e4ed53
9 files changed
+338
-78
No files matched your search
+11
-1
@@ -90,6 +90,16 @@ _BYTES_YELLOW = "bold #fde047"
|
||||
_BYTES_ORANGE = "bold #ff9500"
|
||||
|
||||
|
||||
def _sender_hue(sender: str) -> str:
|
||||
"""The stable per-sender colour a name is drawn in, keyed on the name's characters.
|
||||
|
||||
Shared by the live transcript (sender headers, ``@mentions``) and the conversation list
|
||||
(a contact's colour dot, a channel preview's inline sender), so a person reads the same
|
||||
colour everywhere. ``you`` and unknown (``·``) senders are handled by the caller.
|
||||
"""
|
||||
return _SENDER_COLORS[sum(map(ord, sender)) % len(_SENDER_COLORS)]
|
||||
|
||||
|
||||
def _split_channel_sender(text: str) -> tuple[Optional[str], str]:
|
||||
"""Split a channel message into ``(sender_name, body)`` when it carries a name prefix.
|
||||
|
||||
@@ -436,7 +446,7 @@ class ChatScreen(Screen):
|
||||
return "you" # white, out of the per-sender hue range — always easy to spot
|
||||
if sender == "·":
|
||||
return "muted"
|
||||
return _SENDER_COLORS[sum(map(ord, sender)) % len(_SENDER_COLORS)]
|
||||
return _sender_hue(sender)
|
||||
|
||||
# --- input ---------------------------------------------------------------
|
||||
|
||||
|
||||
+10
-4
@@ -53,7 +53,9 @@ class Ui:
|
||||
"""Return a progress context manager exposing ``add_task``/``advance``/``update``."""
|
||||
raise NotImplementedError
|
||||
|
||||
async def select(self, title: str, items: list, *, default: Any = None) -> Any:
|
||||
async def select(
|
||||
self, title: str, items: list, *, default: Any = None, wrap: bool = True
|
||||
) -> Any:
|
||||
"""Prompt the user to choose one item; return its value or ``None`` if cancelled."""
|
||||
raise NotImplementedError
|
||||
|
||||
@@ -164,7 +166,9 @@ class PlainUi(Ui):
|
||||
"""Build the error raised if a rich prompt is reached on the non-interactive path."""
|
||||
return RuntimeError("interactive prompts are only available in the menu")
|
||||
|
||||
async def select(self, title: str, items: list, *, default: Any = None) -> Any:
|
||||
async def select(
|
||||
self, title: str, items: list, *, default: Any = None, wrap: bool = True
|
||||
) -> Any:
|
||||
"""Unsupported in scripted CLI mode."""
|
||||
raise self._no_prompt()
|
||||
|
||||
@@ -313,9 +317,11 @@ class TuiUi(Ui):
|
||||
|
||||
# --- input ---------------------------------------------------------------
|
||||
|
||||
async def select(self, title: str, items: list, *, default: Any = None) -> Any:
|
||||
async def select(
|
||||
self, title: str, items: list, *, default: Any = None, wrap: bool = True
|
||||
) -> Any:
|
||||
"""Delegate to the session's select screen."""
|
||||
return await self.session.select(title, items, default=default)
|
||||
return await self.session.select(title, items, default=default, wrap=wrap)
|
||||
|
||||
async def select_startup(
|
||||
self,
|
||||
|
||||
+34
-10
@@ -23,7 +23,10 @@ def _visible_slice(screen: Screen, lines: list[str], viewport: int) -> tuple[lis
|
||||
|
||||
Keeps the screen's cursor line in view (for select/text screens) and clamps the scroll
|
||||
offset to the content, then pads the slice to exactly ``viewport`` rows so the panel
|
||||
always fills its allotted height.
|
||||
always fills its allotted height. When the screen offers a sticky header (a grouped list's
|
||||
section heading that has scrolled off), it is pinned to the top row: that reserves one row,
|
||||
so the cursor is kept within the remaining ``viewport - 1`` and the bottom clamp is relaxed
|
||||
by one so the final content row can still reach the last visible line.
|
||||
|
||||
Args:
|
||||
screen: The screen being rendered (its ``scroll`` is adjusted in place).
|
||||
@@ -37,16 +40,37 @@ def _visible_slice(screen: Screen, lines: list[str], viewport: int) -> tuple[lis
|
||||
if isinstance(screen, ScrollScreen):
|
||||
screen.note_viewport(viewport)
|
||||
cursor = screen.cursor_line()
|
||||
if cursor is not None:
|
||||
if cursor < screen.scroll:
|
||||
screen.scroll = cursor
|
||||
elif cursor >= screen.scroll + viewport:
|
||||
screen.scroll = cursor - viewport + 1
|
||||
screen.scroll = max(0, min(screen.scroll, max(0, total - viewport)))
|
||||
|
||||
visible = lines[screen.scroll : screen.scroll + viewport]
|
||||
more_above = screen.scroll > 0
|
||||
more_below = screen.scroll + viewport < total
|
||||
scroll = screen.scroll
|
||||
if cursor is not None:
|
||||
if cursor < scroll:
|
||||
scroll = cursor
|
||||
elif cursor >= scroll + viewport:
|
||||
scroll = cursor - viewport + 1
|
||||
scroll = max(0, min(scroll, max(0, total - viewport)))
|
||||
|
||||
# A pinned section heading takes the top row, leaving one fewer for content; nudge the
|
||||
# scroll down if the cursor would fall in that reserved row, then re-check the heading
|
||||
# (crossing a section boundary can change which one is pinned, or drop it entirely).
|
||||
sticky = screen.sticky_header(scroll) if scroll > 0 else None
|
||||
if sticky is not None:
|
||||
cap = max(1, viewport - 1)
|
||||
if cursor is not None and cursor >= scroll + cap:
|
||||
scroll = min(cursor - cap + 1, max(0, total - cap))
|
||||
sticky = screen.sticky_header(scroll) if scroll > 0 else None
|
||||
|
||||
screen.scroll = scroll
|
||||
|
||||
if sticky is not None:
|
||||
cap = max(1, viewport - 1)
|
||||
visible = [sticky] + lines[scroll : scroll + cap]
|
||||
more_below = scroll + cap < total
|
||||
visible = visible + [""] * (viewport - len(visible))
|
||||
return visible, True, more_below
|
||||
|
||||
visible = lines[scroll : scroll + viewport]
|
||||
more_above = scroll > 0
|
||||
more_below = scroll + viewport < total
|
||||
visible = visible + [""] * (viewport - len(visible))
|
||||
return visible, more_above, more_below
|
||||
|
||||
|
||||
@@ -78,6 +78,15 @@ class Screen:
|
||||
"""
|
||||
return None
|
||||
|
||||
def sticky_header(self, scroll: int) -> Optional[str]:
|
||||
"""An already-rendered body line to pin to the top row once ``scroll`` moves past it.
|
||||
|
||||
Lets a grouped list keep its current section heading in view after the heading itself
|
||||
has scrolled off. Returns an ANSI line (e.g. a section separator) to pin, or ``None``
|
||||
for no pinned header. ``scroll`` is the offset the body is about to be sliced at.
|
||||
"""
|
||||
return None
|
||||
|
||||
# --- input ---------------------------------------------------------------
|
||||
|
||||
def handle(self, action: str, data: str = "") -> None:
|
||||
|
||||
@@ -56,6 +56,10 @@ class Separator:
|
||||
|
||||
Item = "Choice | Separator"
|
||||
|
||||
#: How many rows PageUp/PageDown move the highlight. A fixed step (the screen isn't told the
|
||||
#: viewport height) that keeps paging predictable, matching the chat transcript's own paging.
|
||||
_PAGE = 10
|
||||
|
||||
|
||||
class SelectScreen(Screen):
|
||||
"""A grouped, filterable, single-choice list.
|
||||
@@ -72,6 +76,7 @@ class SelectScreen(Screen):
|
||||
default: Any = None,
|
||||
footer_hint: str = "↑↓ move · type to filter · Enter select · Esc back",
|
||||
filterable: bool = True,
|
||||
wrap: bool = True,
|
||||
) -> None:
|
||||
"""Build a select screen.
|
||||
|
||||
@@ -82,11 +87,15 @@ class SelectScreen(Screen):
|
||||
footer_hint: Footer key hint.
|
||||
filterable: Whether typing narrows the list. Off for short, fixed lists (e.g.
|
||||
the startup device picker) where type-to-filter would only get in the way.
|
||||
wrap: Whether the highlight wraps around the ends (Down from the last row jumps
|
||||
to the first, and vice versa). Off for grouped lists where wrapping across the
|
||||
section headings reads as a jarring jump rather than continuing to scroll.
|
||||
"""
|
||||
super().__init__()
|
||||
self.title = title
|
||||
self.footer_hint = footer_hint
|
||||
self._filterable = filterable
|
||||
self._wrap = wrap
|
||||
self._items = items
|
||||
self._filter = ""
|
||||
# Index into the currently-selectable (filtered) choices.
|
||||
@@ -130,11 +139,16 @@ class SelectScreen(Screen):
|
||||
selected = choices[self._index] if choices else None
|
||||
|
||||
lines: list[str] = []
|
||||
# (body-line index, rendered line) for each separator, so a section heading that
|
||||
# scrolls off can be re-pinned to the top row (see :meth:`sticky_header`).
|
||||
self._separator_lines: list[tuple[int, str]] = []
|
||||
if self._filter:
|
||||
lines.append(render_to_ansi(Text(f"/{self._filter}", style="warn"), width))
|
||||
for item in rows:
|
||||
if isinstance(item, Separator):
|
||||
lines.append(render_to_ansi(Text(item.title, style="muted"), width))
|
||||
sep = render_to_ansi(Text(item.title, style="muted"), width)
|
||||
self._separator_lines.append((len(lines), sep))
|
||||
lines.append(sep)
|
||||
continue
|
||||
is_sel = item is selected
|
||||
pointer = "❯ " if is_sel else " "
|
||||
@@ -160,15 +174,44 @@ class SelectScreen(Screen):
|
||||
"""Return the body line index of the highlighted row."""
|
||||
return getattr(self, "_cursor", None)
|
||||
|
||||
def sticky_header(self, scroll: int) -> Optional[str]:
|
||||
"""The section heading governing the top visible row, when it has scrolled off.
|
||||
|
||||
Returns the rendered separator line for the group the first visible row belongs to,
|
||||
so the "Channels"/"Direct"-style heading stays pinned as you scroll down into a long
|
||||
section. Returns ``None`` when that heading is itself still on screen (nothing to pin)
|
||||
or while filtering (headings are hidden then).
|
||||
"""
|
||||
governing: Optional[str] = None
|
||||
governing_at = -1
|
||||
for idx, line in getattr(self, "_separator_lines", ()):
|
||||
if idx <= scroll:
|
||||
governing, governing_at = line, idx
|
||||
else:
|
||||
break
|
||||
if governing is None or governing_at == scroll:
|
||||
return None # no heading above, or it's already the top visible row
|
||||
return governing
|
||||
|
||||
# --- input ---------------------------------------------------------------
|
||||
|
||||
def handle(self, action: str, data: str = "") -> None:
|
||||
"""Move the highlight, edit the filter, or commit/cancel the selection."""
|
||||
choices = self._choices()
|
||||
if action == "up":
|
||||
self._index = (self._index - 1) % len(choices) if choices else 0
|
||||
if choices:
|
||||
self._index = (self._index - 1) % len(choices) if self._wrap else max(
|
||||
0, self._index - 1
|
||||
)
|
||||
elif action == "down":
|
||||
self._index = (self._index + 1) % len(choices) if choices else 0
|
||||
if choices:
|
||||
self._index = (self._index + 1) % len(choices) if self._wrap else min(
|
||||
len(choices) - 1, self._index + 1
|
||||
)
|
||||
elif action == "pageup":
|
||||
self._index = max(0, self._index - _PAGE)
|
||||
elif action == "pagedown":
|
||||
self._index = min(len(choices) - 1, self._index + _PAGE) if choices else 0
|
||||
elif action == "home":
|
||||
self._index = 0
|
||||
elif action == "end":
|
||||
|
||||
@@ -125,9 +125,13 @@ class TuiSession:
|
||||
finally:
|
||||
self.pop(screen)
|
||||
|
||||
async def select(self, title: str, items: list, *, default: Any = None) -> Any:
|
||||
async def select(
|
||||
self, title: str, items: list, *, default: Any = None, wrap: bool = True
|
||||
) -> Any:
|
||||
"""Show a select screen; return the chosen value or ``None`` if cancelled."""
|
||||
result = await self.run_screen(SelectScreen(title, items, default=default))
|
||||
result = await self.run_screen(
|
||||
SelectScreen(title, items, default=default, wrap=wrap)
|
||||
)
|
||||
return None if result is CANCEL else result
|
||||
|
||||
async def select_startup(
|
||||
|
||||
Reference in new issue
Block a user