Added Commands.console

This commit is contained in:
Paul Gauthier 2023-05-10 11:20:25 -07:00
parent c368737e70
commit 6076cd0fb7
2 changed files with 15 additions and 12 deletions

View file

@ -28,7 +28,6 @@ openai.api_key = os.getenv("OPENAI_API_KEY")
class Coder:
fnames = set()
commands = Commands()
last_modified = 0
repo = None
@ -41,6 +40,8 @@ class Coder:
else:
self.console = Console(force_terminal=True, no_color=True)
self.commands = Commands(self.console)
self.main_model = main_model
if main_model == "gpt-3.5-turbo":
self.console.print(
@ -184,7 +185,7 @@ class Coder:
inp = get_input(self.history_file, self.fnames, self.commands)
if inp.startswith("/"):
self.commands.run(inp, self.console)
self.commands.run(inp)
return
self.num_control_c = 0

View file

@ -1,17 +1,18 @@
class Commands:
def __init__(self, console):
self.console = console
def cmd_help(self, args):
print('help')
print("help")
def cmd_ls(self, args):
print('ls')
print("ls")
def get_commands(self):
commands = []
for attr in dir(self):
if attr.startswith("cmd_"):
commands.append('/' + attr[4:])
commands.append("/" + attr[4:])
return commands
@ -21,22 +22,23 @@ class Commands:
if cmd_method:
cmd_method(args)
else:
print(f"Error: Command {cmd_name} not found.")
def run(self, inp, console):
self.console.print(f"Error: Command {cmd_name} not found.")
def run(self, inp):
words = inp.strip().split()
if not words:
return
first_word = words[0]
rest_inp = inp[len(words[0]):]
rest_inp = inp[len(words[0]) :]
all_commands = self.get_commands()
matching_commands = [cmd for cmd in all_commands if cmd.startswith(first_word)]
if len(matching_commands) == 1:
console.print('[green]run', matching_commands[0])
self.console.print("[green]run", matching_commands[0])
self.do_run(matching_commands[0][1:], rest_inp)
elif len(matching_commands) > 1:
console.print('[red]Ambiguous command:', ', '.join(matching_commands))
self.console.print("[red]Ambiguous command:", ", ".join(matching_commands))
else:
console.print(f'[red]Error: {first_word} is not a valid command.')
self.console.print(f"[red]Error: {first_word} is not a valid command.")