diff --git a/oli_bot/screens/question.py b/oli_bot/screens/question.py index 59c45aa..fc4ef62 100644 --- a/oli_bot/screens/question.py +++ b/oli_bot/screens/question.py @@ -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. @@ -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; @@ -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; @@ -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; @@ -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( @@ -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}", ) diff --git a/tests/unit/test_question_tool.py b/tests/unit/test_question_tool.py index 520bf3b..54e2569 100644 --- a/tests/unit/test_question_tool.py +++ b/tests/unit/test_question_tool.py @@ -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