better handling of random encodings

This commit is contained in:
Jesse Duffield 2021-09-27 19:49:55 +10:00
parent 74b42ecad5
commit aa62b56c15
2 changed files with 33 additions and 10 deletions

11
gui.go
View File

@ -147,10 +147,6 @@ type Gui struct {
// match any known sequence, ESC means KeyEsc.
InputEsc bool
// If ASCII is true then use ASCII instead of unicode to draw the
// interface. Using ASCII is more portable.
ASCII bool
// SupportOverlaps is true when we allow for view edges to overlap with other
// view edges
SupportOverlaps bool
@ -754,9 +750,7 @@ func (g *Gui) clear(fg, bg Attribute) (int, int) {
// drawFrameEdges draws the horizontal and vertical edges of a view.
func (g *Gui) drawFrameEdges(v *View, fgColor, bgColor Attribute) error {
runeH, runeV := '─', '│'
if g.ASCII {
runeH, runeV = '-', '|'
} else if len(v.FrameRunes) >= 2 {
if len(v.FrameRunes) >= 2 {
runeH, runeV = v.FrameRunes[0], v.FrameRunes[1]
}
@ -882,9 +876,6 @@ func (g *Gui) drawFrameCorners(v *View, fgColor, bgColor Attribute) error {
runeBL = corner(v, TOP|RIGHT)
runeBR = corner(v, TOP|LEFT)
}
if g.ASCII {
runeTL, runeTR, runeBL, runeBR = '+', '+', '+', '+'
}
corners := []struct {
x, y int

View File

@ -8,6 +8,7 @@ import (
"time"
"github.com/gdamore/tcell/v2"
"github.com/mattn/go-runewidth"
)
// We probably don't want this being a global variable for YOLO for now
@ -20,19 +21,50 @@ type oldStyle struct {
outputMode OutputMode
}
var runeReplacements = map[rune]string{
'┌': "+",
'┐': "+",
'└': "+",
'┘': "+",
'─': "-",
// using a hyphen here actually looks weird.
// We see these characters when in portrait mode
'╶': " ",
'╴': " ",
'├': "+",
'│': "|",
'▼': "v",
'►': ">",
'▲': "^",
'◄': "<",
}
// tcellInit initializes tcell screen for use.
func (g *Gui) tcellInit() error {
runewidth.DefaultCondition.EastAsianWidth = false
tcell.SetEncodingFallback(tcell.EncodingFallbackASCII)
if s, e := tcell.NewScreen(); e != nil {
return e
} else if e = s.Init(); e != nil {
return e
} else {
registerRuneFallbacks(s)
g.screen = s
Screen = s
return nil
}
}
func registerRuneFallbacks(s tcell.Screen) {
for before, after := range runeReplacements {
s.RegisterRuneFallback(before, after)
}
}
// tcellInitSimulation initializes tcell screen for use.
func (g *Gui) tcellInitSimulation() error {
s := tcell.NewSimulationScreen("")