Browse Source

~/.zshrc: Display cmd runtime and improve cmd escaping

Lu Stadler 7 years ago
parent
commit
a4a05c589a
3 changed files with 33 additions and 5 deletions
  1. 4 1
      .bin/Makefile
  2. 17 0
      .bin/since.go
  3. 12 4
      .zshrc

+ 4 - 1
.bin/Makefile

@ -1,8 +1,11 @@
1
all: rn timely up ydl
1
all: rn since timely up ydl
2 2
3 3
rn: rn.go
4 4
	go build $<
5 5
6
since: since.go
7
	go build $<
8
6 9
timely: timely.go
7 10
	go build timely.go
8 11

+ 17 - 0
.bin/since.go

@ -0,0 +1,17 @@
1
package main
2
3
import (
4
	"fmt"
5
	"os"
6
	"time"
7
)
8
9
func main() {
10
	last := os.Args[1]
11
	t, err := time.Parse(time.RFC3339Nano, last)
12
	if err != nil {
13
		return
14
	}
15
16
	fmt.Printf("(%s) ", time.Since(t))
17
}

+ 12 - 4
.zshrc

@ -37,19 +37,27 @@ function repo_branch {
37 37
	fi
38 38
}
39 39
40
__last_cmd_time=""
41
40 42
autoload colors && colors
41 43
function precmd {
42 44
	echo -ne "\033]0;\007"
43 45
	user="%{$fg[red]%}%n%{$reset_color%}"
44 46
	dir="%{$fg[cyan]%}%~%{$reset_color%}"
45
	lastcmd="$history[$(($HISTCMD-1))]"
46
	lastcmd_witherror="%(?!$lastcmd!%B$lastcmd%b)"
47
	duration=$(since "$__last_cmd_time")
48
	# store last cmd in psvar[1] (magically avoids escaping problems?)
49
	psvar[1]="$history[$(($HISTCMD-1))]"
47 50
	if [ -n "$SSH_CONNECTION" ]; then
48 51
		server="@`echo $SSH_CONNECTION | cut -d' ' -f3`"
49 52
	fi
50
	PROMPT="$user$server in $dir@$(repo_branch) $lastcmd_witherror
53
	PROMPT="$user$server in $dir@$(repo_branch) $duration%(?.%1v.%B%1v%b)
51 54
$(repo_char) $ "
52
	RPROMPT="%D{%Y-%m-%d} %{$fg[red]%}%t%{$reset_color%}"
55
	#RPROMPT="%D{%Y-%m-%d} %{$fg[red]%}%t%{$reset_color%}"
56
	__last_cmd_time=$(date +%Y-%m-%dT%H:%M:%S.%N%:z)
57
}
58
59
function preexec {
60
	__last_cmd_time=$(date +%Y-%m-%dT%H:%M:%S.%N%:z)
53 61
}
54 62
55 63
bindkey -e