Skip to content

Commit

Permalink
🔀 Merge pull request #39 from davep/jump-to-document
Browse files Browse the repository at this point in the history
Add a command to jump to document
  • Loading branch information
davep authored Feb 24, 2025
2 parents a8015bc + eeb76c3 commit d952a2f
Show file tree
Hide file tree
Showing 8 changed files with 55 additions and 2 deletions.
7 changes: 7 additions & 0 deletions ChangeLog.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,13 @@

## Unreleased

**Released: WiP**

- Added an application command for jumping to the markdown document viewer.
- Added `document` as a command that the command line understands.

## v0.4.0

**Released: 2025-02-19**

- Added `changelog` as a command that the command line understands.
Expand Down
2 changes: 2 additions & 0 deletions src/hike/commands/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
CopyMarkdownToClipboard,
Edit,
JumpToCommandLine,
JumpToDocument,
Reload,
SaveCopy,
SearchBookmarks,
Expand Down Expand Up @@ -37,6 +38,7 @@
"Forward",
"JumpToBookmarks",
"JumpToCommandLine",
"JumpToDocument",
"JumpToHistory",
"JumpToLocalBrowser",
"JumpToTableOfContents",
Expand Down
7 changes: 7 additions & 0 deletions src/hike/commands/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,13 @@ class JumpToCommandLine(Command):
BINDING_KEY = "/"


##############################################################################
class JumpToDocument(Command):
"""Jump to the markdown document"""

BINDING_KEY = "ctrl+slash"


##############################################################################
class ChangeCommandLineLocation(Command):
"""Swap the position of the command line between top and bottom"""
Expand Down
6 changes: 4 additions & 2 deletions src/hike/providers/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
Forward,
JumpToBookmarks,
JumpToCommandLine,
JumpToDocument,
JumpToHistory,
JumpToLocalBrowser,
JumpToTableOfContents,
Expand Down Expand Up @@ -66,11 +67,12 @@ def commands(self) -> CommandHits:
yield from self._maybe(Edit)
yield from self._maybe(Forward)
yield Help()
yield JumpToCommandLine()
yield JumpToTableOfContents()
yield JumpToBookmarks()
yield JumpToCommandLine()
yield JumpToDocument()
yield JumpToHistory()
yield JumpToLocalBrowser()
yield JumpToTableOfContents()
yield Quit()
yield from self._maybe(Reload)
yield from self._maybe(SaveCopy)
Expand Down
7 changes: 7 additions & 0 deletions src/hike/screens/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
Forward,
JumpToBookmarks,
JumpToCommandLine,
JumpToDocument,
JumpToHistory,
JumpToLocalBrowser,
JumpToTableOfContents,
Expand Down Expand Up @@ -146,6 +147,7 @@ class Main(EnhancedScreen[None]):
Forward,
JumpToBookmarks,
JumpToCommandLine,
JumpToDocument,
JumpToHistory,
JumpToLocalBrowser,
JumpToTableOfContents,
Expand Down Expand Up @@ -411,6 +413,11 @@ def action_jump_to_command_line_command(self) -> None:
if self.AUTO_FOCUS:
self.query_one(self.AUTO_FOCUS).focus()

@on(JumpToDocument)
def action_jump_to_document_command(self) -> None:
"""Jump to the document."""
self.query_one(Viewer).focus()

@on(Backward)
def action_backward_command(self) -> None:
"""Move backward through history."""
Expand Down
10 changes: 10 additions & 0 deletions src/hike/widgets/command_line/general.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
# Local imports.
from ...commands import (
JumpToBookmarks,
JumpToDocument,
JumpToHistory,
JumpToLocalBrowser,
JumpToTableOfContents,
Expand Down Expand Up @@ -68,6 +69,15 @@ class ContentsCommand(GeneralCommand):
MESSAGE = JumpToTableOfContents


##############################################################################
class DocumentCommand(GeneralCommand):
"""Jump to the markdown document"""

COMMAND = "`document`"
ALIASES = "`d`, `doc`"
MESSAGE = JumpToDocument


##############################################################################
class HelpCommand(GeneralCommand):
"""Show the help screen"""
Expand Down
2 changes: 2 additions & 0 deletions src/hike/widgets/command_line/widget.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
BookmarksCommand,
ChangeLogCommand,
ContentsCommand,
DocumentCommand,
HelpCommand,
HistoryCommand,
LocalCommand,
Expand Down Expand Up @@ -61,6 +62,7 @@
# Once the above are out of the way the order doesn't matter so much.
BookmarksCommand,
ChangeDirectoryCommand,
DocumentCommand,
HelpCommand,
HistoryCommand,
LocalCommand,
Expand Down
16 changes: 16 additions & 0 deletions src/hike/widgets/viewer.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@
from textual.reactive import var
from textual.widgets import Label, Markdown, Rule

##############################################################################
# Typing extensions imports.
from typing_extensions import Self

##############################################################################
# Local imports.
from .. import USER_AGENT
Expand Down Expand Up @@ -149,6 +153,18 @@ def compose(self) -> ComposeResult:
),
)

def focus(self, scroll_visible: bool = True) -> Self:
"""Focus the viewer.
Args:
scroll_visible: Should the widget be scrolled to be visible?
Returns:
Self.
"""
self.query_one("#document").focus(scroll_visible)
return self

@property
def source(self) -> str:
"""The source of the markdown being viewed."""
Expand Down

0 comments on commit d952a2f

Please sign in to comment.