-
Notifications
You must be signed in to change notification settings - Fork 39
/
main.go
157 lines (145 loc) · 3.62 KB
/
main.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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
package main // import "github.com/rs/jplot"
import (
"flag"
"fmt"
"os"
"os/signal"
"strings"
"sync"
"syscall"
"time"
"github.com/monochromegane/terminal"
"github.com/rs/jplot/data"
"github.com/rs/jplot/graph"
"github.com/rs/jplot/term"
)
func main() {
flag.Usage = func() {
out := os.Stderr
fmt.Fprintln(out, "Usage: jplot [OPTIONS] FIELD_SPEC [FIELD_SPEC...]:")
fmt.Fprintln(out, "")
fmt.Fprintln(out, "OPTIONS:")
flag.PrintDefaults()
fmt.Fprintln(out, "")
fmt.Fprintln(out, "FIELD_SPEC: [<option>[,<option>...]:]path")
fmt.Fprintln(out, " option:")
fmt.Fprintln(out, " - counter: Computes the difference with the last value. The value must increase monotonically.")
fmt.Fprintln(out, " - marker: When the value is none-zero, a vertical line is drawn.")
fmt.Fprintln(out, " path:")
fmt.Fprintln(out, " JSON field path (eg: field.sub-field).")
}
url := flag.String("url", "", "URL to fetch every second. Read JSON objects from stdin if not specified.")
interval := flag.Duration("interval", time.Second, "When url is provided, defines the interval between fetches."+
" Note that counter fields are computed based on this interval.")
steps := flag.Int("steps", 100, "Number of values to plot.")
rows := flag.Int("rows", 0, "Limits the height of the graph output.")
flag.Parse()
if !term.HasGraphicsSupport() {
fatal("iTerm2 or DRCS Sixel graphics required")
}
if os.Getenv("TERM") == "screen" {
fatal("screen and tmux not supported")
}
if len(flag.Args()) == 0 {
flag.Usage()
os.Exit(1)
}
specs, err := data.ParseSpec(flag.Args())
if err != nil {
fatal("Cannot parse spec: ", err)
}
var dp *data.Points
if *url != "" {
dp = data.FromHTTP(*url, *interval, *steps)
} else if !terminal.IsTerminal(os.Stdin) {
dp = data.FromStdin(*steps)
} else {
fatal("neither --url nor stdin is provided")
}
dash := graph.Dash{
Specs: specs,
Data: dp,
}
wg := &sync.WaitGroup{}
wg.Add(1)
defer wg.Wait()
exit := make(chan struct{})
defer close(exit)
go func() {
defer wg.Done()
t := time.NewTicker(time.Second)
defer t.Stop()
c := make(chan os.Signal, 2)
signal.Notify(c, os.Interrupt, syscall.SIGTERM)
i := 0
for {
select {
case <-t.C:
if i == 0 {
prepare(*rows)
defer cleanup(*rows)
}
i++
if i%120 == 0 {
// Clear scrollback to avoid iTerm from eating all the memory.
term.ClearScrollback()
}
term.CursorSavePosition()
render(dash, *rows)
term.CursorRestorePosition()
case <-exit:
if i == 0 {
render(dash, *rows)
}
return
case <-c:
dp.Close()
signal.Stop(c)
}
}
}()
if err := dp.Run(specs); err != nil {
fatal("Data source error: ", err)
}
}
func fatal(a ...interface{}) {
fmt.Println(append([]interface{}{"jplot: "}, a...)...)
os.Exit(1)
}
func prepare(rows int) {
term.HideCursor()
if rows == 0 {
var err error
if rows, err = term.Rows(); err != nil {
fatal("Cannot get window size: ", err)
}
}
print(strings.Repeat("\n", rows))
term.CursorMove(term.Up, rows)
}
func cleanup(rows int) {
term.ShowCursor()
if rows == 0 {
rows, _ = term.Rows()
}
term.CursorMove(term.Down, rows)
print("\n")
}
func render(dash graph.Dash, rows int) {
size, err := term.Size()
if err != nil {
fatal("Cannot get window size: ", err)
}
width, height := size.Width, size.Height
if rows > 0 {
height = size.Height / size.Row * rows
} else {
rows = size.Row
}
// Use iTerm2 image display feature.
term := term.NewImageWriter(width, height)
defer term.Close()
if err := dash.Render(term, width, height); err != nil {
fatal(fmt.Sprintf("cannot render graph: %v", err.Error()))
}
}