Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

Re: trick: up-to-the-minute time in prompt

Atom Smasher wrote:
> here's a neat trick i just figured out... have the time that's displayed
> in your prompt update itself!

Here's a version that uses sched; you need 4.3.4 with the fix to sched
that makes it run from within zle if the shell is idle when the period
expires.  Its advantage is it's completely non-invasive, not clashing
with anything else (except a function of the same name, obviously).  If
you have time escapes in your prompt you can just run it once and the
times will then update, otherwise you need the uptime/psvar etc. trickery.

schedprompt() {
  emulate -L zsh
  zmodload -i zsh/sched

  # Remove existing event, so that multiple calls to
  # "schedprompt" work OK.  (You could put one in precmd to push
  # the timer 30 seconds into the future, for example.)
  integer i=${"${(@)zsh_scheduled_events#*:*:}"[(I)schedprompt]}
  (( i )) && sched -$i

  # Test that zle is running before calling the widget (recommended
  # to avoid error messages).
  # Otherwise it updates on entry to zle, so there's no loss.
  zle && zle reset-prompt

  # This ensures we're not too far off the start of the minute
  sched +30 schedprompt

Peter Stephenson <pws@xxxxxxx>                  Software Engineer
CSR PLC, Churchill House, Cambridge Business Park, Cowley Road
Cambridge, CB4 0WZ, UK                          Tel: +44 (0)1223 692070

Messages sorted by: Reverse Date, Date, Thread, Author