Skip to content

Commit 2cb8b79

Browse files
improve compatibility with Tmux
This fixes: #19
1 parent 8071546 commit 2cb8b79

File tree

2 files changed

+38
-0
lines changed

2 files changed

+38
-0
lines changed

graphic/display.go

+7
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,13 @@ func (d *Display) Init(sampleRate float64, sampleSize int) error {
115115

116116
d.styleBuffer = make([]termbox.Attribute, 4096)
117117

118+
// Prevent crash on Tmux.
119+
prevState, err := normalizeTerminal()
120+
if err != nil {
121+
return err
122+
}
123+
defer prevState()
124+
118125
if err := termbox.Init(); err != nil {
119126
return err
120127
}

graphic/terminal.go

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package graphic
2+
3+
import (
4+
"os"
5+
"strings"
6+
)
7+
8+
// normalizeTerminal looks for incompatibilities in the terminal configuration
9+
// with the underlying rendering libraries (Termbox) and makes some adjustments
10+
// to avoid problems.
11+
//
12+
// Returns a function that allows you to restore the terminal configuration to its original state.
13+
func normalizeTerminal() (func(), error) {
14+
prevTERMINFO := os.Getenv("TERMINFO")
15+
16+
if strings.HasPrefix(os.Getenv("TERM"), "tmux") {
17+
// Some combinations of TERMINFO with TERM in some Tmux value
18+
// will cause Termbox to fail.
19+
if err := os.Unsetenv("TERMINFO"); err != nil {
20+
return nil, err
21+
}
22+
}
23+
24+
restore := func() {
25+
if err := os.Setenv("TERMINFO", prevTERMINFO); err != nil {
26+
panic(err)
27+
}
28+
}
29+
30+
return restore, nil
31+
}

0 commit comments

Comments
 (0)