improve multi-line editing with prompt_toolkit
This commit is contained in:
parent
30f2585d33
commit
89981a82f8
51
assistant.py
51
assistant.py
|
|
@ -4,6 +4,9 @@ from ollama import Client
|
||||||
import re
|
import re
|
||||||
import pyperclip
|
import pyperclip
|
||||||
import sys
|
import sys
|
||||||
|
import tty
|
||||||
|
import termios
|
||||||
|
import signal
|
||||||
import argparse
|
import argparse
|
||||||
import pygments
|
import pygments
|
||||||
from pygments.lexers import get_lexer_by_name, guess_lexer
|
from pygments.lexers import get_lexer_by_name, guess_lexer
|
||||||
|
|
@ -11,6 +14,9 @@ from pygments.formatters import TerminalFormatter
|
||||||
import os
|
import os
|
||||||
import json
|
import json
|
||||||
|
|
||||||
|
from prompt_toolkit.key_binding import KeyBindings
|
||||||
|
from prompt_toolkit import PromptSession
|
||||||
|
|
||||||
server = 'localhost:11434'
|
server = 'localhost:11434'
|
||||||
model = 'gemma3:12b'
|
model = 'gemma3:12b'
|
||||||
reasoning_model='deepseek-r1:14b'
|
reasoning_model='deepseek-r1:14b'
|
||||||
|
|
@ -342,37 +348,24 @@ def handle_piped_input(args):
|
||||||
if args.copy and len(blocks):
|
if args.copy and len(blocks):
|
||||||
copy_string_to_clipboard(blocks[0])
|
copy_string_to_clipboard(blocks[0])
|
||||||
|
|
||||||
def improved_input():
|
kb = KeyBindings()
|
||||||
|
|
||||||
|
@kb.add('c-d')
|
||||||
|
def _(event):
|
||||||
|
event.current_buffer.validate_and_handle()
|
||||||
|
|
||||||
|
session = PromptSession(multiline=True, prompt_continuation='', key_bindings=kb)
|
||||||
|
def improved_input(prompt="> "):
|
||||||
"""
|
"""
|
||||||
Handles multi-line input and prevents accidental sending of pasted content.
|
Returns the full text (including embedded newlines) when you press Ctrl-D.
|
||||||
Returns the complete input text when the user indicates they're done.
|
Arrow keys edit within or across lines automatically.
|
||||||
"""
|
"""
|
||||||
lines = []
|
try:
|
||||||
print("> ", end='', flush=True)
|
text = session.prompt(prompt)
|
||||||
while True:
|
return text
|
||||||
try:
|
except KeyboardInterrupt:
|
||||||
line = sys.stdin.readline()
|
print("\nUser aborted input")
|
||||||
if not line:
|
return None
|
||||||
print("", flush=True) # Some visual feedback that user input has ended
|
|
||||||
break # EOF
|
|
||||||
if line.strip() == '':
|
|
||||||
continue # ignore empty lines
|
|
||||||
lines.append(line)
|
|
||||||
|
|
||||||
# Check for termination signal (Ctrl+D or two consecutive empty lines)
|
|
||||||
if len(lines) > 1 and lines[-2] == '\n' and lines[-1] == '\n':
|
|
||||||
break
|
|
||||||
|
|
||||||
except KeyboardInterrupt:
|
|
||||||
print("\nUser aborted input")
|
|
||||||
return None
|
|
||||||
except Exception as e:
|
|
||||||
print(f"Error reading input: {e}")
|
|
||||||
return None
|
|
||||||
|
|
||||||
# Join the lines and strip any trailing newlines
|
|
||||||
full_input = ''.join(lines).rstrip('\n')
|
|
||||||
return full_input
|
|
||||||
|
|
||||||
def handle_non_piped_input(args):
|
def handle_non_piped_input(args):
|
||||||
if args.shell:
|
if args.shell:
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue