33##############################################################################
44# Python imports.
55from argparse import Namespace
6+ from pathlib import Path
67from webbrowser import open as open_in_browser
78
89##############################################################################
5051 update_configuration ,
5152)
5253from ..messages import OpenLocation , OpenText , OpenURI
54+ from ..preflight import is_likely_local_file , path_from_uri
5355from ..providers import MainCommands
5456from ..widgets import CommandLine , HistoryViewer , Viewer
5557
@@ -305,6 +307,27 @@ async def _load_from_capsule(self, request: OpenLocation) -> None:
305307 finally :
306308 self ._command_line .working = False
307309
310+ @work (thread = True )
311+ def _load_from_filesystem (self , request : OpenLocation ) -> None :
312+ """Load a document from the filesystem.
313+
314+ Args:
315+ request: The request to load the document from.
316+ """
317+ assert isinstance (request .location , Path )
318+ try :
319+ self .post_message (
320+ OpenText (request .location .read_text (encoding = "utf-8" ), request .location )
321+ )
322+ # TODO: Remember in history.
323+ except IOError as error :
324+ self .notify (
325+ f"Error loading { request .location } :\n \n { error } " ,
326+ severity = "error" ,
327+ title = "Filesystem Error" ,
328+ )
329+ return
330+
308331 @on (OpenText )
309332 def open_text (self , message : OpenText ) -> None :
310333 """Open text in the viewer.
@@ -324,6 +347,8 @@ def open_location(self, message: OpenLocation) -> None:
324347 """
325348 if isinstance (message .location , GeminiURI ):
326349 self ._load_from_capsule (message )
350+ else :
351+ self ._load_from_filesystem (message )
327352
328353 @on (OpenURI )
329354 def open_uri (self , message : OpenURI ) -> None :
@@ -340,7 +365,10 @@ def open_uri(self, message: OpenURI) -> None:
340365 except URIError :
341366 pass
342367
343- # TODO: Handle gmi files in the filesystem.
368+ # Perhaps it's a local file?
369+ if is_likely_local_file (message .uri ):
370+ self .post_message (OpenLocation (path_from_uri (message .uri )))
371+ return
344372
345373 # Otherwise, try to open it in the system browser.
346374 open_in_browser (message .uri )
0 commit comments