Skip to content

Commit f6b6d7c

Browse files
committed
Fix input() control chars
1 parent ab97a24 commit f6b6d7c

File tree

3 files changed

+116
-113
lines changed

3 files changed

+116
-113
lines changed

interpreter_1/interpreter.py

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,6 @@
4040

4141
from .commands import CommandHandler
4242
from .misc.spinner import SimpleSpinner
43-
44-
# Local imports
4543
from .profiles import Profile
4644
from .tools import BashTool, ComputerTool, EditTool, ToolCollection, ToolResult
4745
from .ui.markdown import MarkdownRenderer
@@ -90,16 +88,13 @@ class Interpreter:
9088
"""
9189
Open Interpreter's main interface.
9290
93-
The Interpreter class provides natural language interaction with your computer,
94-
executing commands and engaging in conversation based on user input.
95-
9691
Examples
9792
--------
9893
>>> from interpreter import Interpreter
9994
10095
# Basic usage
10196
interpreter = Interpreter()
102-
interpreter.chat("Hello, what can you help me with?")
97+
interpreter.chat()
10398
10499
# With custom configuration
105100
from interpreter import Profile
Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
import asyncio
2+
import fcntl
3+
import os
4+
import random
5+
import sys
6+
import termios
7+
8+
9+
async def get_input(
10+
placeholder_text=None, placeholder_color: str = "gray", multiline_support=True
11+
) -> str:
12+
return input("> ")
13+
if placeholder_text is None:
14+
common_placeholders = [
15+
"How can I help you?",
16+
]
17+
rare_placeholders = [
18+
'Use """ for multi-line input',
19+
"Psst... try the wtf command",
20+
]
21+
very_rare_placeholders = [""]
22+
23+
# 69% common, 30% rare, 1% very rare
24+
rand = random.random()
25+
if rand < 0.69:
26+
placeholder_text = random.choice(common_placeholders)
27+
elif rand < 0.99:
28+
placeholder_text = random.choice(rare_placeholders)
29+
else:
30+
placeholder_text = random.choice(very_rare_placeholders)
31+
32+
placeholder_text = "Describe command"
33+
34+
# Save terminal settings and set raw mode
35+
old_settings = termios.tcgetattr(sys.stdin.fileno())
36+
tty_settings = termios.tcgetattr(sys.stdin.fileno())
37+
tty_settings[3] = tty_settings[3] & ~(termios.ECHO | termios.ICANON)
38+
termios.tcsetattr(sys.stdin.fileno(), termios.TCSADRAIN, tty_settings)
39+
40+
# Set up non-blocking stdin
41+
fd = sys.stdin.fileno()
42+
flags = fcntl.fcntl(fd, fcntl.F_GETFL)
43+
fcntl.fcntl(fd, fcntl.F_SETFL, flags | os.O_NONBLOCK)
44+
45+
COLORS = {
46+
"gray": "\033[90m",
47+
"red": "\033[91m",
48+
"green": "\033[92m",
49+
"yellow": "\033[93m",
50+
"blue": "\033[94m",
51+
"magenta": "\033[95m",
52+
"cyan": "\033[96m",
53+
"white": "\033[97m",
54+
}
55+
RESET = "\033[0m"
56+
57+
current_input = []
58+
show_placeholder = True
59+
60+
def redraw():
61+
sys.stdout.write("\r\033[K") # Clear line
62+
if multiline_support:
63+
sys.stdout.write("\r> ")
64+
if current_input:
65+
sys.stdout.write("".join(current_input))
66+
elif show_placeholder:
67+
color_code = COLORS.get(placeholder_color.lower(), COLORS["gray"])
68+
sys.stdout.write(f"{color_code}{placeholder_text}{RESET}")
69+
if multiline_support:
70+
sys.stdout.write("\r> ")
71+
sys.stdout.flush()
72+
73+
try:
74+
redraw()
75+
while True:
76+
try:
77+
char = os.read(fd, 1).decode()
78+
79+
if char == "\n":
80+
if current_input:
81+
result = "".join(current_input)
82+
# Multiline support
83+
if multiline_support and result.startswith('"""'):
84+
while True:
85+
print()
86+
extra_input = await get_input(multiline_support=False)
87+
if extra_input.endswith('"""'):
88+
result += extra_input
89+
return result
90+
else:
91+
result += extra_input
92+
else:
93+
return result
94+
else:
95+
redraw()
96+
elif char == "\x7f": # Backspace
97+
if current_input:
98+
current_input.pop()
99+
if not current_input:
100+
show_placeholder = True
101+
elif char == "\x03": # Ctrl+C
102+
raise KeyboardInterrupt
103+
elif char and char.isprintable():
104+
current_input.append(char)
105+
show_placeholder = False
106+
redraw()
107+
except BlockingIOError:
108+
pass
109+
110+
finally:
111+
termios.tcsetattr(sys.stdin.fileno(), termios.TCSADRAIN, old_settings)
112+
fcntl.fcntl(fd, fcntl.F_SETFL, flags)
113+
print()

interpreter_1/misc/get_input.py

Lines changed: 2 additions & 107 deletions
Original file line numberDiff line numberDiff line change
@@ -1,112 +1,7 @@
1-
import asyncio
2-
import fcntl
3-
import os
4-
import random
5-
import sys
6-
import termios
1+
import readline
72

83

94
async def get_input(
105
placeholder_text=None, placeholder_color: str = "gray", multiline_support=True
116
) -> str:
12-
if placeholder_text is None:
13-
common_placeholders = [
14-
"How can I help you?",
15-
]
16-
rare_placeholders = [
17-
'Use """ for multi-line input',
18-
"Psst... try the wtf command",
19-
]
20-
very_rare_placeholders = [""]
21-
22-
# 69% common, 30% rare, 1% very rare
23-
rand = random.random()
24-
if rand < 0.69:
25-
placeholder_text = random.choice(common_placeholders)
26-
elif rand < 0.99:
27-
placeholder_text = random.choice(rare_placeholders)
28-
else:
29-
placeholder_text = random.choice(very_rare_placeholders)
30-
31-
placeholder_text = "Describe command"
32-
33-
# Save terminal settings and set raw mode
34-
old_settings = termios.tcgetattr(sys.stdin.fileno())
35-
tty_settings = termios.tcgetattr(sys.stdin.fileno())
36-
tty_settings[3] = tty_settings[3] & ~(termios.ECHO | termios.ICANON)
37-
termios.tcsetattr(sys.stdin.fileno(), termios.TCSADRAIN, tty_settings)
38-
39-
# Set up non-blocking stdin
40-
fd = sys.stdin.fileno()
41-
flags = fcntl.fcntl(fd, fcntl.F_GETFL)
42-
fcntl.fcntl(fd, fcntl.F_SETFL, flags | os.O_NONBLOCK)
43-
44-
COLORS = {
45-
"gray": "\033[90m",
46-
"red": "\033[91m",
47-
"green": "\033[92m",
48-
"yellow": "\033[93m",
49-
"blue": "\033[94m",
50-
"magenta": "\033[95m",
51-
"cyan": "\033[96m",
52-
"white": "\033[97m",
53-
}
54-
RESET = "\033[0m"
55-
56-
current_input = []
57-
show_placeholder = True
58-
59-
def redraw():
60-
sys.stdout.write("\r\033[K") # Clear line
61-
if multiline_support:
62-
sys.stdout.write("\r> ")
63-
if current_input:
64-
sys.stdout.write("".join(current_input))
65-
elif show_placeholder:
66-
color_code = COLORS.get(placeholder_color.lower(), COLORS["gray"])
67-
sys.stdout.write(f"{color_code}{placeholder_text}{RESET}")
68-
if multiline_support:
69-
sys.stdout.write("\r> ")
70-
sys.stdout.flush()
71-
72-
try:
73-
redraw()
74-
while True:
75-
try:
76-
char = os.read(fd, 1).decode()
77-
78-
if char == "\n":
79-
if current_input:
80-
result = "".join(current_input)
81-
# Multiline support
82-
if multiline_support and result.startswith('"""'):
83-
while True:
84-
print()
85-
extra_input = await get_input(multiline_support=False)
86-
if extra_input.endswith('"""'):
87-
result += extra_input
88-
return result
89-
else:
90-
result += extra_input
91-
else:
92-
return result
93-
else:
94-
redraw()
95-
elif char == "\x7f": # Backspace
96-
if current_input:
97-
current_input.pop()
98-
if not current_input:
99-
show_placeholder = True
100-
elif char == "\x03": # Ctrl+C
101-
raise KeyboardInterrupt
102-
elif char and char.isprintable():
103-
current_input.append(char)
104-
show_placeholder = False
105-
redraw()
106-
except BlockingIOError:
107-
pass
108-
109-
finally:
110-
termios.tcsetattr(sys.stdin.fileno(), termios.TCSADRAIN, old_settings)
111-
fcntl.fcntl(fd, fcntl.F_SETFL, flags)
112-
print()
7+
return input("> ")

0 commit comments

Comments
 (0)