forked from vEnhance/von
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathterm.py
108 lines (95 loc) · 2.48 KB
/
term.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
from .rc import VON_BASE_PATH, USER_OS
from .view import APPLY_COLOR
from . import view
from . import model
import os
import cmd
import traceback
import shlex
from . import controller
import glob
if USER_OS == "windows":
from pyreadline import Readline
readline = Readline()
from colorama import init
init()
else:
import readline
readline.set_completer_delims(' \t\n')
WELCOME_STRING = APPLY_COLOR("BOLD_YELLOW", "Welcome to VON!")
GOODBYE_STRING = APPLY_COLOR("BOLD_YELLOW", "OK, goodbye! :D")
def _complete_path(path):
if os.path.isdir(path):
return glob.glob(os.path.join(path, '*'))
else:
return glob.glob(path+'*')
class VonTerminal(cmd.Cmd, controller.VonController):
def getcwd(self):
return model.getcwd().replace(VON_BASE_PATH.rstrip("/"), '')
@property
def prompt(self):
return APPLY_COLOR("BOLD_CYAN", "VON/") + \
APPLY_COLOR("YELLOW", self.getcwd()) + \
"\n" + APPLY_COLOR("BOLD_GREEN", ":)") + " "
def emptyline(self):
pass
def completedefault(self, text, line, start_idx, end_idx):
return _complete_path(text)
def run(self):
print(WELCOME_STRING)
os.chdir(model.getCompleteCwd())
while 1:
try:
self.cmdloop()
break
except KeyboardInterrupt:
print("^C")
except SystemExit:
pass
except:
traceback.print_exc()
print("\n" + GOODBYE_STRING)
def onecmd(self, line):
"""Interpret the argument as though it had been typed in response
to the prompt."""
if line.strip() == "":
return self.emptyline()
_ = shlex.split(line)
cmd = _[0]
argv = _[1:]
self.lastcmd = line
if line == 'EOF' :
self.lastcmd = ''
return 1
else:
try:
func = getattr(self, 'do_' + cmd)
except AttributeError:
return self.default(line)
return func(argv)
def direct(self, cargs):
# cargs = sys.argv ostensibly
if len(cargs) == 0:
view.error("No command given")
cmd = cargs[0]
if hasattr(self, 'do_' + cmd):
func = getattr(self, 'do_' + cmd)
func(cargs[1:])
else:
view.error("Command {} not recognized".format(cmd))
def do_help(self, argv):
arg = ''.join(argv)
if arg:
try:
func = getattr(self, 'do_' + arg)
except AttributeError:
views.error('Command {} not found'.format(arg))
else:
print(APPLY_COLOR("MAGENTA", "Getting `{} --help`...".format(arg)))
func(['--help'])
else:
print("Here is a list of available commands:")
for name in sorted(self.get_names()):
if name[:3] == 'do_' and name != 'do_help' and name != 'do_EOF':
print("*", name[3:])
# vim: fdm=marker