@@ -54,18 +54,16 @@ class AtonEditor(App):
5454 ("ctrl+shift+q" , "quit_app" , "Ctrl+Shift+Q Quit" ),
5555 ]
5656
57- def __init__ (self , file_path : str , ** kwargs ) -> None :
57+ def __init__ (self , file_path : str , initial_text : str | None = None , ** kwargs ) -> None :
5858 super ().__init__ (** kwargs )
5959 self .file_path = file_path
60- self .file_content = ""
60+ self .file_content = initial_text if initial_text is not None else ""
6161
62- import os
63-
64- if os .path .exists (file_path ):
62+ if initial_text is None and os .path .exists (file_path ):
6563 try :
6664 with open (file_path , "r" , encoding = "utf-8" ) as handle :
6765 self .file_content = handle .read ()
68- except Exception as error :
66+ except OSError as error :
6967 self .file_content = f"Error reading file: { error } "
7068
7169 def compose (self ) -> ComposeResult :
@@ -81,8 +79,8 @@ def action_save_file(self) -> None:
8179 try :
8280 with open (self .file_path , "w" , encoding = "utf-8" ) as handle :
8381 handle .write (text_area .text )
84- except Exception :
85- pass
82+ except ( PermissionError , OSError ) as error :
83+ self . notify ( f"Failed to save file: { error } " , severity = "error" )
8684
8785 def action_quit_app (self ) -> None :
8886 self .exit ()
@@ -139,7 +137,7 @@ def _handle_edit_command(raw_command: str) -> bool:
139137 initial_text = file_path .read_text (encoding = "utf-8" ) if file_path .exists () else ""
140138
141139 try :
142- AtonEditor (str (file_path ), initial_text ).run ()
140+ AtonEditor (str (file_path ), initial_text = initial_text ).run ()
143141 except Exception as error :
144142 typer .secho (f"Editor error: { error } " , fg = typer .colors .RED )
145143
0 commit comments