continued reorganisation
This commit is contained in:
parent
6f8a7322f2
commit
0a2d9a5694
22 changed files with 190 additions and 61 deletions
42
bcrypter/cli/repl.py
Normal file
42
bcrypter/cli/repl.py
Normal file
|
|
@ -0,0 +1,42 @@
|
|||
import readline # GNU readline (ie allows input() history buffer)
|
||||
from itertools import chain
|
||||
|
||||
from bcrypter.cli.builtins import *
|
||||
from bcrypter.cli.commands import *
|
||||
from bcrypter.lib.result import Result
|
||||
from bcrypter.exceptions import CmdDeclarationError
|
||||
|
||||
class REPL:
|
||||
_PROMPT = '>> '
|
||||
_DEFAULT_HISTORY_FILE = '.bcrypter_history'
|
||||
|
||||
_BUILTINS = [
|
||||
BuiltinHelp(),
|
||||
]
|
||||
_COMMANDS = [
|
||||
]
|
||||
|
||||
def __init__(self, history_file: str = _DEFAULT_HISTORY_FILE) -> None:
|
||||
for cmd in chain(REPL._BUILTINS, REPL._COMMANDS):
|
||||
result = cmd._is_consistent():
|
||||
if result.is_err():
|
||||
raise CmdDeclarationError(result.message)
|
||||
self._history_file = history_file
|
||||
readline.read_history_file(self._history_file)
|
||||
|
||||
def __del__(self) -> None:
|
||||
readline.write_history_file(self._history_file)
|
||||
|
||||
def prompt(self) -> str:
|
||||
return input(REPL._PROMPT)
|
||||
|
||||
'''
|
||||
Parse and execute a string command
|
||||
'''
|
||||
def exec(self, cmd: str) -> Result[Command]:
|
||||
cmd = cmd.strip().split()
|
||||
if not len(cmd):
|
||||
return Result.warn('No command given')
|
||||
|
||||
|
||||
|
||||
Loading…
Add table
Add a link
Reference in a new issue