Skip to content

Commit

Permalink
fix: escaping backslashes to prevent them from escaping other stuff (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
petereon authored Aug 5, 2024
1 parent 5b60fa8 commit 8b3f7c6
Showing 1 changed file with 8 additions and 3 deletions.
11 changes: 8 additions & 3 deletions beaupy/_internals.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,12 +79,17 @@ def _update_rendered(live: Live, renderable: Union[ConsoleRenderable, str]) -> N
def _render_prompt(secure: bool, state: qprompt.PromptState) -> str:
typed_values = [*(state.value or '')]
input_value = len(typed_values) * '*' if secure else ''.join(typed_values)

# Escape backslashes to prevent them from being interpreted as escape characters
cursor_position = state.cursor_position + input_value.count('\\')
input_value = input_value.replace('\\', '\\\\')

render_value = ( # noqa: ECE001
(input_value + ' ')[: state.cursor_position]
(input_value + ' ')[: cursor_position]
+ '[black on white]' # noqa: W503
+ (input_value + ' ')[state.cursor_position] # noqa: W503
+ (input_value + ' ')[cursor_position] # noqa: W503
+ '[/black on white]' # noqa: W503
+ (input_value + ' ')[(state.cursor_position + 1) :] # noqa: W503,E203
+ (input_value + ' ')[(cursor_position + 1) :] # noqa: W503,E203
)

if state.completion.options and not secure:
Expand Down

0 comments on commit 8b3f7c6

Please sign in to comment.