From f6b6d7c5cb24fae17cad8a9a7deb4dcebb660d40 Mon Sep 17 00:00:00 2001 From: killian <63927363+KillianLucas@users.noreply.github.com> Date: Thu, 21 Nov 2024 09:38:00 -0800 Subject: [PATCH] Fix input() control chars --- interpreter_1/interpreter.py | 7 +- interpreter_1/misc/get_input copy 2.py | 113 +++++++++++++++++++++++++ interpreter_1/misc/get_input.py | 109 +----------------------- 3 files changed, 116 insertions(+), 113 deletions(-) create mode 100644 interpreter_1/misc/get_input copy 2.py diff --git a/interpreter_1/interpreter.py b/interpreter_1/interpreter.py index 3bf2f332f..5295232c8 100644 --- a/interpreter_1/interpreter.py +++ b/interpreter_1/interpreter.py @@ -40,8 +40,6 @@ from .commands import CommandHandler from .misc.spinner import SimpleSpinner - -# Local imports from .profiles import Profile from .tools import BashTool, ComputerTool, EditTool, ToolCollection, ToolResult from .ui.markdown import MarkdownRenderer @@ -90,16 +88,13 @@ class Interpreter: """ Open Interpreter's main interface. - The Interpreter class provides natural language interaction with your computer, - executing commands and engaging in conversation based on user input. - Examples -------- >>> from interpreter import Interpreter # Basic usage interpreter = Interpreter() - interpreter.chat("Hello, what can you help me with?") + interpreter.chat() # With custom configuration from interpreter import Profile diff --git a/interpreter_1/misc/get_input copy 2.py b/interpreter_1/misc/get_input copy 2.py new file mode 100644 index 000000000..031507439 --- /dev/null +++ b/interpreter_1/misc/get_input copy 2.py @@ -0,0 +1,113 @@ +import asyncio +import fcntl +import os +import random +import sys +import termios + + +async def get_input( + placeholder_text=None, placeholder_color: str = "gray", multiline_support=True +) -> str: + return input("> ") + if placeholder_text is None: + common_placeholders = [ + "How can I help you?", + ] + rare_placeholders = [ + 'Use """ for multi-line input', + "Psst... try the wtf command", + ] + very_rare_placeholders = [""] + + # 69% common, 30% rare, 1% very rare + rand = random.random() + if rand < 0.69: + placeholder_text = random.choice(common_placeholders) + elif rand < 0.99: + placeholder_text = random.choice(rare_placeholders) + else: + placeholder_text = random.choice(very_rare_placeholders) + + placeholder_text = "Describe command" + + # Save terminal settings and set raw mode + old_settings = termios.tcgetattr(sys.stdin.fileno()) + tty_settings = termios.tcgetattr(sys.stdin.fileno()) + tty_settings[3] = tty_settings[3] & ~(termios.ECHO | termios.ICANON) + termios.tcsetattr(sys.stdin.fileno(), termios.TCSADRAIN, tty_settings) + + # Set up non-blocking stdin + fd = sys.stdin.fileno() + flags = fcntl.fcntl(fd, fcntl.F_GETFL) + fcntl.fcntl(fd, fcntl.F_SETFL, flags | os.O_NONBLOCK) + + COLORS = { + "gray": "\033[90m", + "red": "\033[91m", + "green": "\033[92m", + "yellow": "\033[93m", + "blue": "\033[94m", + "magenta": "\033[95m", + "cyan": "\033[96m", + "white": "\033[97m", + } + RESET = "\033[0m" + + current_input = [] + show_placeholder = True + + def redraw(): + sys.stdout.write("\r\033[K") # Clear line + if multiline_support: + sys.stdout.write("\r> ") + if current_input: + sys.stdout.write("".join(current_input)) + elif show_placeholder: + color_code = COLORS.get(placeholder_color.lower(), COLORS["gray"]) + sys.stdout.write(f"{color_code}{placeholder_text}{RESET}") + if multiline_support: + sys.stdout.write("\r> ") + sys.stdout.flush() + + try: + redraw() + while True: + try: + char = os.read(fd, 1).decode() + + if char == "\n": + if current_input: + result = "".join(current_input) + # Multiline support + if multiline_support and result.startswith('"""'): + while True: + print() + extra_input = await get_input(multiline_support=False) + if extra_input.endswith('"""'): + result += extra_input + return result + else: + result += extra_input + else: + return result + else: + redraw() + elif char == "\x7f": # Backspace + if current_input: + current_input.pop() + if not current_input: + show_placeholder = True + elif char == "\x03": # Ctrl+C + raise KeyboardInterrupt + elif char and char.isprintable(): + current_input.append(char) + show_placeholder = False + redraw() + except BlockingIOError: + pass + + finally: + termios.tcsetattr(sys.stdin.fileno(), termios.TCSADRAIN, old_settings) + fcntl.fcntl(fd, fcntl.F_SETFL, flags) + print() diff --git a/interpreter_1/misc/get_input.py b/interpreter_1/misc/get_input.py index e36e80726..65dba7ce1 100644 --- a/interpreter_1/misc/get_input.py +++ b/interpreter_1/misc/get_input.py @@ -1,112 +1,7 @@ -import asyncio -import fcntl -import os -import random -import sys -import termios +import readline async def get_input( placeholder_text=None, placeholder_color: str = "gray", multiline_support=True ) -> str: - if placeholder_text is None: - common_placeholders = [ - "How can I help you?", - ] - rare_placeholders = [ - 'Use """ for multi-line input', - "Psst... try the wtf command", - ] - very_rare_placeholders = [""] - - # 69% common, 30% rare, 1% very rare - rand = random.random() - if rand < 0.69: - placeholder_text = random.choice(common_placeholders) - elif rand < 0.99: - placeholder_text = random.choice(rare_placeholders) - else: - placeholder_text = random.choice(very_rare_placeholders) - - placeholder_text = "Describe command" - - # Save terminal settings and set raw mode - old_settings = termios.tcgetattr(sys.stdin.fileno()) - tty_settings = termios.tcgetattr(sys.stdin.fileno()) - tty_settings[3] = tty_settings[3] & ~(termios.ECHO | termios.ICANON) - termios.tcsetattr(sys.stdin.fileno(), termios.TCSADRAIN, tty_settings) - - # Set up non-blocking stdin - fd = sys.stdin.fileno() - flags = fcntl.fcntl(fd, fcntl.F_GETFL) - fcntl.fcntl(fd, fcntl.F_SETFL, flags | os.O_NONBLOCK) - - COLORS = { - "gray": "\033[90m", - "red": "\033[91m", - "green": "\033[92m", - "yellow": "\033[93m", - "blue": "\033[94m", - "magenta": "\033[95m", - "cyan": "\033[96m", - "white": "\033[97m", - } - RESET = "\033[0m" - - current_input = [] - show_placeholder = True - - def redraw(): - sys.stdout.write("\r\033[K") # Clear line - if multiline_support: - sys.stdout.write("\r> ") - if current_input: - sys.stdout.write("".join(current_input)) - elif show_placeholder: - color_code = COLORS.get(placeholder_color.lower(), COLORS["gray"]) - sys.stdout.write(f"{color_code}{placeholder_text}{RESET}") - if multiline_support: - sys.stdout.write("\r> ") - sys.stdout.flush() - - try: - redraw() - while True: - try: - char = os.read(fd, 1).decode() - - if char == "\n": - if current_input: - result = "".join(current_input) - # Multiline support - if multiline_support and result.startswith('"""'): - while True: - print() - extra_input = await get_input(multiline_support=False) - if extra_input.endswith('"""'): - result += extra_input - return result - else: - result += extra_input - else: - return result - else: - redraw() - elif char == "\x7f": # Backspace - if current_input: - current_input.pop() - if not current_input: - show_placeholder = True - elif char == "\x03": # Ctrl+C - raise KeyboardInterrupt - elif char and char.isprintable(): - current_input.append(char) - show_placeholder = False - redraw() - except BlockingIOError: - pass - - finally: - termios.tcsetattr(sys.stdin.fileno(), termios.TCSADRAIN, old_settings) - fcntl.fcntl(fd, fcntl.F_SETFL, flags) - print() + return input("> ")