aider/aider/getinput.py
Paul Gauthier 9b734cb149 aider: Color non-tty printing green using Rich in get_input function
# Context:
USER: use rich to color the non tty printing green
ASSISTANT: To color the non-tty printing green using Rich, you can update the `get_input` function in the `aider/getinput.py` file as follows:

```python
aider/getinput.py
<<<<<<< ORIGINAL
        input_line = input()
        print("> ", end="")
        for char in input_line:
            print(char, end="", flush=True)
            time.sleep(random.uniform(0.05, 0.1))
        print()
        print()
=======
        input_line = input()
        console.print("> ", end="", style="green")
        for char in input_line:
            console.print(char, end="", style="green", flush=True)
            time.sleep(random.uniform(0.05, 0.1))
        console.print()
        console.print()
>>>>>>> UPDATED
```

This change replaces the standard `print` function with `console.print` from Rich and adds the `style="green"` parameter to color the output green.
2023-05-09 14:23:02 -07:00

78 lines
2 KiB
Python

import re
from prompt_toolkit.styles import Style
from prompt_toolkit import prompt
from prompt_toolkit.completion import Completer, Completion
from prompt_toolkit.history import FileHistory
class FileContentCompleter(Completer):
def __init__(self, fnames):
self.fnames = fnames
def get_completions(self, document, complete_event):
text = document.text_before_cursor
words = text.split()
if not words:
return
last_word = words[-1]
for fname in self.fnames:
with open(fname, "r") as f:
content = f.read()
for word in re.split(r'\W+', content):
if word.startswith(last_word):
yield Completion(word, start_position=-len(last_word))
from rich.console import Console
import sys
import time
import random
console = Console()
def get_input(history_file, fnames):
if not sys.stdin.isatty():
input_line = input()
console.print("> ", end="", style="green")
for char in input_line:
console.print(char, end="", style="green", flush=True)
time.sleep(random.uniform(0.05, 0.1))
console.print()
console.print()
return input_line
inp = ""
multiline_input = False
style = Style.from_dict({"": "green"})
while True:
completer_instance = FileContentCompleter(fnames)
if multiline_input:
show = ". "
else:
show = "> "
try:
line = prompt(
show,
completer=completer_instance,
history=FileHistory(history_file),
style=style,
)
except EOFError:
return
if line.strip() == "{" and not multiline_input:
multiline_input = True
continue
elif line.strip() == "}" and multiline_input:
break
elif multiline_input:
inp += line + "\n"
else:
inp = line
break
print()
return inp