forked from Danial-Movahed/PyShell
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathterminal-old.py
124 lines (123 loc) · 4.03 KB
/
terminal-old.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
import getpass
import socket
import os
import sys
import subprocess
cls = lambda: print('\n' * 100)
################## class color Start ##############
class color:
def __init__(self):
self.cyan='\033[96m'
self.blue='\033[94m'
self.green='\033[92m'
self.yellow='\033[93m'
self.red='\033[91m'
##################### ----- #####################
self.redd='\033[31m'
self.cyann='\033[36m'
self.bluee='\033[34m'
self.greenn='\033[32m'
self.yelloww='\033[33m'
self.end='\033[0m'
def printC(self,string):
print(self.cyan+string+self.end,end="")
def printB(self,string):
print(self.blue+string+self.end,end="")
def printG(self,string):
print(self.green+string+self.end,end="")
def printY(self,string):
print(self.yellow+string+self.end,end="")
def printR(self,string):
print(self.red+string+self.end,end="")
##################### ----- #####################
def printCC(self,string):
print(self.cyann+string+self.end,end="")
def printBB(self,string):
print(self.bluee+string+self.end,end="")
def printGG(self,string):
print(self.greenn+string+self.end,end="")
def printYY(self,string):
print(self.yelloww+string+self.end,end="")
def printRR(self,string):
print(self.redd+string+self.end,end="")
################## class color End ##############
################## init Start ##############
color_main=color()
azxdew=0
commandsToRun=list()
historyFile = open('.history','a+')
################## init End ##############
################## While Start ##################
while True:
if len(commandsToRun) > 0 and azxdew<len(commandsToRun):
command = commandsToRun[azxdew]
azxdew+=1
else:
color_main.printB(getpass.getuser())
color_main.printG("@"+str(socket.gethostname())+" $ ")
command = input()
if len(command)>0:
historyFile.write(command)
historyFile.write('\n')
historyFile.flush()
try:
commandName = command.split(' ')[0]
commandArg = command.split(' ')[1:]
except:
commandName = command
commandArg = ''
if commandName == 'exit':
break
elif commandName == 'echo':
if commandArg[0][0]=='$':
try:
print(globals()[commandArg[0][1:]])
except:
print("")
else:
for i in range(len(commandArg)):
print(commandArg[i],end=' ')
print('')
elif commandName == 'clear':
cls()
elif commandName == 'help':
color_main.printC("echo: for printing a text \nclear: for clear terminal \nexit: for exiting terminal \nhelp: for show the commands \n")
elif commandName == 'read':
globals()[commandArg[0]]=input()
elif commandName == 'if':
commandsToRun=list()
while True:
TEMP=input("> ")
if TEMP == "fi":
break
commandsToRun.append(TEMP)
if commandArg[0][0]=="$":
try:
commandArg[0]=globals()[commandArg[0][1:]]
except:
commandArg[0]=""
if commandArg[2][0]=="$":
try:
commandArg[2]=globals()[commandArg[2][1:]]
except:
commandArg[2]=""
if commandArg[1]=="==":
if commandArg[0]==commandArg[2]:
azxdew=0
continue
else:
commandsToRun=list()
elif command:
cmd=list()
cmd.append(commandName)
cmd+=commandArg
try:
rc = subprocess.call(cmd, stdout=sys.stdout, stderr=subprocess.STDOUT)
print('Command returned '+str(rc))
except:
color_main.printRR('An error occured while running that command!')
print('')
color_main.printR('Double check your command for any typo')
print('')
################## While End ##################
historyFile.close()