Skip to content

Commit

Permalink
🔀 Merge pull request #41 from davep/jump-to-obsidian-vault
Browse files Browse the repository at this point in the history
Add `obsidian` as a command for the command line
  • Loading branch information
davep authored Mar 2, 2025
2 parents 71213ac + ea69f32 commit b89f64d
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 3 deletions.
7 changes: 7 additions & 0 deletions ChangeLog.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
# Hike ChangeLog

## Unreleased

**Released: WiP**

- Added `obsidian` as a command that the command line understands.
([#41](https://github.com/davep/hike/pull/41))

## v0.5.0

**Released: 2025-02-25**
Expand Down
2 changes: 1 addition & 1 deletion src/hike/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ def get_args() -> Namespace:
# The remainder is going to be the initial command.
parser.add_argument(
"command",
help=("The initial command; " "can be any valid input to Hike's command line."),
help="The initial command; can be any valid input to Hike's command line.",
nargs="*",
)

Expand Down
3 changes: 3 additions & 0 deletions src/hike/data/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@ class Configuration:
main_branches: list[str] = field(default_factory=lambda: ["main", "master"])
"""The branches considered to be main branches on forges."""

obsidian_vaults: str = "~/Library/Mobile Documents/iCloud~md~obsidian/Documents"
"""The path to the root of all Obsidian vaults."""


##############################################################################
def configuration_file() -> Path:
Expand Down
44 changes: 44 additions & 0 deletions src/hike/widgets/command_line/obsidian.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
"""Provides a command for browsing Obsidian vaults."""

##############################################################################
# Python imports.
from pathlib import Path

##############################################################################
# Textual imports.
from textual.widget import Widget

##############################################################################
# Local imports.
from ...data import load_configuration
from ...messages import SetLocalViewRoot
from .base_command import InputCommand


##############################################################################
class ObsidianCommand(InputCommand):
"""Change the root directory to your Obsidian vaults"""

COMMAND = "`obsidian`"
ALIASES = "`obs`"

@classmethod
def handle(cls, text: str, for_widget: Widget) -> bool:
"""Handle the command.
Args:
text: The text of the command.
for_widget: The widget to handle the command for.
Returns:
`True` if the command was handled; `False` if not.
"""
if cls.is_command(text):
if vaults := Path(load_configuration().obsidian_vaults).expanduser():
if vaults.exists() and vaults.is_dir():
for_widget.post_message(SetLocalViewRoot(vaults.resolve()))
return True
return False


### obsidian.py ends here
2 changes: 1 addition & 1 deletion src/hike/widgets/command_line/open_from_forge.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ class OpenFromForgeCommand(InputCommand):
| `<owner> <repo>:<branch> <file>` | Open a specific file from a specific branch of a repository |
If `<branch>` is omitted the requested file is looked in the following branches:
{', '.join(f'`{branch}`' for branch in load_configuration().main_branches)}.
{", ".join(f"`{branch}`" for branch in load_configuration().main_branches)}.
"""

@classmethod
Expand Down
4 changes: 3 additions & 1 deletion src/hike/widgets/command_line/widget.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
QuitCommand,
ReadMeCommand,
)
from .obsidian import ObsidianCommand
from .open_directory import OpenDirectoryCommand
from .open_file import OpenFileCommand
from .open_from_forge import (
Expand Down Expand Up @@ -74,6 +75,7 @@
ContentsCommand,
ReadMeCommand,
QuitCommand,
ObsidianCommand,
)
"""The commands used for the input."""

Expand Down Expand Up @@ -132,7 +134,7 @@ class CommandLine(Vertical):
| Command | Aliases | Arguments | Description |
| -- | -- | -- | -- |
{'\n '.join(sorted(command.help_text() for command in COMMANDS))}
{"\n ".join(sorted(command.help_text() for command in COMMANDS))}
### ¹Forge support
Expand Down

0 comments on commit b89f64d

Please sign in to comment.