-
Notifications
You must be signed in to change notification settings - Fork 0
/
dark.py
executable file
·73 lines (52 loc) · 1.55 KB
/
dark.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
#!/usr/bin/env python3
import os
import subprocess
import shlex
import dark_shell.colors as c
import dark_shell.builtins as dark_builtins
PROMPT = f"{c.PINK}> {c.GREY}"
def print_wellcome_msg():
import getpass
import socket
username = getpass.getuser()
hostname = socket.gethostname()
msg = f"{c.DARK}{username}{c.PINK}@{c.DARK}{hostname}{c.PINK} > {c.BLACK}dark shell"
print(msg)
print(f"{c.DARK}" + os.getcwd())
# for python exec()
python_exec_globals = {"PROMPT": PROMPT}
python_exec_locals = {}
def exec_as_python_code(code):
try:
result = eval(code, python_exec_globals, python_exec_locals)
if(result is not None):
print(result)
except Exception:
exec(code, python_exec_globals, python_exec_locals)
def dark_execute(args, raw_command_line):
if not len(args):
return
if(dark_builtins.is_builtin(args[0])):
return dark_builtins.call(args)
else:
try:
subprocess.run(args)
except FileNotFoundError:
exec_as_python_code(raw_command_line)
return False
def shell_loop():
while True:
try:
command_line = input(PROMPT)
args = shlex.split(command_line)
dark_execute(args, command_line)
except dark_builtins.ExitRequest:
print(f"{c.NONE}")
break
except KeyboardInterrupt:
print("")
except Exception as ex:
print(f"{c.RED}{ex}{c.NONE}")
if __name__ == "__main__":
print_wellcome_msg()
shell_loop()