Skip to content

Commit f5e3e38

Browse files
authored
Merge pull request #5830 from TomJGooding/feat-input-add-cursor-location-properties
feat(input): add cursor location properties
2 parents 3290999 + 891b930 commit f5e3e38

File tree

3 files changed

+13
-7
lines changed

3 files changed

+13
-7
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
2222

2323
- Exposed `CollapsibleTitle` https://github.com/Textualize/textual/pull/5810
2424
- Added `Color.hsv` property and `Color.from_hsv` class method https://github.com/Textualize/textual/pull/5803
25+
- Added `cursor_at_start` and `cursor_at_end` properties to the `Input` widget https://github.com/Textualize/textual/pull/5830
2526

2627
### Changed
2728

src/textual/widgets/_input.py

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -464,13 +464,18 @@ def _position_to_cell(self, position: int) -> int:
464464
def _cursor_offset(self) -> int:
465465
"""The cell offset of the cursor."""
466466
offset = self._position_to_cell(self.cursor_position)
467-
if self._cursor_at_end:
467+
if self.cursor_at_end:
468468
offset += 1
469469
return offset
470470

471471
@property
472-
def _cursor_at_end(self) -> bool:
473-
"""Flag to indicate if the cursor is at the end"""
472+
def cursor_at_start(self) -> bool:
473+
"""Flag to indicate if the cursor is at the start."""
474+
return self.cursor_position == 0
475+
476+
@property
477+
def cursor_at_end(self) -> bool:
478+
"""Flag to indicate if the cursor is at the end."""
474479
return self.cursor_position == len(self.value)
475480

476481
def check_consume_key(self, key: str, character: str | None) -> bool:
@@ -513,7 +518,7 @@ def _watch_cursor_blink(self, blink: bool) -> None:
513518

514519
@property
515520
def cursor_screen_offset(self) -> Offset:
516-
"""The offset of the cursor of this input in screen-space. (x, y)/(column, row)"""
521+
"""The offset of the cursor of this input in screen-space. (x, y)/(column, row)."""
517522
x, y, _width, _height = self.content_region
518523
scroll_x, _ = self.scroll_offset
519524
return Offset(x + self._cursor_offset - scroll_x, y)
@@ -640,7 +645,7 @@ def render_line(self, y: int) -> Strip:
640645
if self._cursor_visible:
641646
cursor_style = self.get_component_rich_style("input--cursor")
642647
cursor = self.cursor_position
643-
if not show_suggestion and self._cursor_at_end:
648+
if not show_suggestion and self.cursor_at_end:
644649
result.pad_right(1)
645650
result.stylize(cursor_style, cursor, cursor + 1)
646651

@@ -848,7 +853,7 @@ def action_cursor_right(self, select: bool = False) -> None:
848853
if select:
849854
self.selection = Selection(start, end + 1)
850855
else:
851-
if self._cursor_at_end and self._suggestion:
856+
if self.cursor_at_end and self._suggestion:
852857
self.value = self._suggestion
853858
self.cursor_position = len(self.value)
854859
else:

src/textual/widgets/_masked_input.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -570,7 +570,7 @@ def render_line(self, y: int) -> Strip:
570570
result.stylize(style, index, index + 1)
571571

572572
if self._cursor_visible and self.has_focus:
573-
if self._cursor_at_end:
573+
if self.cursor_at_end:
574574
result.pad_right(1)
575575
cursor_style = self.get_component_rich_style("input--cursor")
576576
cursor = self.cursor_position

0 commit comments

Comments
 (0)