Decimal time in your Bash prompt

While testing flight_price, I would run into this problem where I would start a record_prices command, go do something else, come back, and want to know how long the command had been running for. I wanted a way to include a short string representing the current time in my bash prompt unintrusively. The record_prices command can take upwards of an hour to complete, so the decimal time representation, especially the 1/1000ths of a day representation used by Swatch Internet Time, seemed like a perfect fit. It's granular enough to display how many minutes have elapsed since I started running the command, but compact enough to not take up precious screen space in my prompt.

I wrote a simple little utility, dectime, to print the current time in decimal, and to convert decimal times back into "normal" times. Putting the dectime on the left side of the prompt looked weird, so I decided to try right-aligning it. This is the prompt code I ended up using:


Excerpt from ~/.bashrc

function right_align_time { printf "%*s\r" $(( COLUMNS-1 )) "@dectime" }

PS1="\$(right_align_time)[\u@\h \W]\$ " ```

The \$(right_align_time) part instructs bash to re-execute the right_align_time function every time the prompt is printed. right_align_time uses printf to right-align the output (using the $COLUMNS variable) and prints @ followed by the output of dectime. 1 If you want to use dectime on your machine, it's just a single .cpp file and should compile easily with no dependencies on any *nix machine. Here's what my terminal looks like with dectime added:


comments powered by Disqus