diff --git a/CHANGELOG.md b/CHANGELOG.md index 041c7ff433..9f4b669a73 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -19,10 +19,8 @@ and this project adheres to [Semantic Versioning](http://semver.org/). ### Added - Exposed `CollapsibleTitle` https://github.com/Textualize/textual/pull/5810 - -### Added - - Added `Color.hsv` property and `Color.from_hsv` class method https://github.com/Textualize/textual/pull/5803 +- Added `cursor_at_start` and `cursor_at_end` properties to the `Input` widget https://github.com/Textualize/textual/pull/5830 ## [3.2.0] - 2025-05-02 diff --git a/src/textual/widgets/_input.py b/src/textual/widgets/_input.py index a9e80c696b..326e535958 100644 --- a/src/textual/widgets/_input.py +++ b/src/textual/widgets/_input.py @@ -464,13 +464,18 @@ def _position_to_cell(self, position: int) -> int: def _cursor_offset(self) -> int: """The cell offset of the cursor.""" offset = self._position_to_cell(self.cursor_position) - if self._cursor_at_end: + if self.cursor_at_end: offset += 1 return offset @property - def _cursor_at_end(self) -> bool: - """Flag to indicate if the cursor is at the end""" + def cursor_at_start(self) -> bool: + """Flag to indicate if the cursor is at the start.""" + return self.cursor_position == 0 + + @property + def cursor_at_end(self) -> bool: + """Flag to indicate if the cursor is at the end.""" return self.cursor_position == len(self.value) def check_consume_key(self, key: str, character: str | None) -> bool: @@ -513,7 +518,7 @@ def _watch_cursor_blink(self, blink: bool) -> None: @property def cursor_screen_offset(self) -> Offset: - """The offset of the cursor of this input in screen-space. (x, y)/(column, row)""" + """The offset of the cursor of this input in screen-space. (x, y)/(column, row).""" x, y, _width, _height = self.content_region scroll_x, _ = self.scroll_offset return Offset(x + self._cursor_offset - scroll_x, y) @@ -640,7 +645,7 @@ def render_line(self, y: int) -> Strip: if self._cursor_visible: cursor_style = self.get_component_rich_style("input--cursor") cursor = self.cursor_position - if not show_suggestion and self._cursor_at_end: + if not show_suggestion and self.cursor_at_end: result.pad_right(1) result.stylize(cursor_style, cursor, cursor + 1) @@ -848,7 +853,7 @@ def action_cursor_right(self, select: bool = False) -> None: if select: self.selection = Selection(start, end + 1) else: - if self._cursor_at_end and self._suggestion: + if self.cursor_at_end and self._suggestion: self.value = self._suggestion self.cursor_position = len(self.value) else: diff --git a/src/textual/widgets/_masked_input.py b/src/textual/widgets/_masked_input.py index 560258f332..47bf61e445 100644 --- a/src/textual/widgets/_masked_input.py +++ b/src/textual/widgets/_masked_input.py @@ -570,7 +570,7 @@ def render_line(self, y: int) -> Strip: result.stylize(style, index, index + 1) if self._cursor_visible and self.has_focus: - if self._cursor_at_end: + if self.cursor_at_end: result.pad_right(1) cursor_style = self.get_component_rich_style("input--cursor") cursor = self.cursor_position