Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
3.6k views
in Technique[技术] by (71.8m points)

mysql - Is it possible to set MYSQL_PS1 with colors without messing up prompt line width calculation?

Consider the following setting for MySQL_PS1:

GREEN="$(echo -e '33[01;32m')"
BLUE="$(echo -e '33[01;34m')"
RESET="$(echo -e '33[00m')"
export MYSQL_PS1="${GREEN}STAGING-\d${BLUE} >${RESET} "

When I start mysql, and type a command that is longer than the visual line length, it wraps without a new line, overwriting the beginning of line.

I tried the [ ] trick mentioned here from here.

export MYSQL_PS1="[${GREEN}]STAGING-\d[${BLUE}] >[${RESET}] "

Unfortunately, it produced this prompt instead of correctly excluding the escape characters from the line length.

] >[]  >[] ]   >[] ]STAGING-d[] >[] STAGING-messenger[] >[]

Is there a way to set colors in MYSQL_PS1 without messing up the estimated line length?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

I found the answer in this comment, which I will repost here.

The correct incantation is to surround the escape sequence with 01 and 02.

GREEN="$(echo -e '0133[01;32m02')"
BLUE="$(echo -e '0133[01;34m02')"
RESET="$(echo -e '0133[00m02')"
export MYSQL_PS1="${GREEN}STAGING-\d${BLUE} >${RESET} "

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...