-
Notifications
You must be signed in to change notification settings - Fork 1
/
meterm.py
123 lines (113 loc) 路 3.67 KB
/
meterm.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
# Import Section
import os
import getpass
import platform
import readline
import json
from types import coroutine
# Initiolize
# # Define Commands List
COMMANDS = ['cd', 'mkdir', 'python', 'python3']
COMMANDS_AW = COMMANDS
# add config.json
config = ''
if os.path.isfile('config.json'):
with open('config.json', 'r') as conf:
config = json.load(conf)
else :
with open('config.json', 'a') as conf:
data = '{'
data += '"dirs":{'
data += '"home":{cwd}'.format(cwd=('"' + os.getcwd().replace('\\'[0], '/') + '"'))
data += '}'
data += '}'
conf.write(data)
with open('config.json', 'r') as conf:
config = json.load(conf)
config_dir = {}
config_dir = config['dirs']
# # Create Auto Complate Function
def complete(text, state):
for cmd in COMMANDS_AW:
if cmd.startswith(text):
if not state:
return cmd
else:
state -= 1
# # Define Colors For Terminal
class color :
GREEN = '\033[92m'
RED = '\033[91m'
WHITE = '\033[0m'
# # Define Input Prompt Text
INPUT = color.GREEN + getpass.getuser()
INPUT += color.RED + '@'
INPUT += color.GREEN + platform.node()
INPUT += color.WHITE
# # Initilize For First Run
onlyfiles = [f for f in os.listdir(os.getcwd())]
COMMANDS_AW = COMMANDS + onlyfiles
readline.parse_and_bind("tab: complete")
readline.set_completer(complete)
# # If Venv Is Activate
if os.getenv('VIRTUAL_ENV'):
addr = os.environ['VIRTUAL_ENV'].split("\\"[0])
INPUT = color.RED +'('+ addr[len(addr)-1] +') '+ INPUT
# Ultimate While For Run Commands
while True:
try:
# # pring Prompt
print(color.WHITE + os.getcwd())
print(INPUT)
work = input('$ ')
# # Exit Form Meterm
if work.split(' ')[0] == 'exit':
break
if work.startswith('mt-ad ') or work.startswith('mt-add_dir '):
print(config)
os.remove('config.json')
with open('config.json', 'a') as conf:
config_dir[work.split(' ')[1]] = work.split(' ')[2]
config['dirs'] = config_dir
json.dump(config, conf)
coroutine
# # add chenge dir command
if work.split(' ')[0] == 'cd':
try:
if work.split(' ')[1] in config_dir:
os.chdir(config_dir[work.split(' ')[1]])
onlyfiles = [f for f in os.listdir(os.getcwd())]
COMMANDS_AW = COMMANDS + onlyfiles
readline.parse_and_bind("tab: complete")
readline.set_completer(complete)
continue
if os.path.isdir(work.split(' ')[1]):
os.chdir(work.split(' ')[1])
else:
os.chdir(os.getcwd() + '\\'[0] +work.split(' ')[1])
except:
print('That Is Not Real Dir')
onlyfiles = [f for f in os.listdir(os.getcwd())]
COMMANDS_AW = COMMANDS + onlyfiles
readline.parse_and_bind("tab: complete")
readline.set_completer(complete)
continue
# # add Some Command On Windows
if os.name == 'nt':
if work == 'clear':
os.system('cls')
continue
if work == 'pwd':
print(os.getcwd())
continue
if work == 'ls':
os.system('dir')
continue
if work.startswith('touch '):
with open(work.split(' ')[1], 'a') as file:
file.write('')
continue
# # Run Command
os.system(work)
except:
print('An Eror In This Command')