Answer stability round 1: 1 🔴, 11 🟡 (0 misread); drop keys typed ahead on leaving an answer, wrap every line to the drawn width, list the routing calls, test the paths mutants survived
This commit is contained in:
+36
-28
@@ -13,14 +13,18 @@ from loop_claude.waiting import Ask, Asking, OpenInInbox, OpenOnTerminal, TALK
|
||||
ENTER = "enter"
|
||||
BACKSPACE = "backspace"
|
||||
ESCAPE = "escape"
|
||||
# Lines of a question the list shows; the answer screen shows it whole.
|
||||
# Lines of a question the list shows; the answer screen shows as much as fits.
|
||||
SHOWN_LINES = 2
|
||||
QUIT = "q"
|
||||
ANSWER_PROMPT = f"Answer ({TALK} opens claude; Enter alone, or Esc, goes back, and the loop keeps waiting):"
|
||||
INDENT = " "
|
||||
ELLIPSIS = "…"
|
||||
NARROWEST = 20
|
||||
|
||||
|
||||
@dataclasses.dataclass(frozen=True)
|
||||
class Listing:
|
||||
"""The waiting loops, as shown when the human began typing a number, which keeps a loop's number from changing under them."""
|
||||
"""The waiting loops as they were when the human began typing a number, so a loop's number doesn't change while they type it."""
|
||||
shown: tuple[Ask, ...] = ()
|
||||
typed: str = ""
|
||||
note: str = ""
|
||||
@@ -62,7 +66,8 @@ def pressed(screen: Screen, key: str) -> Screen | Send | Open | Quit:
|
||||
return picked(shown, typed)
|
||||
if key == BACKSPACE:
|
||||
return Listing(shown, typed[:-1])
|
||||
return Listing(shown, typed + key) if key.isdigit() else screen
|
||||
# isdigit would also take "²", which int() rejects.
|
||||
return Listing(shown, typed + key) if key.isdecimal() else screen
|
||||
case Answering(ask, typed):
|
||||
if key == ESCAPE or (key == ENTER and not typed.strip()):
|
||||
return Listing()
|
||||
@@ -91,47 +96,50 @@ def where_open(state: OpenOnTerminal | OpenInInbox) -> str:
|
||||
return "in loop-inbox" if isinstance(state, OpenInInbox) else "on its terminal"
|
||||
|
||||
|
||||
def listing_lines(screen: Listing, now: float, local_time: typing.Callable[[float], str], width: int) -> list[str]:
|
||||
"""The list, its last line the prompt for a number."""
|
||||
def listing_lines(screen: Listing, now: float, local_time: typing.Callable[[float], str], width: int) -> tuple[list[str], list[str]]:
|
||||
"""The list, in lines of at most width characters, and its foot: the note, what the keys do, and the prompt for a number."""
|
||||
count = len(screen.shown)
|
||||
head = "No loop waits for you." if not count else "1 loop waits for you." if count == 1 else f"{count} loops wait for you, oldest first."
|
||||
lines = [head, ""]
|
||||
lines = [*wrapped(head, width), ""]
|
||||
for number, ask in enumerate(screen.shown, 1):
|
||||
state = "" if isinstance(ask.state, Asking) else f"; claude is open {where_open(ask.state)}"
|
||||
lines.append(f"[{number}] {ask.root}, waiting since {local_time(ask.asked)} ({waited(now - ask.asked)}){state}")
|
||||
lines += cut(wrapped(ask.question, width - 4), SHOWN_LINES, " ")
|
||||
lines += wrapped(f"[{number}] {ask.root}, waiting since {local_time(ask.asked)} ({waited(now - ask.asked)}){state}", width)
|
||||
lines += cut(wrapped(ask.question, width - len(INDENT) - len(ELLIPSIS)), SHOWN_LINES)
|
||||
lines.append("")
|
||||
lines += [screen.note] if screen.note else []
|
||||
ask_for = "Type a number and Enter to answer that loop; " if count else ""
|
||||
return [*lines, f"{ask_for}{QUIT} quits loop-inbox, and the loops keep waiting.", f"> {screen.typed}"]
|
||||
return lines, [*wrapped(screen.note, width), *wrapped(f"{ask_for}{QUIT} quits loop-inbox, and the loops keep waiting.", width), *typed_after(">", screen.typed, width)]
|
||||
|
||||
|
||||
def answering_lines(screen: Answering, now: float, local_time: typing.Callable[[float], str], width: int) -> list[str]:
|
||||
"""The whole question, its last line the prompt for the answer."""
|
||||
def answering_lines(screen: Answering, now: float, local_time: typing.Callable[[float], str], width: int) -> tuple[list[str], list[str]]:
|
||||
"""The question, in lines of at most width characters, and its foot, the prompt for the answer."""
|
||||
ask = screen.ask
|
||||
return [
|
||||
f"{ask.root}, waiting since {local_time(ask.asked)} ({waited(now - ask.asked)}):",
|
||||
"",
|
||||
*wrapped(ask.question, width),
|
||||
"",
|
||||
f"Answer ({TALK} opens claude; Enter alone, or Esc, goes back, and the loop keeps waiting): {screen.typed}",
|
||||
]
|
||||
head = f"{ask.root}, waiting since {local_time(ask.asked)} ({waited(now - ask.asked)}):"
|
||||
return [*wrapped(head, width), "", *wrapped(ask.question, width), ""], typed_after(ANSWER_PROMPT, screen.typed, width)
|
||||
|
||||
|
||||
def fitted(lines: list[str], height: int) -> list[str]:
|
||||
"""lines cut to height, keeping the last, the prompt; a cut is marked with an ellipsis."""
|
||||
if len(lines) <= height:
|
||||
return lines
|
||||
return [*lines[: max(height - 2, 0)], "…", lines[-1]][-height:]
|
||||
def fitted(lines: list[str], foot: list[str], height: int) -> list[str]:
|
||||
"""lines, then foot, cut to height: lines lose their end first, and the cut is marked with an ellipsis."""
|
||||
if len(lines) + len(foot) <= height:
|
||||
return [*lines, *foot]
|
||||
return [*lines[: max(height - len(foot) - 1, 0)], ELLIPSIS, *foot][-height:]
|
||||
|
||||
|
||||
def wrapped(text: str, width: int) -> list[str]:
|
||||
return [line for paragraph in text.splitlines() for line in textwrap.wrap(paragraph, max(width, 20)) or [""]]
|
||||
return [line for paragraph in text.splitlines() for line in textwrap.wrap(paragraph, max(width, NARROWEST)) or [""]]
|
||||
|
||||
|
||||
def cut(lines: list[str], count: int, indent: str) -> list[str]:
|
||||
shown = [f"{indent}{line}" for line in lines[:count]]
|
||||
return [*shown[:-1], f"{shown[-1]}…"] if len(lines) > count else shown
|
||||
def typed_after(prompt: str, typed: str, width: int) -> list[str]:
|
||||
"""prompt wrapped to width, then what the human typed after it, cut every width characters so that each space typed shows."""
|
||||
*lines, last = wrapped(prompt, width)
|
||||
line = f"{last} {typed}"
|
||||
step = max(width, NARROWEST)
|
||||
return [*lines, *(line[start : start + step] for start in range(0, len(line), step))]
|
||||
|
||||
|
||||
def cut(lines: list[str], count: int) -> list[str]:
|
||||
"""The first count lines, indented; a cut is marked with an ellipsis."""
|
||||
shown = [f"{INDENT}{line}" for line in lines[:count]]
|
||||
return [*shown[:-1], f"{shown[-1]}{ELLIPSIS}"] if len(lines) > count else shown
|
||||
|
||||
|
||||
def waited(seconds: float) -> str:
|
||||
|
||||
Reference in New Issue
Block a user