zsh:显示命令花费的时间

zsh: show time spent in command

我有一些可以持续 15 分钟或更长时间的长 运行 命令。

有没有办法在输出中输出动态的“花费时间”部分?

例如

$ `do something long`
10 mins 15 secs
Refreshing state...

然后:

$ `do something long`
14 mins 15 secs
Refreshing state...
Output results
$ ... and we're done
zmodload -F zsh/system p:sysparams # Load the sysparams table.
zmodload -F zsh/terminfo b:echoti  # Load the echoti builtin.

timed() {
  local +h -i SECONDS=0

  # Open an async process for the timer.
  local -i timer_fd
  exec {timer_fd}< <(
    # Print the process id, so we can kill it later.
    print $sysparams[pid] 

    echoti civis   # Make the cursor invisible.

    while true; do
      print "$(( SECONDS / 60 )) mins, $(( SECONDS % 60 )) secs"
      echoti cuu1  # Move the cursor up 1 line.
    done
  )

  # Read the timer process id.
  local -i timer_pid
  read timer_pid <&$timer_fd

  # Start another async process for the actual task.
  local -i task_fd
  exec {task_fd}< <(
    eval "$@"     # Run the task.

    # Close the timer fd and kill the timer process.
    exec {timer_fd}<&-
    kill -KILL $timer_pid

    echoti cnorm  # Make the cursor normal again.
  )

  # Redirect the output of the async processes to the terminal. This is
  # a blocking operation. Thus, we won't get past these two lines until
  # both async processes end.
  <&$timer_fd
  <&$task_fd

  exec {task_fd}<&-  # We're done. Close the task fd.
}

用法示例:

% timed 'sleep 855; print Output results'

参考资料