Skip to content

Commit 7f3fa93

Browse files
committed
Refactor OSC commands
1 parent ab6058d commit 7f3fa93

File tree

3 files changed

+105
-23
lines changed

3 files changed

+105
-23
lines changed

main.go

Lines changed: 16 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,17 @@
11
package main // import "github.com/rs/jplot"
22

33
import (
4-
"bytes"
5-
"encoding/base64"
64
"flag"
75
"fmt"
86
"os"
7+
"os/signal"
98
"sync"
9+
"syscall"
1010
"time"
1111

1212
"github.com/rs/jplot/data"
1313
"github.com/rs/jplot/graph"
14+
"github.com/rs/jplot/osc"
1415
"github.com/rs/jplot/window"
1516
)
1617

@@ -51,9 +52,13 @@ func main() {
5152
defer close(exit)
5253
go func() {
5354
defer wg.Done()
54-
clear()
55+
osc.Clear()
56+
osc.HideCursor()
57+
defer osc.ShowCursor()
5558
t := time.NewTicker(time.Second)
5659
defer t.Stop()
60+
c := make(chan os.Signal, 2)
61+
signal.Notify(c, os.Interrupt, syscall.SIGTERM)
5762
i := 0
5863
for {
5964
width, height, err := window.Size()
@@ -65,12 +70,15 @@ func main() {
6570
i++
6671
if i%120 == 0 {
6772
// Clear scrollback to avoid iTerm from eating all the memory.
68-
cleanup()
73+
osc.ClearScrollback()
6974
}
7075
render(dash, width, height)
7176
case <-exit:
7277
render(dash, width, height)
7378
return
79+
case <-c:
80+
osc.ShowCursor()
81+
os.Exit(0)
7482
}
7583
}
7684
}()
@@ -86,24 +94,9 @@ func fatal(a ...interface{}) {
8694
}
8795

8896
func render(dash graph.Dash, width, height int) {
89-
var b bytes.Buffer
90-
enc := base64.NewEncoder(base64.StdEncoding, &b)
91-
defer enc.Close()
92-
dash.Render(enc, width, height)
93-
reset()
97+
osc.CursorPosition(0, 0)
9498
// Use iTerm2 image display feature.
95-
fmt.Printf("\033]1337;File=preserveAspectRatio=1;inline=1:%s\007", b.Bytes())
96-
}
97-
98-
func clear() {
99-
print("\033\133\110\033\133\062\112") // clear screen
100-
print("\033]1337;CursorShape=1\007") // set cursor to vertical bar
101-
}
102-
103-
func reset() {
104-
print("\033\133\061\073\061\110") // move cursor to 0x0
105-
}
106-
107-
func cleanup() {
108-
print("\033]1337;ClearScrollback\007")
99+
term := &osc.ImageWriter{}
100+
defer term.Close()
101+
dash.Render(term, width, height)
109102
}

osc/iterm2.go

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package osc
2+
3+
import (
4+
"bytes"
5+
"encoding/base64"
6+
"fmt"
7+
"io"
8+
"sync"
9+
)
10+
11+
// ClearScrollback clears iTerm2 scrollback.
12+
func ClearScrollback() {
13+
print("\033]1337;ClearScrollback\007")
14+
}
15+
16+
// ImageWriter is a writer that write into iTerm2 terminal the PNG data written
17+
// to it.
18+
type ImageWriter struct {
19+
Name string
20+
21+
once sync.Once
22+
b66enc io.WriteCloser
23+
buf *bytes.Buffer
24+
}
25+
26+
func (w *ImageWriter) init() {
27+
w.buf = &bytes.Buffer{}
28+
w.b66enc = base64.NewEncoder(base64.StdEncoding, w.buf)
29+
}
30+
31+
func (w *ImageWriter) Write(p []byte) (n int, err error) {
32+
w.once.Do(w.init)
33+
return w.b66enc.Write(p)
34+
}
35+
36+
func (w *ImageWriter) Close() error {
37+
w.once.Do(w.init)
38+
fmt.Printf("\033]1337;File=preserveAspectRatio=1;inline=1:%s\007", w.buf.Bytes())
39+
return w.b66enc.Close()
40+
}

osc/std.go

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package osc
2+
3+
import "fmt"
4+
5+
type Direction string
6+
7+
const (
8+
// Up moves the cursor up.
9+
Up Direction = "A"
10+
// Down moves the cursor down.
11+
Down Direction = "B"
12+
// Forward moves the cursor foward.
13+
Forward Direction = "C"
14+
// Backward moves the cursor backward.
15+
Backward Direction = "D"
16+
// NextLine cursor to beginning of the line next line.
17+
NextLine Direction = "E"
18+
// PreviousLine cursor to beginning of the line previous line.
19+
PreviousLine Direction = "F"
20+
// HorizontalAbsolute the cursor to the specified column.
21+
HorizontalAbsolute Direction = "G"
22+
)
23+
24+
const csi = "\033["
25+
26+
// HideCursor hides cursor.
27+
func HideCursor() {
28+
print(csi + "?25l")
29+
}
30+
31+
// ShowCursor shows cursor.
32+
func ShowCursor() {
33+
print(csi + "?25h")
34+
}
35+
36+
// Clear clears the screen.
37+
func Clear() {
38+
print(csi + "H\033[2J")
39+
}
40+
41+
// CursorPosition moves cursor to row, col.
42+
func CursorPosition(row, col int) {
43+
fmt.Printf("%s%d;%dH", csi, row, col)
44+
}
45+
46+
// CursorMove moves the cursor n times in the direction d.
47+
func CursorMove(d Direction, n uint) {
48+
fmt.Printf("%s%d%s", csi, n, d)
49+
}

0 commit comments

Comments
 (0)