aboutsummaryrefslogtreecommitdiff
path: root/src/gpt_chat_cli
diff options
context:
space:
mode:
authorFurkan Sahin <furkan-dev@proton.me>2023-05-08 16:43:30 -0500
committerFurkan Sahin <furkan-dev@proton.me>2023-05-08 16:43:30 -0500
commit995319bffae51d716ab3ebff2f1708e189a75c09 (patch)
tree0336e6d3990f885f4ccc1075c7c9660ab7486f0a /src/gpt_chat_cli
parentb0e83140a24e94390edd2995d0090917795be6b0 (diff)
Fix issue with wrap around caused by escape sequence mishandling in GNU readline
Diffstat (limited to 'src/gpt_chat_cli')
-rw-r--r--src/gpt_chat_cli/gcli.py21
1 files changed, 21 insertions, 0 deletions
diff --git a/src/gpt_chat_cli/gcli.py b/src/gpt_chat_cli/gcli.py
index 5f2a478..916542b 100644
--- a/src/gpt_chat_cli/gcli.py
+++ b/src/gpt_chat_cli/gcli.py
@@ -219,6 +219,26 @@ def cmd_list_models():
for model in list_models():
print(model)
+def surround_ansi_escapes(prompt, start = "\x01", end = "\x02"):
+ '''
+ Fixes issue on Linux with the readline module
+ See: https://github.com/python/cpython/issues/61539
+ '''
+ escaped = False
+ result = ""
+
+ for c in prompt:
+ if c == "\x1b" and not escaped:
+ result += start + c
+ escaped = True
+ elif c.isalpha() and escaped:
+ result += c + end
+ escaped = False
+ else:
+ result += c
+
+ return result
+
def cmd_interactive(args : Arguments):
enable_emacs_editing()
@@ -231,6 +251,7 @@ def cmd_interactive(args : Arguments):
hist = [ get_system_message( args.system_message ) ]
PROMPT = f'[{COLOR_CODE.WHITE}#{COLOR_CODE.RESET}] '
+ PROMPT = surround_ansi_escapes(PROMPT)
def prompt_message() -> bool: