mirror of
https://github.com/bytedance/deer-flow.git
synced 2026-11-20 11:20:35 +00:00
* fix(tui): preserve composer on Up with empty history * fix(tui): preserve cursor and undo on identical history recalls
59 lines
2.0 KiB
Python
59 lines
2.0 KiB
Python
"""Bounded composer input history with up/down navigation (pure).
|
|
|
|
No persistence and no Textual dependency here; the app may seed/save entries
|
|
elsewhere. Navigation stashes the in-progress draft so walking back through
|
|
history and forward again restores what the user was typing.
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
DEFAULT_LIMIT = 200
|
|
|
|
|
|
class InputHistory:
|
|
def __init__(self, entries: list[str] | None = None, limit: int = DEFAULT_LIMIT) -> None:
|
|
self._limit = max(1, limit)
|
|
self._entries: list[str] = list(entries or [])[-self._limit :]
|
|
self._cursor: int | None = None # None => not navigating
|
|
self._draft: str = ""
|
|
|
|
def entries(self) -> list[str]:
|
|
return list(self._entries)
|
|
|
|
def add(self, text: str) -> None:
|
|
"""Record a submitted entry. Ignores blank and consecutive-duplicate lines."""
|
|
self._cursor = None
|
|
self._draft = ""
|
|
if not text.strip():
|
|
return
|
|
if self._entries and self._entries[-1] == text:
|
|
return
|
|
self._entries.append(text)
|
|
if len(self._entries) > self._limit:
|
|
self._entries = self._entries[-self._limit :]
|
|
|
|
def up(self, draft: str = "") -> str | None:
|
|
"""Move one entry older; return None when history is empty."""
|
|
if not self._entries:
|
|
return None
|
|
if self._cursor is None:
|
|
self._draft = draft
|
|
self._cursor = len(self._entries) - 1
|
|
elif self._cursor > 0:
|
|
self._cursor -= 1
|
|
return self._entries[self._cursor]
|
|
|
|
def down(self) -> str | None:
|
|
"""Move newer or restore the draft; return None when not navigating."""
|
|
if self._cursor is None:
|
|
return None
|
|
if self._cursor < len(self._entries) - 1:
|
|
self._cursor += 1
|
|
return self._entries[self._cursor]
|
|
self._cursor = None
|
|
return self._draft
|
|
|
|
def reset(self) -> None:
|
|
self._cursor = None
|
|
self._draft = ""
|