5
5
"fmt"
6
6
"os"
7
7
"os/signal"
8
+ "strings"
8
9
"sync"
9
10
"syscall"
10
11
"time"
@@ -19,6 +20,7 @@ func main() {
19
20
interval := flag .Duration ("interval" , time .Second , "When url is provided, defines the interval between fetches." +
20
21
" Note that counter fields are computed based on this interval." )
21
22
steps := flag .Int ("steps" , 100 , "Number of values to plot." )
23
+ rows := flag .Int ("rows" , 0 , "Limits the height of the graph output." )
22
24
flag .Parse ()
23
25
24
26
if os .Getenv ("TERM_PROGRAM" ) != "iTerm.app" {
@@ -51,32 +53,29 @@ func main() {
51
53
defer close (exit )
52
54
go func () {
53
55
defer wg .Done ()
54
- osc .Clear ()
55
- osc .HideCursor ()
56
- defer osc .ShowCursor ()
57
56
t := time .NewTicker (time .Second )
58
57
defer t .Stop ()
59
58
c := make (chan os.Signal , 2 )
60
59
signal .Notify (c , os .Interrupt , syscall .SIGTERM )
61
60
i := 0
61
+ prepare (* rows )
62
+ defer cleanup (* rows )
62
63
for {
63
- size , err := osc .Size ()
64
- if err != nil {
65
- fatal ("Cannot get window size: " , err )
66
- }
67
64
select {
68
65
case <- t .C :
69
66
i ++
70
67
if i % 120 == 0 {
71
68
// Clear scrollback to avoid iTerm from eating all the memory.
72
69
osc .ClearScrollback ()
73
70
}
74
- render (dash , size .Width , size .Height )
71
+ osc .CursorSavePosition ()
72
+ render (dash , * rows )
73
+ osc .CursorRestorePosition ()
75
74
case <- exit :
76
- render (dash , size . Width , size . Height )
75
+ render (dash , * rows )
77
76
return
78
77
case <- c :
79
- osc . ShowCursor ( )
78
+ cleanup ( * rows )
80
79
os .Exit (0 )
81
80
}
82
81
}
@@ -92,8 +91,40 @@ func fatal(a ...interface{}) {
92
91
os .Exit (1 )
93
92
}
94
93
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
+ }
97
128
// Use iTerm2 image display feature.
98
129
term := & osc.ImageWriter {}
99
130
defer term .Close ()
0 commit comments