Skip to content

Commit 3602d51

Browse files
committed
Cleaner handling of cli error messages
1 parent fe6526b commit 3602d51

File tree

3 files changed

+37
-19
lines changed

3 files changed

+37
-19
lines changed

data/http.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ func (h httpSource) run(url string, interval time.Duration) {
4040
case <-t.C:
4141
h.fetch(url)
4242
case <-h.done:
43+
close(h.c)
4344
return
4445
}
4546
}

main.go

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@ func main() {
4040
} else {
4141
dp = data.FromStdin(*steps)
4242
}
43-
defer dp.Close()
4443
dash := graph.Dash{
4544
Specs: specs,
4645
Data: dp,
@@ -58,11 +57,13 @@ func main() {
5857
c := make(chan os.Signal, 2)
5958
signal.Notify(c, os.Interrupt, syscall.SIGTERM)
6059
i := 0
61-
prepare(*rows)
62-
defer cleanup(*rows)
6360
for {
6461
select {
6562
case <-t.C:
63+
if i == 0 {
64+
prepare(*rows)
65+
defer cleanup(*rows)
66+
}
6667
i++
6768
if i%120 == 0 {
6869
// Clear scrollback to avoid iTerm from eating all the memory.
@@ -72,11 +73,13 @@ func main() {
7273
render(dash, *rows)
7374
osc.CursorRestorePosition()
7475
case <-exit:
75-
render(dash, *rows)
76+
if i == 0 {
77+
render(dash, *rows)
78+
}
7679
return
7780
case <-c:
78-
cleanup(*rows)
79-
os.Exit(0)
81+
dp.Close()
82+
signal.Stop(c)
8083
}
8184
}
8285
}()
@@ -94,11 +97,10 @@ func fatal(a ...interface{}) {
9497
func prepare(rows int) {
9598
osc.HideCursor()
9699
if rows == 0 {
97-
size, err := osc.Size()
98-
if err != nil {
100+
var err error
101+
if rows, err = osc.Rows(); err != nil {
99102
fatal("Cannot get window size: ", err)
100103
}
101-
rows = size.Row
102104
}
103105
print(strings.Repeat("\n", rows))
104106
osc.CursorMove(osc.Up, rows)
@@ -107,8 +109,7 @@ func prepare(rows int) {
107109
func cleanup(rows int) {
108110
osc.ShowCursor()
109111
if rows == 0 {
110-
size, _ := osc.Size()
111-
rows = size.Row
112+
rows, _ = osc.Rows()
112113
}
113114
osc.CursorMove(osc.Down, rows)
114115
print("\n")

osc/iterm2.go

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package osc
33
import (
44
"bytes"
55
"encoding/base64"
6+
"errors"
67
"fmt"
78
"io"
89
"os"
@@ -15,6 +16,9 @@ import (
1516
var ecsi = "\033]"
1617
var st = "\007"
1718

19+
var cellSizeOnce sync.Once
20+
var cellWidth, cellHeight float64
21+
1822
func init() {
1923
if os.Getenv("TERM") == "screen" {
2024
ecsi = "\033Ptmux;\033" + ecsi
@@ -35,26 +39,38 @@ type TermSize struct {
3539
Height int
3640
}
3741

38-
// Size gathers sizing information of the current session's controling terminal.
39-
func Size() (size TermSize, err error) {
40-
size.Col, size.Row, err = terminal.GetSize(1)
41-
if err != nil {
42-
return
43-
}
42+
func initCellSize() {
4443
s, err := terminal.MakeRaw(1)
4544
if err != nil {
4645
return
4746
}
4847
defer terminal.Restore(1, s)
49-
var cellWidth, cellHeight float64
5048
fmt.Fprint(os.Stdout, ecsi+"1337;ReportCellSize"+st)
5149
fileSetReadDeadline(os.Stdout, time.Now().Add(time.Second))
5250
defer fileSetReadDeadline(os.Stdout, time.Time{})
53-
_, err = fmt.Fscanf(os.Stdout, "\033]1337;ReportCellSize=%f;%f\033\\", &cellHeight, &cellWidth)
51+
fmt.Fscanf(os.Stdout, "\033]1337;ReportCellSize=%f;%f\033\\", &cellHeight, &cellWidth)
52+
}
53+
54+
// Size gathers sizing information of the current session's controling terminal.
55+
func Size() (size TermSize, err error) {
56+
size.Col, size.Row, err = terminal.GetSize(1)
57+
if err != nil {
58+
return
59+
}
60+
cellSizeOnce.Do(initCellSize)
61+
if cellWidth+cellHeight == 0 {
62+
err = errors.New("cannot get iTerm2 cell size")
63+
}
5464
size.Width, size.Height = size.Col*int(cellWidth), size.Row*int(cellHeight)
5565
return
5666
}
5767

68+
// Rows returns the number of rows for the controling terminal.
69+
func Rows() (rows int, err error) {
70+
_, rows, err = terminal.GetSize(1)
71+
return
72+
}
73+
5874
// ImageWriter is a writer that write into iTerm2 terminal the PNG data written
5975
// to it.
6076
type ImageWriter struct {

0 commit comments

Comments
 (0)