-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathplotval.go
93 lines (77 loc) · 1.45 KB
/
plotval.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
package main
import (
"bytes"
"flag"
"fmt"
"io"
"log"
"os"
"os/exec"
)
func main() {
sigch := make(chan os.Signal)
flag.Usage = usage
flag.Parse()
var files []string
var name string
if flag.NArg() < 1 {
buf := readstdin(sigch)
name = writetmpfile(buf)
defer os.Remove(name)
files = append(files, name)
} else {
files = flag.Args()
}
var cmds []*exec.Cmd
for _, name := range files {
args := fmt.Sprintf("set term wxt title '%s';", name)
args += fmt.Sprintf("N = system(\"awk 'NR==1{print NF}' %s\");", name)
args += fmt.Sprintf("plot for [i=1:N] \"%s\" u 0:i w l title \"Column \".i;", name)
cmd := exec.Command("gnuplot", "-p", "-e", args)
cmd.Stdin = os.Stdin
cmd.Stderr = os.Stderr
cmd.Stdout = os.Stdout
cmd.Start()
cmds = append(cmds, cmd)
}
for _, cmd := range cmds {
cmd.Wait()
}
}
func ck(err error) {
if err != nil {
log.Fatal(err)
}
}
func usage() {
fmt.Fprintln(os.Stderr, "usage: plotval [options] <file> ...")
flag.PrintDefaults()
os.Exit(2)
}
func readstdin(sigch chan os.Signal) []byte {
var buf []byte
var err error
readch := make(chan bool)
go func() {
buf, err = io.ReadAll(os.Stdin)
if err != nil {
log.Fatal(err)
}
readch <- true
}()
select {
case <-sigch:
os.Exit(1)
case <-readch:
}
return buf
}
func writetmpfile(buf []byte) string {
f, err := os.CreateTemp(".", "plotval")
ck(err)
name := f.Name()
w := bytes.NewBuffer(buf)
io.Copy(f, w)
f.Close()
return name
}