-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcli.py
More file actions
124 lines (104 loc) · 4.67 KB
/
Copy pathcli.py
File metadata and controls
124 lines (104 loc) · 4.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# cli.py
from textual.app import App, ComposeResult
from textual.containers import Center, Vertical
from textual.widgets import Header, Footer, Static
from rich.text import Text
from rich.console import Console # Import Console for rich printing
import time # Import the time module for the delay
import os # Import the os module to clear the screen
# Import tools
from tools.dev_jokes import get_joke
from tools.code_roast import get_roast
from tools.commit_excuse import get_excuse
from tools.do_it_now import get_motivation
# Import the snake game screen
from games.snake_game import SnakeGameScreen
class DevFunApp(App):
"""A humorous, interactive terminal app for developers."""
CSS_PATH = "cli.tcss"
BINDINGS = [
("q", "quit", "Quit"),
("up", "cursor_up", "Cursor Up"),
("down", "cursor_down", "Cursor Down"),
("enter", "select_item", "Select / More"),
("escape", "show_menu", "Back to Menu"),
]
def __init__(self):
super().__init__()
self.menu_items = ["Dev Jokes", "Code Roast", "Commit Excuse", "Do It Now!", "Play Snake", "Exit"]
self.current_selection = 0
self.last_action = None
self.actions = {
"Dev Jokes": (get_joke, "bold cyan"),
"Code Roast": (get_roast, "bold magenta"),
"Commit Excuse": (get_excuse, "bold yellow"),
"Do It Now!": (get_motivation, "bold green"),
}
def compose(self) -> ComposeResult:
yield Header(show_clock=True, id="app-header")
with Center():
with Vertical(id="content-container"):
yield Static("What would you like to do now!!\n(Anyhow, entertaining is my work 😎)", id="title")
with Vertical(id="menu"):
for item in self.menu_items:
yield Static(item, classes="menu-item")
yield Static(id="message-display", classes="message-box")
yield Footer()
def on_mount(self) -> None:
self.query_one(Header).text = "DevFun - Your Daily Dose of Developer Humor"
self.action_show_menu()
self.update_highlight()
def update_highlight(self) -> None:
for i, item in enumerate(self.query(".menu-item")):
item.set_class(i == self.current_selection, "highlighted")
def show_message_view(self, message: str, style: str):
self.query_one("#title").display = False
self.query_one("#menu").display = False
message_display = self.query_one("#message-display")
message_display.display = True
prompt = "\n\n[dim](Press Enter for more, ESC for main menu)[/dim]"
full_text = Text.from_markup(f"{message}{prompt}", style=style, justify="center")
message_display.update(full_text)
def action_show_menu(self) -> None:
self.query_one("#title").display = True
self.query_one("#menu").display = True
self.query_one("#message-display").display = False
self.last_action = None
self.update_highlight()
def action_cursor_up(self) -> None:
if self.query_one("#menu").display:
self.current_selection = (self.current_selection - 1) % len(self.menu_items)
self.update_highlight()
def action_cursor_down(self) -> None:
if self.query_one("#menu").display:
self.current_selection = (self.current_selection + 1) % len(self.menu_items)
self.update_highlight()
def action_select_item(self) -> None:
if self.last_action:
action_func, style = self.last_action
new_message = action_func()
self.show_message_view(new_message, style)
return
if self.query_one("#menu").display:
selected_item = self.menu_items[self.current_selection]
if selected_item == "Exit":
self.exit()
elif selected_item == "Play Snake":
self.push_screen(SnakeGameScreen())
elif selected_item in self.actions:
action_func, style = self.actions[selected_item]
self.last_action = (action_func, style)
message = action_func()
self.show_message_view(message, style)
def run():
"""Prints a launch message, waits, clears the screen, and then runs the app."""
console = Console()
# Print the launch message using rich for nice colors
console.print("\n[bold green]🚀 Launching DevFun! Fun awaiting...[/bold green]\n")
# Wait for 2 seconds
time.sleep(1.5)
# Clear the terminal screen (works on Windows, macOS, and Linux)
os.system('cls' if os.name == 'nt' else 'clear')
# Launch the Textual app
app = DevFunApp()
app.run()