diff options
author | flu0r1ne <flu0r1ne@flu0r1ne.net> | 2023-05-08 16:43:30 -0500 |
---|---|---|
committer | flu0r1ne <flu0r1ne@flu0r1ne.net> | 2023-05-08 16:43:30 -0500 |
commit | c6088554f68bda27d3b74468f9e65d0ea322a7b6 (patch) | |
tree | b04423c337486f192206de6b487ad8f7f29e5cb5 /src | |
parent | 883fe86bb9be501df1dfec94fd6a0fc81b9a8602 (diff) | |
download | gpt-chat-cli-c6088554f68bda27d3b74468f9e65d0ea322a7b6.tar.xz gpt-chat-cli-c6088554f68bda27d3b74468f9e65d0ea322a7b6.zip |
Fix issue with wrap around caused by escape sequence mishandling in GNU readline
Diffstat (limited to 'src')
-rw-r--r-- | src/gpt_chat_cli/gcli.py | 21 |
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: |