Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

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

macos - Why is this bash prompt acting strangely/disappearing, and how do I fix it (OS X)?

I admit that I use a somewhat long-winded bash prompt:

--(username)-(Wed April 01|12:00:00)--(~ $

Recently, I got the bright idea to change it so that depending on the exit value from the previous command, if success, the interior elements of the ()'s would be green, and if failure, they would be red. I got it working for the most part (some odd exit statuses will change the color to something else, but I'm ok with it), but when typing a command which is more than one line, and causes the terminal to scroll, the prompt disappears! My prompt worked fine when there was no color, so I'm guessing it is related to my color escaping, and particularly my unclosed ['s, but I can't pin it down.

#.profile
export PS1='--(e[$((32-${?}))mue[0m)-(e[$((32-${?}))mde[0m|e[$((32-${?}))mTe[0m)--(e[$((32-${?}))mwe[0m $ '

Thanks in advance!

See Question&Answers more detail:os

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

1 Reply

0 votes
by (71.8m points)

It sounds like this should solve your problem.

This seems to work for me*:

export PS1='--([e[$((32-${?}))m]u[e[0m])-([e[$((32-${?}))m]d[e[0m]|[e[$((32-${?}))m]T[e[0m])--([e[$((32-${?}))m]w[e[0m] $ '

* well, really export PS1='u@h:w$ ' works for me

To quote the linked post, the answer lies in adding [ and ] around all of your color sequences in your PS1 declaration:

Before I had the following value for PS1:

'e[0;34mh:w [!]$e[0m '

which gave me a nice blue prompt of the following form

hostname:working-directory [command-number]$

However, I had the same line-wrapping problem you did. The fix was to insert [ and ] around the ANSI escapes so that the shell knows not to include them in the line wrapping calculation. This results in the following value for PS1:

'[e[0;34m]h:w [!]$[e[m] '


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...