Skip to content

Commit 69dd65d

Browse files
committed
Remove the need of osascript hack to get term size
1 parent 7f3fa93 commit 69dd65d

File tree

7 files changed

+37
-123
lines changed

7 files changed

+37
-123
lines changed

main.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ import (
1212
"github.com/rs/jplot/data"
1313
"github.com/rs/jplot/graph"
1414
"github.com/rs/jplot/osc"
15-
"github.com/rs/jplot/window"
1615
)
1716

1817
func main() {
@@ -61,7 +60,7 @@ func main() {
6160
signal.Notify(c, os.Interrupt, syscall.SIGTERM)
6261
i := 0
6362
for {
64-
width, height, err := window.Size()
63+
size, err := osc.Size()
6564
if err != nil {
6665
fatal("Cannot get window size: ", err)
6766
}
@@ -72,9 +71,9 @@ func main() {
7271
// Clear scrollback to avoid iTerm from eating all the memory.
7372
osc.ClearScrollback()
7473
}
75-
render(dash, width, height)
74+
render(dash, size.Width, size.Height)
7675
case <-exit:
77-
render(dash, width, height)
76+
render(dash, size.Width, size.Height)
7877
return
7978
case <-c:
8079
osc.ShowCursor()

window/go110.go renamed to osc/go110.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// +build go1.10
22

3-
package window
3+
package osc
44

55
import (
66
"os"

osc/iterm2.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,46 @@ import (
55
"encoding/base64"
66
"fmt"
77
"io"
8+
"os"
89
"sync"
10+
"time"
11+
12+
"golang.org/x/crypto/ssh/terminal"
913
)
1014

1115
// ClearScrollback clears iTerm2 scrollback.
1216
func ClearScrollback() {
1317
print("\033]1337;ClearScrollback\007")
1418
}
1519

20+
// TermSize contains sizing information of the terminal.
21+
type TermSize struct {
22+
Row int
23+
Col int
24+
Width int
25+
Height int
26+
}
27+
28+
// Size gathers sizing information of the current session's controling terminal.
29+
func Size() (size TermSize, err error) {
30+
size.Col, size.Row, err = terminal.GetSize(1)
31+
if err != nil {
32+
return
33+
}
34+
s, err := terminal.MakeRaw(1)
35+
if err != nil {
36+
return
37+
}
38+
defer terminal.Restore(1, s)
39+
var cellWidth, cellHeight float64
40+
fmt.Fprint(os.Stdout, "\033]1337;ReportCellSize\033\\")
41+
fileSetReadDeadline(os.Stdout, time.Now().Add(time.Second))
42+
defer fileSetReadDeadline(os.Stdout, time.Time{})
43+
_, err = fmt.Fscanf(os.Stdout, "\033]1337;ReportCellSize=%f;%f\033\\", &cellHeight, &cellWidth)
44+
size.Width, size.Height = size.Col*int(cellWidth), size.Row*int(cellHeight)
45+
return
46+
}
47+
1648
// ImageWriter is a writer that write into iTerm2 terminal the PNG data written
1749
// to it.
1850
type ImageWriter struct {

window/not_go110.go renamed to osc/not_go110.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// +build !go1.10
22

3-
package window
3+
package osc
44

55
import (
66
"os"

window/osa.go

Lines changed: 0 additions & 67 deletions
This file was deleted.

window/osc.go

Lines changed: 0 additions & 29 deletions
This file was deleted.

window/size.go

Lines changed: 0 additions & 21 deletions
This file was deleted.

0 commit comments

Comments
 (0)