From f86b963120c8b09c00508bf1a0d787fa7d1888eb Mon Sep 17 00:00:00 2001 From: Till Höppner Date: Sun, 11 Mar 2018 15:52:48 +0100 Subject: Prettier duration printing --- conf.d/60-prompt.zsh | 34 +++++++++++++++++++++++++++++++++- 1 file changed, 33 insertions(+), 1 deletion(-) diff --git a/conf.d/60-prompt.zsh b/conf.d/60-prompt.zsh index 1ae2531..66146a5 100644 --- a/conf.d/60-prompt.zsh +++ b/conf.d/60-prompt.zsh @@ -1,6 +1,9 @@ bindkey -e # emacs-style keybindings setopt autocd +# last line without newline terminator +# is swallowed if this is absent +setopt prompt_sp autoload -U zmv @@ -21,6 +24,25 @@ elapsed() { echo $(($(timestamp) - $command_start_time)) } +humanize_duration() { + # ORS= disables the trailing newline + command awk -v ORS= ' + function hmTime(time, stamp) { + split("h:m:s:ms", units, ":") + for (i = 2; i >= -1; i--) { + if (t = int( i < 0 ? time % 1000 : time / (60 ^ i * 1000) % 60 )) { + stamp = stamp t units[sqrt((i - 2) ^ 2) + 1] " " + } + } + if (stamp ~ /^ *$/) { + return "0ms" + } + return substr(stamp, 1, length(stamp) - 1) + } + { print hmTime($0) } + ' +} + duration=0 command_start_time=$(timestamp) # called just before line is evaluated @@ -64,6 +86,16 @@ left_prompt() { echo -n "@$(hostname | tr -d '\n') " fi + if test -n "$IN_NIX_SHELL"; then + zcol "$fg[cyan]" + echo -n "nix " + fi + + if test -n "$IN_BWRAP"; then + zcol "$fg[cyan]" + echo -n "bwrap " + fi + if test -n "$DOCKER_HOST"; then zcol "$fg[cyan]" echo -n "$(basename "$DOCKER_HOST") " @@ -91,7 +123,7 @@ right_prompt() { command_start_time=$(timestamp) if (($duration > 0.99)); then zcol "$fg_bold[cyan]" - printf "%.1fs" "$duration" + humanize_duration <<<"$(( $duration * 1000 ))" fi zcol "$reset_color" -- cgit v1.2.3