-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathterminal1.py
135 lines (118 loc) · 5.7 KB
/
terminal1.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
import subprocess
import signal
import sys
import os
from os.path import expanduser
from os import popen
with os.popen('clear') as f:
clear = f.read()
user=os.getenv('USER')
pc_name=os.uname()[1]
def main():
while(1):
try:
raise_parameter_error=0
comm=take_input()
command=comm.split()
i=0
while i<len(command):
if(command[i][-1:]=='\\' and (i+1<len(command))):
command[i]=command[i][:-1]+" "+command[i+1]
command.remove(command[i+1])
i+=1
parameters=[]
other=[]
for x in command[1:]:
if x[0]=='-':
parameters+=list(x[1:])
if x[1]=='-':
raise_parameter_error=1
else:
other+=[x]
if command[0]=='cd':
cd_self(other)
elif command[0]=='help':
help_display(other)
else:
subprocess.call(comm, shell = True)
except Exception as inst:
print "Error processing your request, use 'help' to get list of supported commands"
#####################################################
#### PROGRAM RELATED FUNCTIONS
#####################################################
def cd_self(other):
if len(other)>0 and (os.path.isfile(other[0])==False) and (os.path.exists(other[0])==True):
os.chdir(other[0])
elif len(other)==0:
os.chdir(os.environ['HOME'])
elif os.path.isfile(other[0]):
print "bash: cd: "+other[0]+": Not a directory"
else:
print "bash: cd: "+other[0]+": No such file or directory"
#####################################################
#### LETS GIVE IT SOME TERMINAL TOUCH
#####################################################
# FROM http://stackoverflow.com/questions/287871/print-in-terminal-with-colors-using-python
class bcolors:
HEADER = '\033[95m'
OKBLUE = '\033[94m'
OKGREEN = '\033[92m'
WARNING = '\033[93m'
FAIL = '\033[91m'
ENDC = '\033[0m'
def take_input():
return raw_input(user+'@'+pc_name+':'+see_directory()+'$ ')
def see_directory(x=0):
path=os.getcwd()
if x==0:
return path.replace(os.environ['HOME'],'~',1)
else:
return path
def help_display(command):
print '''GNU bash, version 4.3.30(1)-release (x86_64-pc-linux-gnu)
These shell commands are defined internally. Type `help' to see this list.
Type `help name' to find out more about the function `name'.
Use `info bash' to find out more about the shell in general.
Use `man -k' or `info' to find out more about commands not in this list.
A star (*) next to a name means that the command is disabled.
job_spec [&] history [-c] [-d offset] [n] or hist>
(( expression )) if COMMANDS; then COMMANDS; [ elif C>
. filename [arguments] jobs [-lnprs] [jobspec ...] or jobs >
: kill [-s sigspec | -n signum | -sigs>
[ arg... ] let arg [arg ...]
[[ expression ]] local [option] name[=value] ...
alias [-p] [name[=value] ... ] logout [n]
bg [job_spec ...] mapfile [-n count] [-O origin] [-s c>
bind [-lpsvPSVX] [-m keymap] [-f file> popd [-n] [+N | -N]
break [n] printf [-v var] format [arguments]
builtin [shell-builtin [arg ...]] pushd [-n] [+N | -N | dir]
caller [expr] pwd [-LP]
case WORD in [PATTERN [| PATTERN]...)> read [-ers] [-a array] [-d delim] [->
cd [-L|[-P [-e]] [-@]] [dir] readarray [-n count] [-O origin] [-s>
command [-pVv] command [arg ...] readonly [-aAf] [name[=value] ...] o>
compgen [-abcdefgjksuv] [-o option] > return [n]
complete [-abcdefgjksuv] [-pr] [-DE] > select NAME [in WORDS ... ;] do COMM>
compopt [-o|+o option] [-DE] [name ..> set [-abefhkmnptuvxBCHP] [-o option->
continue [n] shift [n]
coproc [NAME] command [redirections] shopt [-pqsu] [-o] [optname ...]
declare [-aAfFgilnrtux] [-p] [name[=v> source filename [arguments]
dirs [-clpv] [+N] [-N] suspend [-f]
disown [-h] [-ar] [jobspec ...] test [expr]
echo [-neE] [arg ...] time [-p] pipeline
enable [-a] [-dnps] [-f filename] [na> times
eval [arg ...] trap [-lp] [[arg] signal_spec ...]
exec [-cl] [-a name] [command [argume> true
exit [n] type [-afptP] name [name ...]
export [-fn] [name[=value] ...] or ex> typeset [-aAfFgilrtux] [-p] name[=va>
false ulimit [-SHabcdefilmnpqrstuvxT] [lim>
fc [-e ename] [-lnr] [first] [last] o> umask [-p] [-S] [mode]
fg [job_spec] unalias [-a] name [name ...]
for NAME [in WORDS ... ] ; do COMMAND> unset [-f] [-v] [-n] [name ...]
for (( exp1; exp2; exp3 )); do COMMAN> until COMMANDS; do COMMANDS; done
function name { COMMANDS ; } or name > variables - Names and meanings of so>
getopts optstring name [arg] wait [-n] [id ...]
hash [-lr] [-p pathname] [-dt] [name > while COMMANDS; do COMMANDS; done
help [-dms] [pattern ...] { COMMANDS ; }
'''
if __name__=="__main__":
main()