Просмотр исходного кода

~/.zshrc,.bin/since.go: Notify when long-running programs finish

Lu Stadler лет назад: 6
Родитель
Сommit
9b1754e495
2 измененных файлов с 31 добавлено и 2 удалено
  1. 30 1
      .bin/since.go
  2. 1 1
      .zshrc

+ 30 - 1
.bin/since.go

3
import (
3
import (
4
	"fmt"
4
	"fmt"
5
	"os"
5
	"os"
6
	"os/exec"
7
	"strings"
6
	"time"
8
	"time"
7
)
9
)
8
10
11
// commandBlacklist is used to filter out some commands from
12
// notifications for long-running commands.
13
var commandBlacklist = []string{
14
	"fg",
15
	"git commit",
16
	"less",
17
	"vim",
18
}
19
9
func main() {
20
func main() {
10
	last := os.Args[1]
21
	last := os.Args[1]
22
	cmdName := os.Args[2]
11
	t, err := time.Parse(time.RFC3339Nano, last)
23
	t, err := time.Parse(time.RFC3339Nano, last)
12
	if err != nil {
24
	if err != nil {
13
		return
25
		return
14
	}
26
	}
15
27
16
	fmt.Printf("(%s) ", time.Since(t))
28
	dur := time.Since(t)
29
	if dur > 10*time.Second && !containsOneOf(cmdName, commandBlacklist) {
30
		// notify window manager (bell character, some window managers/terminals mark when it appears)
31
		fmt.Printf("\a")
32
		// send notification for long-running commands
33
		cmd := exec.Command("notify-send", fmt.Sprintf("%q done", cmdName), fmt.Sprintf("took %s", dur))
34
		cmd.Start()
35
	}
36
	fmt.Printf("(%s) ", dur)
37
}
38
39
func containsOneOf(s string, contains []string) bool {
40
	for _, ss := range contains {
41
		if strings.Contains(s, ss) {
42
			return true
43
		}
44
	}
45
	return false
17
}
46
}

+ 1 - 1
.zshrc

44
	echo -ne "\033]0;\007"
44
	echo -ne "\033]0;\007"
45
	user="%{$fg[red]%}%n%{$reset_color%}"
45
	user="%{$fg[red]%}%n%{$reset_color%}"
46
	dir="%{$fg[cyan]%}%~%{$reset_color%}"
46
	dir="%{$fg[cyan]%}%~%{$reset_color%}"
47
	duration=$(since "$__last_cmd_time")
47
	duration=$(since "$__last_cmd_time" "$history[$(($HISTCMD-1))]")
48
	# store last cmd in psvar[1] (magically avoids escaping problems?)
48
	# store last cmd in psvar[1] (magically avoids escaping problems?)
49
	psvar[1]="$history[$(($HISTCMD-1))]"
49
	psvar[1]="$history[$(($HISTCMD-1))]"
50
	if [ -n "$SSH_CONNECTION" ]; then
50
	if [ -n "$SSH_CONNECTION" ]; then