Skip to content

Commit 03cd9be

Browse files
committed
Add support for --rows option
1 parent 69dd65d commit 03cd9be

File tree

5 files changed

+57
-14
lines changed

5 files changed

+57
-14
lines changed

README.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
Jplot tracks expvar-like (JSON) metrics and plot their evolution over time right into your iTerm2 terminal.
55

6-
![all](doc/demo.gif)
6+
![](doc/demo.gif)
77

88
Above capture is jplot monitoring a Go service's [expvar](https://golang.org/pkg/expvar/):
99

@@ -15,6 +15,10 @@ jplot --url http://:8080/debug/vars \
1515
memstats.StackSys+memstats.StackInuse
1616
```
1717

18+
By default, jplot uses the full size of the terminal, but it is possible to limit the render to a few rows:
19+
20+
![](doc/rows.gif)
21+
1822
## Install
1923

2024
```

doc/demo.gif

116 KB
Loading

doc/rows.gif

250 KB
Loading

main.go

Lines changed: 43 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"fmt"
66
"os"
77
"os/signal"
8+
"strings"
89
"sync"
910
"syscall"
1011
"time"
@@ -19,6 +20,7 @@ func main() {
1920
interval := flag.Duration("interval", time.Second, "When url is provided, defines the interval between fetches."+
2021
" Note that counter fields are computed based on this interval.")
2122
steps := flag.Int("steps", 100, "Number of values to plot.")
23+
rows := flag.Int("rows", 0, "Limits the height of the graph output.")
2224
flag.Parse()
2325

2426
if os.Getenv("TERM_PROGRAM") != "iTerm.app" {
@@ -51,32 +53,29 @@ func main() {
5153
defer close(exit)
5254
go func() {
5355
defer wg.Done()
54-
osc.Clear()
55-
osc.HideCursor()
56-
defer osc.ShowCursor()
5756
t := time.NewTicker(time.Second)
5857
defer t.Stop()
5958
c := make(chan os.Signal, 2)
6059
signal.Notify(c, os.Interrupt, syscall.SIGTERM)
6160
i := 0
61+
prepare(*rows)
62+
defer cleanup(*rows)
6263
for {
63-
size, err := osc.Size()
64-
if err != nil {
65-
fatal("Cannot get window size: ", err)
66-
}
6764
select {
6865
case <-t.C:
6966
i++
7067
if i%120 == 0 {
7168
// Clear scrollback to avoid iTerm from eating all the memory.
7269
osc.ClearScrollback()
7370
}
74-
render(dash, size.Width, size.Height)
71+
osc.CursorSavePosition()
72+
render(dash, *rows)
73+
osc.CursorRestorePosition()
7574
case <-exit:
76-
render(dash, size.Width, size.Height)
75+
render(dash, *rows)
7776
return
7877
case <-c:
79-
osc.ShowCursor()
78+
cleanup(*rows)
8079
os.Exit(0)
8180
}
8281
}
@@ -92,8 +91,40 @@ func fatal(a ...interface{}) {
9291
os.Exit(1)
9392
}
9493

95-
func render(dash graph.Dash, width, height int) {
96-
osc.CursorPosition(0, 0)
94+
func prepare(rows int) {
95+
osc.HideCursor()
96+
if rows == 0 {
97+
size, err := osc.Size()
98+
if err != nil {
99+
fatal("Cannot get window size: ", err)
100+
}
101+
rows = size.Row
102+
}
103+
print(strings.Repeat("\n", rows))
104+
osc.CursorMove(osc.Up, rows)
105+
}
106+
107+
func cleanup(rows int) {
108+
osc.ShowCursor()
109+
if rows == 0 {
110+
size, _ := osc.Size()
111+
rows = size.Row
112+
}
113+
osc.CursorMove(osc.Down, rows)
114+
print("\n")
115+
}
116+
117+
func render(dash graph.Dash, rows int) {
118+
size, err := osc.Size()
119+
if err != nil {
120+
fatal("Cannot get window size: ", err)
121+
}
122+
width, height := size.Width, size.Height
123+
if rows > 0 {
124+
height = size.Height / size.Row * rows
125+
} else {
126+
rows = size.Row
127+
}
97128
// Use iTerm2 image display feature.
98129
term := &osc.ImageWriter{}
99130
defer term.Close()

osc/std.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,14 @@ func CursorPosition(row, col int) {
4444
}
4545

4646
// CursorMove moves the cursor n times in the direction d.
47-
func CursorMove(d Direction, n uint) {
47+
func CursorMove(d Direction, n int) {
4848
fmt.Printf("%s%d%s", csi, n, d)
4949
}
50+
51+
func CursorSavePosition() {
52+
print(csi + "s")
53+
}
54+
55+
func CursorRestorePosition() {
56+
print(csi + "u")
57+
}

0 commit comments

Comments
 (0)