I'm trying to update the previous command prompt with the time when the command was written.
With the code I wrote, typing the following command
[--:--] costam:~ $ echo "Wrote this at 10:20"
and launching it after five minutes, results in this output:
[10:25] costam:~ $ echo "Wrote this at 10:20"
Wrote this at 10:20
The code in the zsh-theme to achieve this, is the following
PROMPT='[--:--] %{$fg[red]%}$USER:%{$reset_color%}%{$fg[green]%}%c%{$reset_color%}$(git_prompt_info) %(!.#.$) '
preexec () {
DATE=`date +"%H:%M"`
echo -e "\r\033[1A[${DATE}]"
}
The problem is when the command exceed one line or there's an activated virtual env. In such cases, the prompt is not overridden where it should, and results in this:
# Multiline
[--:--] costam:~ (master) $ cat ~/.oh-my-zsh/custom/themes/davever
[10:20]-theme
# Virtualenv
[10:20]env) [--:--] costam:~ (master) $ echo "Broken"
Broken
A solution I was thinking of, was of finding the current prompt in preexec()
and then replace [--:--]
with the current time, but I don't know how or if is even possible to get the current prompt.
Any other solution is welcome, as long as the time is printed in the same way (or very similar) and not, for example, printing on the right side using RPROMT
or printing in the new prompt.
Thank you!
\e[s
at the beginning ofPROMPT
to save the current terminal position, then use\e[u
instead of\r
to move the cursor to the saved position. Howeverpreexec
executes after the newline to terminate the command is printed to the screen, which means the location of the prompt has changed by the timepreexec
writes the date. I'm not sure there is a good solution to this. – Yusem