0

I'm using Mac. I want to set PS1 variable to get current time at the end of line in command line prompt. Like this: exmaple 1 or this: example 2

But, unfortunately, I can't get what I want through following configuration:

CYAN="\[$(tput setaf 6)\]"
GREEN="\[$(tput setaf 2)\]" 
BLUE="\[$(tput setaf 4)\]"
YELLOW="\[$(tput setaf 3)\]" 
export PS1="$GREEN\u$BLUE@$CYAN\h $YELLOW\w\[$(tput cuf $(($(tput cols)-33)))\]$CYAN\[$(date +%H:%M)\]\n\\$ "

It seems that I need to get the current position of cursor and do some computations based on the coordinate of the position, but I don't know how to get it. Is anyone can help?

Thomas Dickey
  • 51,086
  • 7
  • 70
  • 105
AngelIW
  • 23
  • 7
  • You might consider using `zsh` instead; it has built-in support for a prompt that appears on the right side of the screen with the `RPROMPT` / `RPS1` variable. – chepner May 09 '15 at 16:05

2 Answers2

1

There are a few issues. This chunk

$(tput cuf $(($(tput cols)-33)))

appears to be what you are asking about. If you want to position the date at the end of the line, you might try using the hpa (horizontal position absolute) capability, e.g.,

$(tput hpa $(($(tput cols)-5)))

That way, you can move the cursor to a given absolute position on the line rather than moving relative to the current position.

The prompt as given occupies two lines. If you use the sc and rc capabilities (save/restore cursor) before and after the part where you write the date, you can reduce that to a single line. That might run into problems with line-editing clearing the date, but it is something to consider.

Thomas Dickey
  • 51,086
  • 7
  • 70
  • 105
  • Hi @Thomas, I found that it's a good solution but not perfect, do you have any ideas to improve it? Considering following situation: ![screencut](http://i.stack.imgur.com/T4GtY.png) That is to say, when the path string is long while not long enough to a new line, it will be covered by the date string. Any idea to solve it? For your recommendation about reducing that to a single line, actually I tried it. But as you said, it has problem with line-editing clearing the date. So I prefer the multiline solution. – AngelIW May 12 '15 at 02:04
  • I don't see much possibility for improvement since bash either *knows* the length of a path string itself - and writes it without your formatting - or the string is escaped. – Thomas Dickey May 12 '15 at 08:44
  • I found another issue of current solution. When I resize the command line prompt window, the format will be damaged, although I refreshed the prompt. It seems like the setting of PS1 will load just once when the window is launched, so the printed prompt will not change when I resize the window. Any solution? – AngelIW May 12 '15 at 15:52
0

Thanks @Thomas for the good answer, it can truly solve the question I asked as a work-around solution. But it is not perfect, which means it still cannot get the command prompt that I want.

I found that there should be no solution for the prompt I want in bash. But, as @chepner said, there do exist methods to get the prompt in zsh.

How to get the prompt in zsh:

zsh is a very powerful shell which is built-in in mac, but it's a little complicate for the newbie to it. So there is a great tools to solve it named "oh-my-zsh" which make the use of zsh much easier.

"oh-my-zsh" include many themes for zsh. And, fortunately, the prompt I want is very similar with one of them(see the themes here), which named blink. So I just need to modify a little of the theme file located in ~/.oh-my-zsh/themes/blinks.zsh-theme and the change needed to do is:

change the last line of the file from:

RPROMPT='%{%B%F{cyan}%}%!%{%f%k%b%}

to

RPROMPT='%{%B%F{cyan}%}%@%{%f%k%b%}

AngelIW
  • 23
  • 7