Sfoglia il codice sorgente

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

Lu Stadler 6 anni fa
parent
commit
9b1754e495
2 ha cambiato i file con 31 aggiunte e 2 eliminazioni
  1. 30 1
      .bin/since.go
  2. 1 1
      .zshrc

+ 30 - 1
.bin/since.go

@ -3,15 +3,44 @@ package main
3 3
import (
4 4
	"fmt"
5 5
	"os"
6
	"os/exec"
7
	"strings"
6 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 20
func main() {
10 21
	last := os.Args[1]
22
	cmdName := os.Args[2]
11 23
	t, err := time.Parse(time.RFC3339Nano, last)
12 24
	if err != nil {
13 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,7 +44,7 @@ function precmd {
44 44
	echo -ne "\033]0;\007"
45 45
	user="%{$fg[red]%}%n%{$reset_color%}"
46 46
	dir="%{$fg[cyan]%}%~%{$reset_color%}"
47
	duration=$(since "$__last_cmd_time")
47
	duration=$(since "$__last_cmd_time" "$history[$(($HISTCMD-1))]")
48 48
	# store last cmd in psvar[1] (magically avoids escaping problems?)
49 49
	psvar[1]="$history[$(($HISTCMD-1))]"
50 50
	if [ -n "$SSH_CONNECTION" ]; then