-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclireader.py
83 lines (70 loc) · 2.53 KB
/
clireader.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
# clireader.py -- The entry point of the application
from __future__ import unicode_literals
import sys
import shell.cmdfactory as cmdfactory
import shell.cmdbase as cmdbase
import shell.format_utils.result_formatter as res_fmt
from crawlers.cfactory import CrawlerFactory as cf
'''
following are the imports from prompt-tookit
for AutoSuggestion and World Completion and session
'''
from prompt_toolkit.styles import Style # for styling the prompt
from prompt_toolkit.auto_suggest import AutoSuggestFromHistory
from prompt_toolkit.completion import WordCompleter
from prompt_toolkit import PromptSession
from prompt_toolkit import print_formatted_text
PROMPT_USERNAME = 'clireader'
style = Style.from_dict({
# User input (default text).
'': '#b0bec5',
# Prompt.
'username': '#b0bec5',
'at': '#b0bec5',
'colon': '#0000aa',
'pound': '#00aa00',
'novelname': '#e30425',
'path': 'ansicyan underline',
'completion-menu.completion': 'bg:#9a9a9a #ffffff',
'completion-menu.completion.current': 'bg:#00aaaa #000000',
'scrollbar.background': 'bg:#88aaaa',
'scrollbar.button': 'bg:#222222',
})
PROMPT_NOVEL_INDEX = 2
prompt_message = [
['class:username', PROMPT_USERNAME],
['class:at', '@'],
['class:novelname', cf.DEFAULT_WEB],
['class:colon', ':'],
]
command_completer = WordCompleter(
['help', 'listwebs', 'search', 'read', 'setweb', 'settheme', 'quit'])
if __name__ == '__main__':
session = PromptSession()
# Add new path variables
sys.path.append('crawlers/')
sys.path.append('shell/')
# Print the greeting message
greet_msg = res_fmt.get_greet_msg()
print_formatted_text(greet_msg)
cmdFactoryObj = cmdfactory.CommandFactory()
while True:
try:
cmd = session.prompt(
prompt_message, style=style, completer=command_completer, auto_suggest=AutoSuggestFromHistory())
except KeyboardInterrupt:
print("Use quit command to quit the shell")
continue
except EOFError:
break
else:
args = cmd.split()
len_args = len(args)
if len_args == 0:
continue
cmd_obj = cmdFactoryObj.get_command(args[0])
rest = args[1:] if len_args != 1 else []
status_code, result = cmd_obj.execute(rest)
if status_code != cmdbase.CommandBase.CMD_STATUS_READ_SUCCESS:
print_formatted_text(result)
prompt_message[PROMPT_NOVEL_INDEX][-1] = cf.DEFAULT_WEB