Skip to content

Commit

Permalink
🔀 Merge pull request #55 from davep/tidy-commands
Browse files Browse the repository at this point in the history
Tidy up everything to do with messages, commands and command providers
  • Loading branch information
davep authored Jan 8, 2025
2 parents a25897e + 3b4ae06 commit 8cf79c4
Show file tree
Hide file tree
Showing 15 changed files with 140 additions and 110 deletions.
64 changes: 58 additions & 6 deletions src/braindrop/app/commands/__init__.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,66 @@
"""Provides the command palette command provides for the application."""
"""Provides command-oriented messages for the application.
These messages differ a little from other messages in that they have a
common base class and provide information such as help text, binding
information, etc.
"""

##############################################################################
# Local imports.
from .collections import CollectionCommands
from .commands_provider import CommandsProvider
from .main import MainCommands
from .tags import TagCommands
from .base import Command
from .commands import (
AddRaindrop,
ChangeTheme,
CheckTheWaybackMachine,
ClearFilters,
CompactMode,
CopyLinkToClipboard,
DeleteRaindrop,
Details,
EditRaindrop,
Escape,
Help,
Logout,
Quit,
Redownload,
Search,
SearchCollections,
SearchTags,
ShowAll,
ShowUnsorted,
ShowUntagged,
TagOrder,
VisitLink,
VisitRaindrop,
)

##############################################################################
# Exports.
__all__ = ["CollectionCommands", "CommandsProvider", "MainCommands", "TagCommands"]
__all__ = [
"Command",
"AddRaindrop",
"ChangeTheme",
"CheckTheWaybackMachine",
"ClearFilters",
"CompactMode",
"CopyLinkToClipboard",
"DeleteRaindrop",
"Details",
"EditRaindrop",
"Escape",
"Help",
"Logout",
"Quit",
"Redownload",
"Search",
"SearchCollections",
"SearchTags",
"ShowAll",
"ShowUnsorted",
"ShowUntagged",
"TagOrder",
"VisitLink",
"VisitRaindrop",
]

### __init__.py ends here
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,8 @@

##############################################################################
# Local imports.
from ...raindrop import Collection, Tag
from ..data import Raindrops
from .base_command import Command
from .base import Command


##############################################################################
Expand All @@ -19,19 +18,10 @@ class SearchCollections(Command):
SHOW_IN_FOOTER = False


##############################################################################
@dataclass
class ShowCollection(Command):
"""A message that requests that a particular collection is shown."""

collection: Collection
"""The collection to show."""


##############################################################################
@dataclass
class SearchTags(Command):
"""A message that requests that the tag-based command palette is shown."""
"""A message that requests that the tag-based command palette is shown"""

BINDING_KEY = "t"
SHOW_IN_FOOTER = False
Expand All @@ -54,15 +44,6 @@ def context_tooltip(self) -> str:
)


##############################################################################
@dataclass
class ShowTagged(Command):
"""A message that requests that Raindrops with a particular tag are shown."""

tag: Tag
"""The tag to show."""


##############################################################################
class Logout(Command):
"""Forget your API token and remove the local raindrop cache"""
Expand Down
53 changes: 1 addition & 52 deletions src/braindrop/app/messages/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,64 +2,13 @@

##############################################################################
# Local imports.
from .base_command import Command
from .commands import (
AddRaindrop,
ChangeTheme,
CheckTheWaybackMachine,
ClearFilters,
CompactMode,
CopyLinkToClipboard,
DeleteRaindrop,
Details,
EditRaindrop,
Escape,
Help,
Logout,
Quit,
Redownload,
Search,
SearchCollections,
SearchTags,
ShowAll,
ShowCollection,
ShowTagged,
ShowUnsorted,
ShowUntagged,
TagOrder,
VisitLink,
VisitRaindrop,
)
from .main import ShowCollection, ShowTagged

##############################################################################
# Exports.
__all__ = [
"AddRaindrop",
"ChangeTheme",
"CheckTheWaybackMachine",
"ClearFilters",
"Command",
"CompactMode",
"CopyLinkToClipboard",
"DeleteRaindrop",
"Details",
"EditRaindrop",
"Escape",
"Help",
"Logout",
"Quit",
"Redownload",
"Search",
"SearchCollections",
"SearchTags",
"ShowAll",
"ShowCollection",
"ShowTagged",
"ShowUnsorted",
"ShowUntagged",
"TagOrder",
"VisitLink",
"VisitRaindrop",
]

### __init__.py ends here
34 changes: 34 additions & 0 deletions src/braindrop/app/messages/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
"""The main application messages."""

##############################################################################
# Python imports.
from dataclasses import dataclass

##############################################################################
# Textual imports.
from textual.message import Message

##############################################################################
# Local imports.
from ...raindrop import Collection, Tag


##############################################################################
@dataclass
class ShowCollection(Message):
"""A message that requests that a particular collection is shown."""

collection: Collection
"""The collection to show."""


##############################################################################
@dataclass
class ShowTagged(Message):
"""A message that requests that Raindrops with a particular tag are shown."""

tag: Tag
"""The tag to show."""


### main.py ends here
14 changes: 14 additions & 0 deletions src/braindrop/app/providers/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
"""Provides the command palette command provides for the application."""

##############################################################################
# Local imports.
from .collections import CollectionCommands
from .commands_provider import CommandsProvider
from .main import MainCommands
from .tags import TagCommands

##############################################################################
# Exports.
__all__ = ["CollectionCommands", "CommandsProvider", "MainCommands", "TagCommands"]

### __init__.py ends here
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,11 @@
##############################################################################
# Textual imports.
from textual.command import DiscoveryHit, Hit, Hits, Provider
from textual.message import Message

##############################################################################
# Local imports.
from ..messages import Command
from ..commands import Command


##############################################################################
Expand All @@ -27,7 +28,7 @@ class CommandHit(NamedTuple):
"""The command."""
description: str
"""The description of the command."""
message: Command
message: Message
"""The message to emit when the command is chosen."""


Expand Down Expand Up @@ -64,28 +65,26 @@ def _commands(self) -> Iterator[CommandHit]:
for command in self.commands()
)

def _maybe_add_binding(self, command: Command, text: str | Text) -> Text:
def _maybe_add_binding(self, message: Command | Message, text: str | Text) -> Text:
"""Maybe add binding details to some text.
Args:
command: The command message to get the binding for.
message: The command message to maybe get the binding for.
text: The text to add the binding details to.
Returns:
The resulting text.
"""
if isinstance(text, str):
text = Text(text)
if not isinstance(message, Command) or not message.has_binding:
return text
style = self.app.current_theme.accent if self.app.current_theme else None
return (
text.append_text(Text(" ")).append_text(
Text(
f"[{self.app.get_key_display(command.primary_binding())}]",
style=style or "dim",
)
return text.append_text(Text(" ")).append_text(
Text(
f"[{self.app.get_key_display(message.primary_binding())}]",
style=style or "dim",
)
if command.has_binding
else text
)

async def discover(self) -> Hits:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@

##############################################################################
# Local imports.
from ..data import Raindrops
from ..messages import (
from ..commands import (
AddRaindrop,
ChangeTheme,
CheckTheWaybackMachine,
Expand All @@ -28,6 +27,7 @@
VisitLink,
VisitRaindrop,
)
from ..data import Raindrops
from .commands_provider import CommandHits, CommandsProvider


Expand Down
File renamed without changes.
2 changes: 1 addition & 1 deletion src/braindrop/app/screens/help.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
##############################################################################
# Local imports.
from ... import __version__
from ..messages import Command
from ..commands import Command

##############################################################################
# The help text.
Expand Down
25 changes: 12 additions & 13 deletions src/braindrop/app/screens/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,17 +28,7 @@
# Local imports.
from ... import __version__
from ...raindrop import API, Raindrop, SpecialCollection, User
from ..commands import CollectionCommands, CommandsProvider, MainCommands, TagCommands
from ..data import (
ExitState,
LocalData,
Raindrops,
load_configuration,
local_data_file,
save_configuration,
token_file,
)
from ..messages import (
from ..commands import (
AddRaindrop,
ChangeTheme,
CheckTheWaybackMachine,
Expand All @@ -58,14 +48,23 @@
SearchCollections,
SearchTags,
ShowAll,
ShowCollection,
ShowTagged,
ShowUnsorted,
ShowUntagged,
TagOrder,
VisitLink,
VisitRaindrop,
)
from ..data import (
ExitState,
LocalData,
Raindrops,
load_configuration,
local_data_file,
save_configuration,
token_file,
)
from ..messages import ShowCollection, ShowTagged
from ..providers import CollectionCommands, CommandsProvider, MainCommands, TagCommands
from ..widgets import Navigation, RaindropDetails, RaindropsView
from .confirm import Confirm
from .downloading import Downloading
Expand Down
3 changes: 2 additions & 1 deletion src/braindrop/app/widgets/navigation.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,9 @@
##############################################################################
# Local imports.
from ...raindrop import API, Collection, SpecialCollection, Tag, TagData
from ..commands import ShowAll, ShowUnsorted, ShowUntagged
from ..data import LocalData, Raindrops
from ..messages import ShowAll, ShowCollection, ShowTagged, ShowUnsorted, ShowUntagged
from ..messages import ShowCollection, ShowTagged
from .extended_option_list import OptionListEx


Expand Down
3 changes: 2 additions & 1 deletion src/braindrop/app/widgets/raindrop_details.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,9 @@
##############################################################################
# Local imports.
from ...raindrop import Raindrop, Tag
from ..commands import VisitLink
from ..data import LocalData
from ..messages import ShowTagged, VisitLink
from ..messages import ShowTagged
from .extended_option_list import OptionListEx
from .icons import PRIVATE_ICON, PUBLIC_ICON

Expand Down
2 changes: 1 addition & 1 deletion src/braindrop/app/widgets/raindrops_view.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@
##############################################################################
# Local imports.
from ...raindrop import Raindrop
from ..commands import VisitLink
from ..data import LocalData, Raindrops
from ..messages import VisitLink
from .extended_option_list import OptionListEx
from .icons import BROKEN_ICON, PRIVATE_ICON, PUBLIC_ICON, UNSORTED_ICON

Expand Down

0 comments on commit 8cf79c4

Please sign in to comment.