ソースを参照

~/.bin/rn: Run and notify if it runs "long"

Where long is defined as "longer than one second" for now.  Might help
with these media download thingies?  Also only notifies if the window is
not focused. :)
Lu Stadler 7 年 前
コミット
3601ae21b6
共有2 個のファイルを変更した67 個の追加1 個の削除を含む
  1. 4 1
      .bin/Makefile
  2. 63 0
      .bin/rn.go

+ 4 - 1
.bin/Makefile

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

+ 63 - 0
.bin/rn.go

@ -0,0 +1,63 @@
1
package main
2
3
import (
4
	"fmt"
5
	"os"
6
	"os/exec"
7
	"strings"
8
	"time"
9
)
10
11
func main() {
12
	if len(os.Args) < 2 {
13
		fmt.Fprintf(os.Stderr, "Usage: %s <cmd> [<args>*]\n", os.Args[0])
14
		os.Exit(1)
15
	}
16
17
	win := xGetActiveWindow()
18
19
	cmd := exec.Command(os.Args[1], os.Args[2:]...)
20
	cmd.Stdin = os.Stdin
21
	cmd.Stdout = os.Stdout
22
	cmd.Stderr = os.Stderr
23
24
	start := time.Now()
25
	err := cmd.Run()
26
	duration := time.Since(start)
27
28
	winAfter := xGetActiveWindow()
29
30
	msg := fmt.Sprintf("Finished running %q in %s", cmdToString(cmd), duration)
31
	if duration > 1*time.Second && win != winAfter {
32
		notifyCompletion(msg)
33
	}
34
	fmt.Println(msg)
35
36
	success := true
37
	if err != nil {
38
		success = false
39
		fmt.Fprintf(os.Stderr, "Error: %s\n", err)
40
	}
41
42
	if !success {
43
		os.Exit(1)
44
	}
45
}
46
47
func notifyCompletion(msg string) {
48
	notifyCmd := exec.Command("notify-send", msg)
49
	notifyCmd.Run()
50
}
51
52
func xGetActiveWindow() string {
53
	cmd := exec.Command("xdotool", "getactivewindow")
54
	out, err := cmd.Output()
55
	if err != nil {
56
		return ""
57
	}
58
	return string(out)
59
}
60
61
func cmdToString(cmd *exec.Cmd) string {
62
	return strings.Join(cmd.Args, " ")
63
}