Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ability to view markdown without clicking on them in file explorer. #105

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions frogmouth/screens/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ def compose(self) -> ComposeResult:
yield Viewer(classes="focusable")
yield Footer()

def visit(self, location: Path | URL, remember: bool = True) -> None:
def visit(self, location: Path | URL, remember: bool = True, focus:bool = True) -> None:
"""Visit the given location.

Args:
Expand All @@ -122,7 +122,7 @@ def visit(self, location: Path | URL, remember: bool = True) -> None:
# locally in the filesystem or out on the web...
if maybe_markdown(location):
# ...attempt to visit it in the viewer.
self.query_one(Viewer).visit(location, remember)
self.query_one(Viewer).visit(location, remember, focus)
elif isinstance(location, Path):
# So, it's not Markdown, but it *is* a Path of some sort. If the
# resource seems to exist...
Expand Down Expand Up @@ -303,7 +303,7 @@ def on_local_files_goto(self, event: LocalFiles.Goto) -> None:
Args:
event: The local file visit request event.
"""
self.visit(event.location)
self.visit(event.location , focus=event.focus_viewer)

def on_history_goto(self, event: History.Goto) -> None:
"""Handle a request to go to a location from history.
Expand Down
22 changes: 20 additions & 2 deletions frogmouth/widgets/navigation_panes/local_files.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@

from pathlib import Path
from typing import Iterable
import os

from httpx import URL
from textual.app import ComposeResult
from textual.message import Message
from textual.widgets import DirectoryTree
from textual.types import DirEntry

from ...utility import maybe_markdown
from .navigation_pane import NavigationPane
Expand Down Expand Up @@ -84,7 +86,7 @@ def set_focus_within(self) -> None:
class Goto(Message):
"""Message that requests the viewer goes to a given location."""

def __init__(self, location: Path | URL) -> None:
def __init__(self, location: Path | URL, focus_viewer:bool) -> None:
"""Initialise the history goto message.

Args:
Expand All @@ -93,6 +95,8 @@ def __init__(self, location: Path | URL) -> None:
super().__init__()
self.location = location
"""The location to go to."""
self.focus_viewer = focus_viewer
"""Viewer has to be focused or not"""

def on_directory_tree_file_selected(
self, event: DirectoryTree.FileSelected
Expand All @@ -103,4 +107,18 @@ def on_directory_tree_file_selected(
event: The direct tree selection event.
"""
event.stop()
self.post_message(self.Goto(Path(event.path)))
self.post_message(self.Goto(Path(event.path) , True))

def on_tree_node_highlighted(self, event: DirectoryTree.NodeHighlighted):
"""Handle node highlight in directory tree.

Args:
event: The directory node highlight event.
"""

event.stop()
if not event.node.data:
return
pathEntry: DirEntry = event.node.data

return os.path.isfile(pathEntry.path) and self.post_message(self.Goto(Path(pathEntry.path), False))
3 changes: 2 additions & 1 deletion frogmouth/widgets/viewer.py
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,7 @@ async def _remote_load(self, location: URL, remember: bool = True) -> None:
# to the user. Let's be nice...
open_url(str(location))

def visit(self, location: Path | URL, remember: bool = True) -> None:
def visit(self, location: Path | URL, remember: bool = True, focus:bool=True) -> None:
"""Visit a location.

Args:
Expand All @@ -263,6 +263,7 @@ def visit(self, location: Path | URL, remember: bool = True) -> None:
"""
# Based on the type of the location, load up the content.
if isinstance(location, Path):
self.can_focus = focus
self._local_load(location.expanduser().resolve(), remember)
elif isinstance(location, URL):
self._remote_load(location, remember)
Expand Down