From c6088554f68bda27d3b74468f9e65d0ea322a7b6 Mon Sep 17 00:00:00 2001 From: flu0r1ne Date: Mon, 8 May 2023 16:43:30 -0500 Subject: Fix issue with wrap around caused by escape sequence mishandling in GNU readline --- src/gpt_chat_cli/gcli.py | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) 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: -- cgit v1.2.3