-
Notifications
You must be signed in to change notification settings - Fork 819
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
88b2638
commit 82d0025
Showing
5 changed files
with
85 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
from typing import NamedTuple | ||
|
||
from textual.geometry import Offset | ||
|
||
|
||
class Selection(NamedTuple): | ||
"""A selected range of lines.""" | ||
|
||
start: Offset | None | ||
"""Offset or None for `start`.""" | ||
end: Offset | None | ||
"""Offset or None for `end`.""" | ||
|
||
def get_span(self, y: int) -> tuple[int, int] | None: | ||
"""Get the selected span in a given line. | ||
Args: | ||
y: Offset of the line. | ||
Returns: | ||
A tuple of x start and end offset, or None for no selection. | ||
""" | ||
start, end = self | ||
if start is None and end is None: | ||
# Selection covers everything | ||
return 0, -1 | ||
|
||
if start is not None and end is not None: | ||
if y < start.y or y > end.y: | ||
# Outside | ||
return None | ||
if y == start.y == end.y: | ||
# Same line | ||
return start.x, end.x | ||
if y == end.y: | ||
# Last line | ||
return 0, end.x | ||
# Remaining lines | ||
return start.x, -1 | ||
|
||
if start is None and end is not None: | ||
if y == end.y: | ||
return 0, end.x | ||
return 0, -1 | ||
|
||
if end is None and start is not None: | ||
if y == start.y: | ||
return start.x, -1 | ||
return 0, -1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters