jomoz
jomoz

Reputation: 29

Problem getting user input using a prompt with ANSI escapes and longish initial text in Bash

I'm trying to get some input from the user at the Bash command line. I want to include some ANSI escapes in the prompt (to underline it) and some initial text, e.g.:

read -e -i "Lucy and Ricky" -p $'\e[4mWhom do you love?\e[m ' response

This works fine. However, when the initial text exceeds a certain length, line editing gets messed up, e.g.:

read -e -i "Lucy and Ricky and Ethel and Fred" -p $'\e[4mWhom do you love?\e[m ' response

When the user presses Ctrl-a to try to move to the beginning of the initial text, the cursor instead ends up a few characters to the right of its start (on the "d" in the first "and"). Then pressing Ctrl-e moves the cursor several characters (specifically, in this case, seven) to the right of the end of the initial text.

Also, if the user tries to edit the end of the initial text after moving past the end of it, e.g., by deleting "and Fred", the content of the response variable ends up being truncated by an additional number of characters (again, seven in this instance). This only happens if the user moved the cursor to the beginning of the line by pressing Ctrl-a first. It seems that trying to move to the beginning of the initial text by pressing Ctrl-a is what messes up readline's tracking of the cursor position.

Here's a script that lets you test this behavior:

echo -e "\nCase 1: No ANSI escapes in prompt, short initial text"
read -e -i "Lucy and Ricky" -p 'Whom do you love? ' response
echo $response

echo -e "\nCase 2: With ANSI escapes in prompt, short initial text"
read -e -i "Lucy and Ricky" -p $'\e[4mWhom do you love?\e[m ' response
echo $response

echo -e "\nCase 3: No ANSI escapes in prompt, longer initial text"
read -e -i "Lucy and Ricky and Ethel and Fred" -p 'Whom do you love? ' response
echo $response

echo -e "\nCase 4: With ANSI escapes in prompt, longer initial text"
read -e -i "Lucy and Ricky and Ethel and Fred" -p $'\e[4mWhom do you love?\e[m ' response
echo $response

In each case, when editing the initial text, try pressing Ctrl-a and then Ctrl-e first. You'll see that everything works as expected until the last case.

Output:

Case 1: No ANSI escapes in prompt, short initial text
Whom do you love? Lucy and Ricky - Works
Lucy and Ricky - Works

Case 2: With ANSI escapes in prompt, short initial text
Whom do you love? Lucy and Ricky - Works
Lucy and Ricky - Works

Case 3: No ANSI escapes in prompt, longer initial text
Whom do you love? Lucy and Ricardo and Ethel and Fred - Works
Lucy and Ricky and Ethel and Fred - Works

Case 4: With ANSI escapes in prompt, longer initial text
Whom do you love? Lucy and Ricky and Ethel and Fred       - No worky
Lucy and Ricky and Ethel and Fred- No worky

Any ideas why this might be happening? Fixes or workarounds?

I'm using GNU bash, version 4.4.23(1)-release (x86_64-apple-darwin18.0.0).

Upvotes: 1

Views: 96

Answers (1)

my guess
my guess

Reputation: 11

ANSI escCodes are invisible, but exist anyway, and as such are added to the visible chars count to move the cursor on the same line.

as a workaround, I suggest you add a line feed to read's prompt...

Upvotes: 1

Related Questions