Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 28 additions & 2 deletions oli_bot/screens/question.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,26 @@

from textual.app import ComposeResult
from textual.containers import Container, Horizontal, VerticalScroll
from textual.geometry import Size
from textual.screen import ModalScreen
from textual.widgets import Button, Input, Label, RadioButton, RadioSet

from ..tools.question import _resolve_recommended


class WrappingRadioButton(RadioButton):
"""A ``RadioButton`` whose label wraps instead of being ellipsized.

Textual's ``ToggleButton`` (the base of ``RadioButton``) hardcodes
``get_content_height()`` to 1 and defaults to ``nowrap``/``ellipsis``, so
long option text is clipped. Overriding the content height from the wrapped
render lets the option grow to as many lines as it needs.
"""

def get_content_height(self, container: Size, viewport: Size, width: int) -> int:
return max(1, len(self.render().wrap(width)))


class QuestionScreen(ModalScreen[str | None]):
"""Present questions posed by the agent and collect the user's answers.

Expand All @@ -24,7 +38,8 @@ class QuestionScreen(ModalScreen[str | None]):
#question-container {
width: 90%;
max-width: 78;
height: 80%;
height: auto;
max-height: 90%;
border: round $primary;
background: $surface;
padding: 1;
Expand All @@ -48,10 +63,14 @@ class QuestionScreen(ModalScreen[str | None]):
.question-text {
text-style: bold;
width: 1fr;
text-wrap: wrap;
text-overflow: fold;
}
.question-meta {
color: $text-muted;
width: 1fr;
text-wrap: wrap;
text-overflow: fold;
}
RadioSet {
margin: 0 0 1 0;
Expand All @@ -61,6 +80,11 @@ class QuestionScreen(ModalScreen[str | None]):
RadioSet:focus {
border: round $primary;
}
WrappingRadioButton {
text-wrap: wrap;
text-overflow: fold;
height: auto;
}
.question-own-input {
margin: 1 0 1 0;
width: 1fr;
Expand Down Expand Up @@ -89,6 +113,7 @@ def compose(self) -> ComposeResult:
yield Label(
f"{i + 1}. {q.get('question', '')}",
classes="question-text",
markup=False,
)
if q.get("options"):
idx, freeform = _resolve_recommended(
Expand All @@ -98,10 +123,11 @@ def compose(self) -> ComposeResult:
yield Label(
f"[Recommended action: {freeform}]",
classes="question-meta",
markup=False,
)
yield RadioSet(
*[
RadioButton(
WrappingRadioButton(
str(opt),
id=f"q{i}-opt{j}",
)
Expand Down
42 changes: 42 additions & 0 deletions tests/unit/test_question_tool.py
Original file line number Diff line number Diff line change
Expand Up @@ -215,3 +215,45 @@ async def test_question_screen_cancel_returns_none():
await pilot.press("escape")
await pilot.pause()
assert app.result is None


# ---------- full question/option text is visible --------------------------------


async def test_question_screen_preserves_bracketed_text():
question = "Pick [A]/[B] or [1] then press [Enter] to confirm"
app = _QuestionHost([{"question": question}])
async with app.run_test():
label = app.screen.query_one(".question-text")
rendered = str(label.render())
for token in ("[A]", "[B]", "[1]", "[Enter]"):
assert token in rendered
assert f"1. {question}" == rendered


async def test_question_screen_wraps_long_question():
question = "Long question " + "with a great deal of context " * 8
app = _QuestionHost([{"question": question}])
async with app.run_test(size=(60, 30)):
label = app.screen.query_one(".question-text")
assert label.size.height > 1
assert str(label.render()) == f"1. {question}"


async def test_question_screen_wraps_long_option():
option = "No, wait until the replicas are fully upgraded and tested in staging first"
app = _QuestionHost([{"question": "Proceed?", "options": [option, "Yes"]}])
async with app.run_test(size=(60, 30)):
radio = app.screen.query_one("#q0-opt0")
assert radio.size.height > 1, "long option should wrap onto multiple lines"
assert option in str(radio.render())


async def test_question_screen_result_keeps_full_bracketed_question():
question = "Pick [A]/[B] or choose option [3]?"
app = _QuestionHost([{"question": question}])
async with app.run_test() as pilot:
await pilot.click("#question-confirm")
await pilot.pause()
assert app.result is not None
assert f"Question 1: {question}" in app.result
Loading