From e71aaa8b92b52d47d859322467fdf07e1283591f Mon Sep 17 00:00:00 2001 From: Brayo Date: Fri, 1 Nov 2024 10:12:41 +0300 Subject: [PATCH] refactor: revert to "[c] to copy" --- gptme/util.py | 32 ++++++-------------------------- 1 file changed, 6 insertions(+), 26 deletions(-) diff --git a/gptme/util.py b/gptme/util.py index 78dd02fb..ac91ce6f 100644 --- a/gptme/util.py +++ b/gptme/util.py @@ -3,7 +3,6 @@ import random import re import sys -import tty import termios import textwrap from datetime import datetime, timedelta @@ -169,17 +168,15 @@ def ask_execute(question="Execute code?", default=True) -> bool: # pragma: no c termios.tcflush(sys.stdin, termios.TCIFLUSH) # flush stdin choicestr = f"[{'Y' if default else 'y'}/{'n' if default else 'N'}]" - copystr = "[Ctrl+c] to copy" - console.print( - f"[bold bright_yellow on red] {question} {choicestr} {copystr if copiable else ''} [/] ", - end="", + copystr = r"\[c] to copy" if copiable else "" + answer = console.input( + f"[bold bright_yellow on red] {question} {choicestr}{copystr} [/] ", ) - print() - answer = listen_for_character() - if copiable and answer == "\x03": + if copiable and "c" in answer.lower(): if copy(): print("Copied to clipboard.") - return False + if len(answer) > 1: + answer = answer.lower().strip("c") global override_auto if answer.lower() in [ @@ -339,20 +336,3 @@ def path_with_tilde(path: Path) -> str: if path_str.startswith(home): return path_str.replace(home, "~", 1) return path_str - - -def listen_for_character(): - fd = sys.stdin.fileno() - old_settings = termios.tcgetattr(fd) - try: - tty.setraw(sys.stdin.fileno()) - ch = sys.stdin.read(1) - if ch in ("\n", "\r"): - # handle enter key - ch = "" - except KeyboardInterrupt: - ch = "\x03" # ASCII code for Ctrl+C - finally: - termios.tcsetattr(fd, termios.TCSADRAIN, old_settings) - clear_copiable() - return ch