Skip to content

Commit

Permalink
Fix input() control chars
Browse files Browse the repository at this point in the history
  • Loading branch information
KillianLucas committed Nov 21, 2024
1 parent ab97a24 commit f6b6d7c
Show file tree
Hide file tree
Showing 3 changed files with 116 additions and 113 deletions.
7 changes: 1 addition & 6 deletions interpreter_1/interpreter.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
113 changes: 113 additions & 0 deletions interpreter_1/misc/get_input copy 2.py
Original file line number Diff line number Diff line change
@@ -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()
109 changes: 2 additions & 107 deletions interpreter_1/misc/get_input.py
Original file line number Diff line number Diff line change
@@ -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("> ")

0 comments on commit f6b6d7c

Please sign in to comment.