4
4
import os
5
5
import select
6
6
import platform
7
- import argparse
8
7
from rich .console import Console
9
8
from watchdog .observers import Observer
10
9
from .app_event_handler import AppFileEventHandler
17
16
if platform .system () == "Windows" :
18
17
import msvcrt
19
18
20
-
21
19
class TkreloadApp :
22
20
"""Main application class for managing the Tkinter app."""
23
21
@@ -41,23 +39,17 @@ def monitor_file_changes(self, on_reload):
41
39
self .observer .stop ()
42
40
self .observer .join ()
43
41
44
- event_handler = AppFileEventHandler (
45
- on_reload , self .app_file , self .auto_reload_manager
46
- )
42
+ event_handler = AppFileEventHandler (on_reload , self .app_file , self .auto_reload_manager )
47
43
self .observer = Observer ()
48
- self .observer .schedule (
49
- event_handler , path = os .path .dirname (self .app_file ) or "." , recursive = False
50
- )
44
+ self .observer .schedule (event_handler , path = os .path .dirname (self .app_file ) or '.' , recursive = False )
51
45
self .observer .start ()
52
46
return self .observer
53
47
54
48
def restart_app (self ):
55
49
"""Restarts the Tkinter app."""
56
50
if self .process :
57
51
self .reload_count += 1
58
- self .console .log (
59
- f"[bold yellow]Restarting the Tkinter app... (x{ self .reload_count } )[/bold yellow]"
60
- )
52
+ self .console .log (f"[bold yellow]Restarting the Tkinter app... (x{ self .reload_count } )[/bold yellow]" )
61
53
self .process .terminate ()
62
54
self .process .wait ()
63
55
time .sleep (1 )
@@ -69,27 +61,17 @@ def start(self):
69
61
self .monitor_file_changes (self .restart_app )
70
62
71
63
try :
72
- self .console .print (
73
- "\n \n \t [bold cyan]Tkreload[/bold cyan] [bold blue]is running ✅\n \t [/bold blue]- Press [bold cyan]H[/bold cyan] for help,\n \t [bold cyan]- R[/bold cyan] to restart,\n \t [bold cyan]- A[/bold cyan] to toggle auto-reload (currently [bold magenta]{}[/bold magenta]),\n \t [bold red]- Ctrl + C[/bold red] to exit." .format (
74
- "Disabled"
75
- if not self .auto_reload_manager .get_status ()
76
- else "Enabled"
77
- )
78
- )
64
+ self .console .print ("\n \n \t [bold cyan]Tkreload[/bold cyan] [bold blue]is running ✅\n \t [/bold blue]- Press [bold cyan]H[/bold cyan] for help,\n \t [bold cyan]- R[/bold cyan] to restart,\n \t [bold cyan]- A[/bold cyan] to toggle auto-reload (currently [bold magenta]{}[/bold magenta]),\n \t [bold red]- Ctrl + C[/bold red] to exit." .format ("Disabled" if not self .auto_reload_manager .get_status () else "Enabled" ))
79
65
80
66
while True :
81
67
if platform .system () == "Windows" :
82
68
if msvcrt .kbhit (): # Check for keyboard input (Windows only)
83
- user_input = (
84
- msvcrt .getch ().decode ("utf-8" ).lower ()
85
- ) # Read single character input
69
+ user_input = msvcrt .getch ().decode ('utf-8' ).lower () # Read single character input
86
70
self .handle_input (user_input )
87
71
else :
88
72
# Use select for Unix-like systems
89
73
if sys .stdin in select .select ([sys .stdin ], [], [], 0 )[0 ]:
90
- user_input = sys .stdin .read (
91
- 1
92
- ).lower () # Capture a single character input
74
+ user_input = sys .stdin .read (1 ).lower () # Capture a single character input
93
75
self .handle_input (user_input )
94
76
95
77
time .sleep (0.1 )
@@ -103,13 +85,11 @@ def start(self):
103
85
104
86
def handle_input (self , user_input ):
105
87
"""Handles the user input commands."""
106
- if user_input == "h" :
107
- show_help (
108
- "Enabled" if self .auto_reload_manager .get_status () else "Disabled"
109
- )
110
- elif user_input == "r" :
88
+ if user_input == 'h' :
89
+ show_help ("Enabled" if self .auto_reload_manager .get_status () else "Disabled" )
90
+ elif user_input == 'r' :
111
91
self .restart_app ()
112
- elif user_input == "a" :
92
+ elif user_input == 'a' :
113
93
self .toggle_auto_reload ()
114
94
115
95
def toggle_auto_reload (self ):
@@ -119,31 +99,20 @@ def toggle_auto_reload(self):
119
99
self .reload_count = 0
120
100
status = "Enabled" if self .auto_reload_manager .get_status () else "Disabled"
121
101
122
-
123
102
def main ():
124
- parser = argparse .ArgumentParser (
125
- description = "Real-time reload Tkinter app" ,
126
- formatter_class = argparse .RawDescriptionHelpFormatter ,
127
- )
128
- parser .add_argument ("app_file" , help = "Tkinter app file path" )
129
-
130
- args = parser .parse_args ()
131
-
132
- app_file = args .app_file
133
-
134
- if not app_file :
135
- parser .print_help ()
103
+ if len (sys .argv ) < 2 :
136
104
Console ().print ("[bold red]Error: No Tkinter app file provided![/bold red]" )
137
105
sys .exit (1 )
138
106
107
+ app_file = sys .argv [1 ]
108
+
139
109
if not file_exists (app_file ):
140
110
Console ().print (f"[bold red]Error: File '{ app_file } ' not found![/bold red]" )
141
111
sys .exit (1 )
142
112
143
113
tkreload_app = TkreloadApp (app_file )
144
114
tkreload_app .start ()
145
115
146
-
147
116
if __name__ == "__main__" :
148
117
clear_terminal ()
149
118
main ()
0 commit comments