Skip to content

Commit

Permalink
Rebuild echo, exit, read, clear
Browse files Browse the repository at this point in the history
  • Loading branch information
Danial-Movahed committed Jul 22, 2021
1 parent 22b4bbf commit f837db3
Show file tree
Hide file tree
Showing 2 changed files with 155 additions and 96 deletions.
124 changes: 124 additions & 0 deletions terminal-old.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,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()
127 changes: 31 additions & 96 deletions terminal.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
import getpass
import cmd
import socket
import os
import sys
import subprocess
cls = lambda: print('\n' * 100)
################## class color Start ##############
import getpass

class color:
def __init__(self):
self.cyan='\033[96m'
Expand All @@ -18,107 +15,45 @@ def __init__(self):
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="")
return(self.cyan+string+self.end)
def printB(self,string):
print(self.blue+string+self.end,end="")
return(self.blue+string+self.end)
def printG(self,string):
print(self.green+string+self.end,end="")
return(self.green+string+self.end)
def printY(self,string):
print(self.yellow+string+self.end,end="")
return(self.yellow+string+self.end)
def printR(self,string):
print(self.red+string+self.end,end="")
return(self.red+string+self.end)
##################### ----- #####################
def printCC(self,string):
print(self.cyann+string+self.end,end="")
return(self.cyann+string+self.end)
def printBB(self,string):
print(self.bluee+string+self.end,end="")
return(self.bluee+string+self.end)
def printGG(self,string):
print(self.greenn+string+self.end,end="")
return(self.greenn+string+self.end)
def printYY(self,string):
print(self.yelloww+string+self.end,end="")
return(self.yelloww+string+self.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("")
return(self.redd+string+self.end)
main_color = color()
cls = lambda: print('\n' * 100)
class Shell(cmd.Cmd):
prompt=main_color.printBB(getpass.getuser())+main_color.printGG("@"+socket.gethostname()+" $ ")
def do_echo(self, args):
if args[0] == '$':
print(globals()[args.split(' ')[0]])
else:
for i in range(len(commandArg)):
print(commandArg[i],end=' ')
print('')
elif commandName == 'clear':
print(args)
def do_exit(self):
return True
def do_clear(self):
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()
def do_read(self,arg):
arg=arg.split(' ')[0]
globals()[arg]=input()



Shell().cmdloop()

0 comments on commit f837db3

Please sign in to comment.