-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
iterm2ui.go
69 lines (62 loc) · 1.45 KB
/
iterm2ui.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
package mkrg
import (
"bytes"
"encoding/base64"
"fmt"
"image"
"image/png"
"os"
"time"
)
type iterm2UI struct {
height, width int
column, maxColumn int
from, until time.Time
img *image.RGBA
}
func newIterm2UI(height, width, maxColumn int, from, until time.Time) *iterm2UI {
return &iterm2UI{height, width, 0, maxColumn, from, until, nil}
}
func (ui *iterm2UI) output(graph graph, ms metricsByName) error {
imgHeight, imgWidth, padding := ui.height*20, ui.width*12, ui.width/5
if ui.column == 0 {
ui.img = image.NewRGBA(image.Rect(0, 0, (imgWidth+padding)*ui.maxColumn-padding, imgHeight+padding*2))
}
printImage(&imageWithMargins{ui.img, padding, (imgWidth + padding) * ui.column}, graph, ms, imgHeight, imgWidth, ui.from, ui.until)
if ui.column == ui.maxColumn-1 {
if err := ui.cleanup(); err != nil {
return err
}
ui.column = 0
} else {
ui.column++
}
return nil
}
func (ui *iterm2UI) cleanup() error {
if ui.column > 0 {
buf := new(bytes.Buffer)
if err := png.Encode(buf, ui.img); err != nil {
return err
}
printOSC()
fmt.Print("1337;File=inline=1;preserveAspectRatio=1;width=100%:")
fmt.Print(base64.StdEncoding.EncodeToString(buf.Bytes()))
printST()
}
return nil
}
func printOSC() {
if os.Getenv("TMUX") != "" {
fmt.Print("\x1bPtmux;\x1b\x1b]")
} else {
fmt.Print("\x1b]")
}
}
func printST() {
if os.Getenv("TMUX") != "" {
fmt.Print("\x07\x1b\\\n")
} else {
fmt.Print("\x07\n")
}
}