Explorar el Código

~/.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 %!s(int64=7) %!d(string=hace) años
padre
commit
3601ae21b6
Se han modificado 2 ficheros con 67 adiciones y 1 borrados
  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
}