Skip to content

Commit

Permalink
allow reverse order
Browse files Browse the repository at this point in the history
  • Loading branch information
willmcgugan committed Dec 19, 2024
1 parent 5364fc7 commit cbe63ca
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
4 changes: 3 additions & 1 deletion src/textual/screen.py
Original file line number Diff line number Diff line change
Expand Up @@ -1469,7 +1469,9 @@ def watch__select_end(self, select_end: tuple[Widget, Offset] | None) -> None:

start_widget, start_offset = self._select_start
end_widget, end_offset = select_end
self.selections = {start_widget: Selection(start_offset, end_offset)}
self.selections = {
start_widget: Selection.from_offsets(start_offset, end_offset)
}

def dismiss(self, result: ScreenResultType | None = None) -> AwaitComplete:
"""Dismiss the screen, optionally with a result.
Expand Down
16 changes: 16 additions & 0 deletions src/textual/selection.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from __future__ import annotations

from typing import NamedTuple

from textual.geometry import Offset
Expand All @@ -11,6 +13,20 @@ class Selection(NamedTuple):
end: Offset | None
"""Offset or None for `end`."""

@classmethod
def from_offsets(cls, offset1: Offset, offset2: Offset) -> Selection:
"""Create selection from 2 offsets.
Args:
offset1: First offset.
offset2: Second offset.
Returns:
New Selection.
"""
offsets = sorted([offset1, offset2], key=(lambda offset: (offset.y, offset.x)))
return cls(*offsets)

def get_span(self, y: int) -> tuple[int, int] | None:
"""Get the selected span in a given line.
Expand Down

0 comments on commit cbe63ca

Please sign in to comment.