Skip to content

Commit

Permalink
Revert #19
Browse files Browse the repository at this point in the history
Revert "feat: Use argparse instead sys.argv[..]"
  • Loading branch information
iamDyeus authored Oct 27, 2024
2 parents d6ef479 + 53bb8aa commit 92b8be6
Showing 1 changed file with 13 additions and 44 deletions.
57 changes: 13 additions & 44 deletions tkreload/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import os
import select
import platform
import argparse
from rich.console import Console
from watchdog.observers import Observer
from .app_event_handler import AppFileEventHandler
Expand All @@ -17,7 +16,6 @@
if platform.system() == "Windows":
import msvcrt


class TkreloadApp:
"""Main application class for managing the Tkinter app."""

Expand All @@ -41,23 +39,17 @@ def monitor_file_changes(self, on_reload):
self.observer.stop()
self.observer.join()

event_handler = AppFileEventHandler(
on_reload, self.app_file, self.auto_reload_manager
)
event_handler = AppFileEventHandler(on_reload, self.app_file, self.auto_reload_manager)
self.observer = Observer()
self.observer.schedule(
event_handler, path=os.path.dirname(self.app_file) or ".", recursive=False
)
self.observer.schedule(event_handler, path=os.path.dirname(self.app_file) or '.', recursive=False)
self.observer.start()
return self.observer

def restart_app(self):
"""Restarts the Tkinter app."""
if self.process:
self.reload_count += 1
self.console.log(
f"[bold yellow]Restarting the Tkinter app... (x{self.reload_count})[/bold yellow]"
)
self.console.log(f"[bold yellow]Restarting the Tkinter app... (x{self.reload_count})[/bold yellow]")
self.process.terminate()
self.process.wait()
time.sleep(1)
Expand All @@ -69,27 +61,17 @@ def start(self):
self.monitor_file_changes(self.restart_app)

try:
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"
)
)
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"))

while True:
if platform.system() == "Windows":
if msvcrt.kbhit(): # Check for keyboard input (Windows only)
user_input = (
msvcrt.getch().decode("utf-8").lower()
) # Read single character input
user_input = msvcrt.getch().decode('utf-8').lower() # Read single character input
self.handle_input(user_input)
else:
# Use select for Unix-like systems
if sys.stdin in select.select([sys.stdin], [], [], 0)[0]:
user_input = sys.stdin.read(
1
).lower() # Capture a single character input
user_input = sys.stdin.read(1).lower() # Capture a single character input
self.handle_input(user_input)

time.sleep(0.1)
Expand All @@ -103,13 +85,11 @@ def start(self):

def handle_input(self, user_input):
"""Handles the user input commands."""
if user_input == "h":
show_help(
"Enabled" if self.auto_reload_manager.get_status() else "Disabled"
)
elif user_input == "r":
if user_input == 'h':
show_help("Enabled" if self.auto_reload_manager.get_status() else "Disabled")
elif user_input == 'r':
self.restart_app()
elif user_input == "a":
elif user_input == 'a':
self.toggle_auto_reload()

def toggle_auto_reload(self):
Expand All @@ -119,31 +99,20 @@ def toggle_auto_reload(self):
self.reload_count = 0
status = "Enabled" if self.auto_reload_manager.get_status() else "Disabled"


def main():
parser = argparse.ArgumentParser(
description="Real-time reload Tkinter app",
formatter_class=argparse.RawDescriptionHelpFormatter,
)
parser.add_argument("app_file", help="Tkinter app file path")

args = parser.parse_args()

app_file = args.app_file

if not app_file:
parser.print_help()
if len(sys.argv) < 2:
Console().print("[bold red]Error: No Tkinter app file provided![/bold red]")
sys.exit(1)

app_file = sys.argv[1]

if not file_exists(app_file):
Console().print(f"[bold red]Error: File '{app_file}' not found![/bold red]")
sys.exit(1)

tkreload_app = TkreloadApp(app_file)
tkreload_app.start()


if __name__ == "__main__":
clear_terminal()
main()

0 comments on commit 92b8be6

Please sign in to comment.