Skip to content

Commit

Permalink
Better handling for monochrome.
Browse files Browse the repository at this point in the history
We will automatically select an inverse video mode if we cannot
find any colors at all.  This should help some fallbacks.
  • Loading branch information
gdamore committed Jul 30, 2022
1 parent e15e96c commit 896efab
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 6 deletions.
2 changes: 1 addition & 1 deletion _demos/mouse.go
Expand Up @@ -136,7 +136,7 @@ func main() {
keyfmt := "Keys: %s"
pastefmt := "Paste: [%d] %s"
white := tcell.StyleDefault.
Foreground(tcell.ColorWhite).Background(tcell.ColorRed)
Foreground(tcell.ColorMidnightBlue).Background(tcell.ColorLightCoral)

mx, my := -1, -1
ox, oy := -1, -1
Expand Down
27 changes: 22 additions & 5 deletions tscreen.go
Expand Up @@ -647,11 +647,27 @@ func (t *tScreen) encodeRune(r rune, buf []byte) []byte {
return buf
}

func (t *tScreen) sendFgBg(fg Color, bg Color) {
func (t *tScreen) sendFgBg(fg Color, bg Color, attr AttrMask) AttrMask {
ti := t.ti
if ti.Colors == 0 {
return
// foreground vs background, we calculate luminance
// and possibly do a reverse video
if !fg.Valid() {
return attr
}
v, ok := t.colors[fg]
if !ok {
v = FindColor(fg, []Color{ColorBlack, ColorWhite})
t.colors[fg] = v
}
switch v {
case ColorWhite:
return attr
case ColorBlack:
return attr ^ AttrReverse
}
}

if fg == ColorReset || bg == ColorReset {
t.TPuts(ti.ResetFgBg)
}
Expand All @@ -662,7 +678,7 @@ func (t *tScreen) sendFgBg(fg Color, bg Color) {
t.TPuts(ti.TParm(ti.SetFgBgRGB,
int(r1), int(g1), int(b1),
int(r2), int(g2), int(b2)))
return
return attr
}

if fg.IsRGB() && ti.SetFgRGB != "" {
Expand Down Expand Up @@ -709,6 +725,7 @@ func (t *tScreen) sendFgBg(fg Color, bg Color) {
t.TPuts(ti.TParm(ti.SetBg, int(bg&0xff)))
}
}
return attr
}

func (t *tScreen) drawCell(x, y int) int {
Expand Down Expand Up @@ -751,7 +768,7 @@ func (t *tScreen) drawCell(x, y int) int {

t.TPuts(ti.AttrOff)

t.sendFgBg(fg, bg)
attrs = t.sendFgBg(fg, bg, attrs)
if attrs&AttrBold != 0 {
t.TPuts(ti.Bold)
}
Expand Down Expand Up @@ -896,7 +913,7 @@ func (t *tScreen) clearScreen() {
t.TPuts(t.ti.AttrOff)
t.TPuts(t.exitUrl)
fg, bg, _ := t.style.Decompose()
t.sendFgBg(fg, bg)
_ = t.sendFgBg(fg, bg, AttrNone)
t.TPuts(t.ti.Clear)
t.clear = false
}
Expand Down

0 comments on commit 896efab

Please sign in to comment.