|
| 1 | +"""Bookmark search and visit commands for the command palette.""" |
| 2 | + |
| 3 | +############################################################################## |
| 4 | +# Python imports. |
| 5 | +from dataclasses import dataclass |
| 6 | +from functools import total_ordering |
| 7 | +from typing import Final |
| 8 | + |
| 9 | +############################################################################## |
| 10 | +# httpx imports. |
| 11 | +from httpx import URL |
| 12 | + |
| 13 | +############################################################################## |
| 14 | +# Rich imports. |
| 15 | +from rich.emoji import Emoji |
| 16 | + |
| 17 | +############################################################################## |
| 18 | +# Textual enhanced imports. |
| 19 | +from textual_enhanced.commands import CommandHit, CommandHits, CommandsProvider |
| 20 | + |
| 21 | +############################################################################## |
| 22 | +# Local imports. |
| 23 | +from ..messages import OpenLocation |
| 24 | +from ..types import HikeHistory, HikeLocation |
| 25 | + |
| 26 | +############################################################################## |
| 27 | +# Icons. |
| 28 | +LOCAL_FILE: Final[str] = Emoji.replace(":page_facing_up:") |
| 29 | +"""Icon to use for a local file.""" |
| 30 | +REMOTE_FILE: Final[str] = Emoji.replace(":globe_with_meridians:") |
| 31 | +"""icon to sue for a remote file.""" |
| 32 | + |
| 33 | + |
| 34 | +############################################################################## |
| 35 | +@dataclass(frozen=True) |
| 36 | +@total_ordering |
| 37 | +class Historical: |
| 38 | + """Holds a location from history.""" |
| 39 | + |
| 40 | + location: HikeLocation |
| 41 | + """The location.""" |
| 42 | + |
| 43 | + @property |
| 44 | + def name(self) -> str: |
| 45 | + """A name for the location.""" |
| 46 | + if isinstance(self.location, URL): |
| 47 | + return self.location.path |
| 48 | + return str(self.location) |
| 49 | + |
| 50 | + @property |
| 51 | + def context(self) -> str: |
| 52 | + """The context for the location.""" |
| 53 | + if isinstance(self.location, URL): |
| 54 | + return f"{REMOTE_FILE} Remote on {self.location.host}" |
| 55 | + return f"{LOCAL_FILE} Local file" |
| 56 | + |
| 57 | + def __gt__(self, value: object, /) -> bool: |
| 58 | + if isinstance(value, Historical): |
| 59 | + return self.name.casefold() > value.name.casefold() |
| 60 | + raise NotImplementedError |
| 61 | + |
| 62 | + def __eq__(self, value: object, /) -> bool: |
| 63 | + if isinstance(value, Historical): |
| 64 | + return self.name == value.name |
| 65 | + raise NotImplementedError |
| 66 | + |
| 67 | + def __str__(self) -> str: |
| 68 | + return self.name |
| 69 | + |
| 70 | + |
| 71 | +############################################################################## |
| 72 | +class HistoryCommands(CommandsProvider): |
| 73 | + """A command palette provider related to history.""" |
| 74 | + |
| 75 | + history: HikeHistory = HikeHistory() |
| 76 | + """The history.""" |
| 77 | + |
| 78 | + @classmethod |
| 79 | + def prompt(cls) -> str: |
| 80 | + """The prompt for the command provider.""" |
| 81 | + return "Search history..." |
| 82 | + |
| 83 | + def commands(self) -> CommandHits: |
| 84 | + """Provide the history-based command data for the command palette. |
| 85 | +
|
| 86 | + Yields: |
| 87 | + The commands for the command palette. |
| 88 | + """ |
| 89 | + for location in sorted(set(Historical(location) for location in self.history)): |
| 90 | + # TODO: Improve what's shown in the palette. |
| 91 | + yield CommandHit( |
| 92 | + location.name, |
| 93 | + location.context, |
| 94 | + OpenLocation(location.location), |
| 95 | + ) |
| 96 | + |
| 97 | + |
| 98 | +### history.py ends here |
0 commit comments