-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathterminals.py
445 lines (364 loc) · 15.2 KB
/
terminals.py
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
import os
import time
import psutil
import socket
import subprocess
import platform
from datetime import datetime
from rich.text import Text
from rich.console import Console
from rich.prompt import Prompt
import config
console = Console()
prompt = None
prompt_flag = True
class Terminal:
def set_prompt(self, value):
global prompt
prompt = value
def get_prompt(self):
global prompt
return prompt
def set_prompt_flag(self, value: bool):
global prompt_flag
prompt_flag = value
def get_prompt_flag(self):
global prompt_flag
return prompt_flag
def terminal_1(self):
cwd = os.getcwd().split(os.sep)
time_str = datetime.now().strftime("%H:%M")
mem = psutil.virtual_memory()
left = Text()
left.append("\n ☾ ", style="white on dark_blue")
left.append("Solar Night", style="bright_white on dark_blue")
left.append(f" {time_str} ", style="white on dark_blue")
left.append(f" | 📁 {'/'.join(cwd[-2:])} ", style="cyan")
try:
branch = subprocess.check_output(
["git", "rev-parse", "--abbrev-ref", "HEAD"],
stderr=subprocess.DEVNULL
).decode().strip()
except subprocess.CalledProcessError:
branch = "no-branch"
right = Text()
right.append("", style="black on blue")
right.append(f" {branch} ", style="black on blue")
global prompt
prompt = left + Text(" " * (console.width - len(left.plain) - len(right.plain))) + right
self.set_prompt(prompt)
return prompt
def terminal_2(self):
cwd = os.getcwd().split(os.sep)
time_str = datetime.now().strftime("%H:%M")
mem = psutil.virtual_memory()
left = Text()
left.append("\nHacker Mode", style="white on green")
left.append(f" | ⏰ {time_str} | MEM: {mem.percent}% ", style="white on green")
left.append(f" | 📁 {'/'.join(cwd[-2:])} ", style="bright_green")
try:
branch = subprocess.check_output(
["git", "rev-parse", "--abbrev-ref", "HEAD"],
stderr=subprocess.DEVNULL
).decode().strip()
except subprocess.CalledProcessError:
branch = "no-branch"
right = Text()
right.append("", style="black on green")
right.append(f" {branch} ", style="black on green")
global prompt
prompt = left + Text(" " * (console.width - len(left.plain) - len(right.plain))) + right
self.set_prompt(prompt)
return prompt
def terminal_3(self):
# Extract current working directory
cwd = os.getcwd().split(os.sep)
hostname = platform.node()
# Build left segment
left = Text()
left.append("\n", style="green")
left.append("", style="black")
left.append(f" {hostname} ", style="black on white")
left.append("", style="white on black")
# Build middle segment (folder path)
for part in cwd:
if part:
left.append("", style="black on blue")
left.append(f" {part} ", style="white on blue")
left.append("", style="blue on black")
# Build right segment (branch name)
right = Text()
right.append("", style="black on green")
try:
branch = subprocess.check_output(
["git", "rev-parse", "--abbrev-ref", "HEAD"],
stderr=subprocess.DEVNULL
).decode("utf-8").strip()
except subprocess.CalledProcessError:
branch = "no-branch"
right.append(f" {branch} ", style="black on green")
right.append("", style="green on black")
# Combine everything
global prompt
prompt = left + right
self.set_prompt(prompt)
return prompt
def terminal_4(self):
# Get current folder
folder = os.path.basename(os.getcwd())
time_str = datetime.now().strftime("%H:%M")
try:
branch = subprocess.check_output(
['git', 'rev-parse', '--abbrev-ref', 'HEAD'],
stderr=subprocess.DEVNULL
).decode().strip()
except subprocess.CalledProcessError:
branch = "no-branch"
try:
status_output = subprocess.check_output(
["git", "status", "--porcelain"],
stderr=subprocess.DEVNULL
).decode().strip()
changes = len(status_output.splitlines())
except subprocess.CalledProcessError:
changes = 0
# Left: folder name segment
p = Text()
p.append("\n", style="black on #FFD700")
p.append(f" {folder} ", style="black on #FFD700")
p.append("", style="#FFD700 on dark_orange")
# Middle: branch and status
p.append(f" {branch} = ", style="black on dark_orange")
p.append(f" {changes} ", style="black on dark_orange")
# Right: bolt/power symbol
p.append("", style="dark_orange on blue")
p.append(f" {time_str} ", style="white on blue")
p.append("", style="blue on black")
global prompt
prompt = p
self.set_prompt(prompt)
return prompt
def terminal_5(self):
folder = os.path.basename(os.getcwd())
# Start timer
start_time = time.time()
# Git branch
try:
branch = subprocess.check_output(
['git', 'rev-parse', '--abbrev-ref', 'HEAD'],
stderr=subprocess.DEVNULL
).decode().strip()
except subprocess.CalledProcessError:
branch = "no-branch"
# Git status
try:
status_output = subprocess.check_output(
["git", "status", "--porcelain"],
stderr=subprocess.DEVNULL
).decode().strip()
changes = len(status_output.splitlines())
except subprocess.CalledProcessError:
changes = 0
# End timer
end_time = time.time()
exec_time_ms = int((end_time - start_time) * 1000)
# Memory usage
mem = psutil.virtual_memory()
used_percent = mem.percent
used = mem.used // (1024 ** 3)
total = mem.total // (1024 ** 3)
# Build left side of prompt
left = Text()
left.append("\n", style="white on #DCDCDC")
left.append(" shell ", style="black on #DCDCDC")
left.append("", style="#DCDCDC on black")
left.append("", style="black on #4682B4")
left.append(f" MEM: {used_percent:.2f}% ", style="white on #4682B4")
left.append(f" {used}/{total}GB ", style="white on #4682B4")
left.append("", style="#4682B4 on grey30")
left.append(f" {exec_time_ms}ms ", style="white on grey30")
left.append("", style="grey30 on black")
left.append(f" → {folder} ", style="white on black")
# Build right side of prompt
right = Text()
right.append("", style="bright_cyan on black")
right.append(f" {branch} = ", style="black on bright_cyan")
right.append(f" {changes} ", style="black on bright_cyan")
# Combine with spacing
space = " " * max(console.width - len(left.plain) - len(right.plain), 1)
global prompt
prompt = left + Text(space) + right
self.set_prompt(prompt)
return prompt
def terminal_6(self):
start_time = time.time()
# Current folder name
folder = os.path.basename(os.getcwd())
# Git info
try:
branch = subprocess.check_output(
['git', 'rev-parse', '--abbrev-ref', 'HEAD'],
stderr=subprocess.DEVNULL
).decode().strip()
except subprocess.CalledProcessError:
branch = "no-branch"
try:
status_output = subprocess.check_output(
["git", "status", "--porcelain"],
stderr=subprocess.DEVNULL
).decode().strip()
changes = len(status_output.splitlines())
except subprocess.CalledProcessError:
changes = 0
end_time = time.time()
exec_time = f"{int((end_time - start_time) * 1000)}ms"
# Memory
mem = psutil.virtual_memory()
mem_used = mem.used / (1024 ** 3)
mem_total = mem.total / (1024 ** 3)
mem_percent = mem.percent
# Host and Time
hostname = socket.gethostname().split(".")[0]
now = datetime.now().strftime("%a, %H:%M")
left = Text()
left.append("\n", style="black on blue")
left.append(" shell ", style="white on blue")
left.append("", style="blue on black")
left.append("", style="black on dark_orange")
left.append(" ", style="black on dark_orange")
left.append(f"{folder} ", style="black on dark_orange")
left.append("", style="dark_orange on black")
left.append("", style="black on yellow")
left.append(f" {branch} = {changes} ", style="black on yellow")
left.append("", style="yellow on black")
left.append("", style="black on grey70")
left.append(f" {exec_time} ", style="black on grey70")
left.append("", style="grey70 on black")
right = Text()
right.append("", style="green on black")
right.append(f" {mem_percent:.1f}% ", style="green on black")
right.append("", style="blue on black")
right.append(f" {hostname} ", style="white on blue")
right.append("", style="black on blue")
right.append("", style="grey70 on black")
right.append(f" {now} ", style="black on grey70")
spacing = " " * max(console.width - len(left.plain) - len(right.plain), 1)
global prompt
prompt = left + Text(spacing) + right
self.set_prompt(prompt)
return prompt
def terminal_7(self):
cwd = os.getcwd().split(os.sep)
time_str = datetime.now().strftime("%H:%M")
mem = psutil.virtual_memory()
mem_percent = mem.percent
mem_total_gb = round(mem.total / (1024 ** 3))
mem_used_gb = round(mem.used / (1024 ** 3))
left_prompt = Text()
left_prompt.append("\n # ", style="black on white")
left_prompt.append(" shell ", style="white on blue")
left_prompt.append("", style="blue on black")
left_prompt.append("", style="black on blue")
left_prompt.append(f" MEM: {mem_percent}% ↑ {mem_used_gb}/{mem_total_gb}GB ", style="white on blue")
left_prompt.append("", style="blue on grey15")
left_prompt.append(" code ", style="white on grey15")
left_prompt.append("", style="grey15 on black")
left_prompt.append(f" {time_str} ", style="white on black")
left_prompt.append("", style="black")
for part in cwd:
if part:
left_prompt.append(" // ", style="white")
left_prompt.append("📁", style="white")
left_prompt.append(f" {part} ", style="white")
right_prompt = Text()
right_prompt.append("", style="black on medium_sea_green")
right_prompt.append(" ", style="black on medium_sea_green")
try:
branch = subprocess.check_output(
["git", "rev-parse", "--abbrev-ref", "HEAD"],
stderr=subprocess.DEVNULL
).decode("utf-8").strip()
except subprocess.CalledProcessError:
branch = "no-branch"
right_prompt.append(f" {branch} ≡ ⎔ ~1 ", style="black on medium_sea_green")
global prompt
prompt = left_prompt + Text(" " * (console.width - len(left_prompt.plain) - len(right_prompt.plain))) + right_prompt
self.set_prompt(prompt)
return prompt
def terminal_8(self):
start_time = time.time()
# Gather information
user = os.getenv("USER") or os.getenv("USERNAME") or "user"
hostname = socket.gethostname().split('.')[0]
folder = os.path.basename(os.getcwd())
# Git branch info
try:
branch = subprocess.check_output(
["git", "rev-parse", "--abbrev-ref", "HEAD"],
stderr=subprocess.DEVNULL
).decode().strip()
except subprocess.CalledProcessError:
branch = ""
end_time = time.time()
exec_time = f"{int((end_time - start_time) * 1000)}ms"
current_time = datetime.now().strftime("%d/%m/%y %H:%M")
# LEFT prompt
left = Text()
left.append("\n", style="grey37")
left.append(" shell ", style="black on grey37")
left.append("\uE0B4", style="grey37")
left.append("", style="grey85")
left.append(f" {user}@{hostname} ", style="black on grey85")
left.append("\uE0B4", style="grey85")
if branch:
left.append("", style="khaki1")
left.append(f" {branch} ", style="black on khaki1")
left.append("\uE0B4", style="khaki1")
# Add directory
full_path = os.getcwd()
folders = full_path.split(os.sep)
folder_path = " » ".join(folders[-2:]) # Show last 2 folders
left.append(f"\n[ {folder_path} ]", style="white")
# RIGHT prompt
right = Text()
right.append(f"{exec_time} - {current_time}", style="bold palegreen3")
# Final combined prompt
spacing = " " * max(console.width - len(left.plain.splitlines()[-1]) - len(right.plain), 1)
global prompt
prompt = left + Text(spacing) + right
self.set_prompt(prompt)
return prompt
def change_terminal(self, *args):
global prompt_flag
prompt_flag = False
self.set_prompt_flag(False) # Update the flag globally
console.print("\nChoose Terminal Layout:", style="bold blue")
console.print("[1] Solarized Night")
console.print("[2] Hacker Green")
console.print("[3] Agnoster")
console.print("[4] Marcduiker")
console.print("[5] Clean Detailed")
console.print("[6] Atomic-Lite")
console.print("[7] PyShell Default")
console.print("[8] Softline")
choice = Prompt.ask("Enter layout number", choices=["1", "2", "3", "4", "5", "6", "7", "8"], default="1")
current_terminal = int(choice)
config.current_terminal_layout = current_terminal
console.clear()
console.print(f"Terminal switched to layout {choice}!", style="bold green")
if current_terminal == 1:
self.terminal_1()
elif current_terminal == 2:
self.terminal_2()
elif current_terminal == 3:
self.terminal_3()
elif current_terminal == 4:
self.terminal_4()
elif current_terminal == 5:
self.terminal_5()
elif current_terminal == 6:
self.terminal_6()
elif current_terminal == 7:
self.terminal_7()
elif current_terminal == 8:
self.terminal_8()