Files
claude-config/bin/loop_inbox/inbox.py
T

146 lines
4.9 KiB
Python

"""What loop-inbox shows, and what each key does there: the core that main.py's curses screen runs.
How it shows the loops and a question was picked on 2026-10-28 by a bench of three readers seated as the README's
audience, as ~/.claude/CLAUDE.md → "Working loop" → "Bench a syntax with the people who write it." has it: a block per
loop with its checkout's path and how long it has waited, and an answer prompt saying that Enter alone doesn't stop the loop.
"""
import dataclasses
import textwrap
import typing
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.
SHOWN_LINES = 2
QUIT = "q"
@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."""
shown: tuple[Ask, ...] = ()
typed: str = ""
note: str = ""
@dataclasses.dataclass(frozen=True)
class Answering:
ask: Ask
typed: str = ""
Screen = Listing | Answering
@dataclasses.dataclass(frozen=True)
class Send:
ask: Ask
text: str
@dataclasses.dataclass(frozen=True)
class Open:
"""Opens the worker's claude session for /talk."""
ask: Ask
@dataclasses.dataclass(frozen=True)
class Quit:
pass
def pressed(screen: Screen, key: str) -> Screen | Send | Open | Quit:
"""What follows key, which is ENTER, BACKSPACE, ESCAPE or one character typed."""
match screen:
case Listing(shown, typed):
if key == QUIT and not typed:
return Quit()
if key == ENTER:
return picked(shown, typed)
if key == BACKSPACE:
return Listing(shown, typed[:-1])
return Listing(shown, typed + key) if key.isdigit() else screen
case Answering(ask, typed):
if key == ESCAPE or (key == ENTER and not typed.strip()):
return Listing()
if key == ENTER:
return Open(ask) if typed.strip() == TALK else Send(ask, typed.strip())
if key == BACKSPACE:
return Answering(ask, typed[:-1])
return Answering(ask, typed + key) if len(key) == 1 and key.isprintable() else screen
case _:
typing.assert_never(screen)
def picked(shown: tuple[Ask, ...], typed: str) -> Listing | Answering:
if not typed:
return Listing()
number = int(typed)
if not 1 <= number <= len(shown):
return Listing(note=f"No loop has the number {number}.")
ask = shown[number - 1]
if not isinstance(ask.state, Asking):
return Listing(note=f"{ask.root}: claude is open {where_open(ask.state)}; answer there.")
return Answering(ask)
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."""
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, ""]
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.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}"]
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."""
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}",
]
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 wrapped(text: str, width: int) -> list[str]:
return [line for paragraph in text.splitlines() for line in textwrap.wrap(paragraph, max(width, 20)) 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 waited(seconds: float) -> str:
minutes = int(seconds // 60)
if minutes < 60:
return f"{minutes} min"
hours, minutes = divmod(minutes, 60)
if hours < 24:
return f"{hours} h {minutes} min"
days, hours = divmod(hours, 24)
return f"{days} d {hours} h"