A bunch more replacing fg/bg color with tcell.Style

pull/232/head
Simon Roberts 3 years ago
parent 00cf22ad8b
commit c0d75adeb6
No known key found for this signature in database
GPG Key ID: 0F30F99E6B771FD4

@ -282,16 +282,17 @@ func (c *Colorscheme) GocuiFgColor(name string) gocui.Attribute {
attrs = append(attrs, fg)
}
}
if v, ok := c.colors[name+"_bold"].(bool); ok {
if v {
attrs = append(attrs, gocui.AttrBold)
}
}
if v, ok := c.colors[name+"_underline"].(bool); ok {
if v {
attrs = append(attrs, gocui.AttrUnderline)
}
}
// TODO: fixme
// if v, ok := c.colors[name+"_bold"].(bool); ok {
// if v {
// attrs = append(attrs, gocui.AttrBold)
// }
// }
// if v, ok := c.colors[name+"_underline"].(bool); ok {
// if v {
// attrs = append(attrs, gocui.AttrUnderline)
// }
// }
if len(attrs) > 0 {
var combined gocui.Attribute
for _, v := range attrs {

@ -25,8 +25,8 @@ const (
)
// Text style attributes.
const (
AttrBold Attribute = Attribute(termbox.AttrBold)
AttrUnderline = Attribute(termbox.AttrUnderline)
AttrReverse = Attribute(termbox.AttrReverse)
)
// const (
// AttrBold Attribute = Attribute(termbox.AttrBold)
// AttrUnderline = Attribute(termbox.AttrUnderline)
// AttrReverse = Attribute(termbox.AttrReverse)
// )

@ -269,9 +269,8 @@ func (v *View) writeRune(x, y int, ch rune) error {
copy(v.lines[y][x+1:], v.lines[y][x:])
}
v.lines[y][x] = cell{
fgColor: v.FgColor,
bgColor: v.BgColor,
chr: ch,
style: v.g.MkStyle(v.FgColor, v.BgColor),
chr: ch,
}
return nil

@ -7,13 +7,15 @@ package gocui
import (
"errors"
"strconv"
"github.com/gdamore/tcell/v2"
)
type escapeInterpreter struct {
state escapeState
curch rune
csiParam []string
curFgColor, curBgColor Attribute
state escapeState
curch rune
csiParam []string
curStyle tcell.Style
// mode OutputMode
}
@ -56,9 +58,8 @@ func (ei *escapeInterpreter) runes() []rune {
// terminal escape sequences.
func newEscapeInterpreter() *escapeInterpreter {
ei := &escapeInterpreter{
state: stateNone,
curFgColor: ColorDefault,
curBgColor: ColorDefault,
state: stateNone,
curStyle: tcell.StyleDefault,
// mode: mode,
}
return ei
@ -67,8 +68,7 @@ func newEscapeInterpreter() *escapeInterpreter {
// reset sets the escapeInterpreter in initial state.
func (ei *escapeInterpreter) reset() {
ei.state = stateNone
ei.curFgColor = ColorDefault
ei.curBgColor = ColorDefault
ei.curStyle = tcell.StyleDefault
ei.csiParam = nil
}
@ -150,24 +150,25 @@ func (ei *escapeInterpreter) outputNormal() error {
return errCSIParseError
}
// see https://en.wikipedia.org/wiki/ANSI_escape_code#Colors
// see https://en.wikipedia.org/wiki/ANSI_escape_code#SGR_(Select_Graphic_Rendition)_parameters
switch {
case p >= 30 && p <= 37:
ei.curFgColor = Attribute(p - 30 + 1)
ei.curStyle = ei.curStyle.Foreground(tcell.PaletteColor(p - 30))
case p == 39:
ei.curFgColor = ColorDefault
ei.curStyle = ei.curStyle.Foreground(tcell.ColorDefault)
case p >= 40 && p <= 47:
ei.curBgColor = Attribute(p - 40 + 1)
ei.curStyle = ei.curStyle.Background(tcell.PaletteColor(p - 40))
case p == 49:
ei.curBgColor = ColorDefault
ei.curStyle = ei.curStyle.Background(tcell.ColorDefault)
case p == 1:
ei.curFgColor |= AttrBold
ei.curStyle = ei.curStyle.Bold(true)
case p == 4:
ei.curFgColor |= AttrUnderline
ei.curStyle = ei.curStyle.Underline(true)
case p == 7:
ei.curFgColor |= AttrReverse
ei.curStyle = ei.curStyle.Reverse(true)
case p == 0:
ei.curFgColor = ColorDefault
ei.curBgColor = ColorDefault
ei.curStyle = tcell.StyleDefault
}
}
@ -203,7 +204,7 @@ func (ei *escapeInterpreter) output256() error {
switch fgbg {
case 38:
ei.curFgColor = Attribute(color + 1)
ei.curStyle = ei.curStyle.Foreground(tcell.PaletteColor(color + 1))
for _, param := range ei.csiParam[3:] {
p, err := strconv.Atoi(param)
@ -213,15 +214,15 @@ func (ei *escapeInterpreter) output256() error {
switch {
case p == 1:
ei.curFgColor |= AttrBold
ei.curStyle = ei.curStyle.Bold(true)
case p == 4:
ei.curFgColor |= AttrUnderline
ei.curStyle = ei.curStyle.Underline(true)
case p == 7:
ei.curFgColor |= AttrReverse
ei.curStyle = ei.curStyle.Reverse(true)
}
}
case 48:
ei.curBgColor = Attribute(color + 1)
ei.curStyle = ei.curStyle.Background(tcell.PaletteColor(color + 1))
default:
return errCSIParseError
}

@ -115,53 +115,34 @@ func (g *Gui) Size() (x, y int) {
}
// temporary kludge for the pretty
func (g *Gui) prettyColor(x, y int, fgColor, bgColor Attribute) tcell.Style {
st := g.MkStyle(fgColor, bgColor)
fg, bg, _ := st.Decompose()
// log.Debugf("XXX bg=%s", bg)
w, h := g.screen.Size()
func (g *Gui) prettyColor(x, y int, st tcell.Style) tcell.Style {
if true {
w, h := g.screen.Size()
// dark blue gradient background
red := int32(0)
grn := int32(0)
blu := int32(50 * float64(y) / float64(h))
bg = tcell.NewRGBColor(red, grn, blu)
st = st.Background(bg)
}
// if bg == 0x100000006 {
// bg = tcell.ColorRed
// st = st.Background(bg)
// }
st = st.Background(tcell.NewRGBColor(red, grn, blu))
if true {
red := int32(200)
grn := int32(255 * float64(y) / float64(h))
blu := int32(255 * float64(x) / float64(w))
fg = tcell.NewRGBColor(red, grn, blu)
st = st.Foreground(fg)
// two-axis green-blue gradient
red = int32(200)
grn = int32(255 * float64(y) / float64(h))
blu = int32(255 * float64(x) / float64(w))
st = st.Foreground(tcell.NewRGBColor(red, grn, blu))
}
// st := tcell.StyleDefault
// return st.Foreground(fg).Background(bg).Attributes(attr)
return st
}
// SetRune writes a rune at the given point, relative to the top-left
// corner of the terminal. It checks if the position is valid and applies
// the given colors.
func (g *Gui) SetRune(x, y int, ch rune, fgColor, bgColor Attribute) error {
func (g *Gui) SetRune(x, y int, ch rune, st tcell.Style) error {
if x < 0 || y < 0 || x >= g.maxX || y >= g.maxY {
return errors.New("invalid point")
}
st := g.MkStyle(fgColor, bgColor)
if bgColor == ColorBlack {
st = g.prettyColor(x, y, fgColor, bgColor)
}
return g.SetRuneNew(x, y, ch, st)
}
func (g *Gui) SetRuneNew(x, y int, ch rune, st tcell.Style) error {
// temporary kludge for the pretty
// st = g.prettyColor(x, y, st)
g.screen.SetContent(x, y, ch, nil, st)
return nil
}
@ -526,15 +507,16 @@ func (g *Gui) MkStyle(fg, bg Attribute) tcell.Style {
b = g.fixColor(b)
st = st.Background(b)
}
if (fg|bg)&AttrBold != 0 {
st = st.Bold(true)
}
if (fg|bg)&AttrUnderline != 0 {
st = st.Underline(true)
}
if (fg|bg)&AttrReverse != 0 {
st = st.Reverse(true)
}
// TODO: fixme
// if (fg|bg)&AttrBold != 0 {
// st = st.Bold(true)
// }
// if (fg|bg)&AttrUnderline != 0 {
// st = st.Underline(true)
// }
// if (fg|bg)&AttrReverse != 0 {
// st = st.Reverse(true)
// }
return st
}
@ -600,18 +582,19 @@ func (g *Gui) drawFrameEdges(v *View, fgColor, bgColor Attribute) error {
if g.ASCII {
runeH, runeV = '-', '|'
}
st := g.MkStyle(fgColor, bgColor) // TODO: push up
for x := v.x0 + 1; x < v.x1 && x < g.maxX; x++ {
if x < 0 {
continue
}
if v.y0 > -1 && v.y0 < g.maxY {
if err := g.SetRune(x, v.y0, runeH, fgColor, bgColor); err != nil {
if err := g.SetRune(x, v.y0, runeH, st); err != nil {
return err
}
}
if v.y1 > -1 && v.y1 < g.maxY {
if err := g.SetRune(x, v.y1, runeH, fgColor, bgColor); err != nil {
if err := g.SetRune(x, v.y1, runeH, st); err != nil {
return err
}
}
@ -621,12 +604,12 @@ func (g *Gui) drawFrameEdges(v *View, fgColor, bgColor Attribute) error {
continue
}
if v.x0 > -1 && v.x0 < g.maxX {
if err := g.SetRune(v.x0, y, runeV, fgColor, bgColor); err != nil {
if err := g.SetRune(v.x0, y, runeV, st); err != nil {
return err
}
}
if v.x1 > -1 && v.x1 < g.maxX {
if err := g.SetRune(v.x1, y, runeV, fgColor, bgColor); err != nil {
if err := g.SetRune(v.x1, y, runeV, st); err != nil {
return err
}
}
@ -646,9 +629,10 @@ func (g *Gui) drawFrameCorners(v *View, fgColor, bgColor Attribute) error {
ch rune
}{{v.x0, v.y0, runeTL}, {v.x1, v.y0, runeTR}, {v.x0, v.y1, runeBL}, {v.x1, v.y1, runeBR}}
st := g.MkStyle(fgColor, bgColor) // TODO: push up
for _, c := range corners {
if c.x >= 0 && c.y >= 0 && c.x < g.maxX && c.y < g.maxY {
if err := g.SetRune(c.x, c.y, c.ch, fgColor, bgColor); err != nil {
if err := g.SetRune(c.x, c.y, c.ch, st); err != nil {
return err
}
}
@ -662,6 +646,7 @@ func (g *Gui) drawTitle(v *View, fgColor, bgColor Attribute) error {
return nil
}
st := g.MkStyle(fgColor, bgColor) // TODO: push up
for i, ch := range v.Title {
x := v.x0 + i + 2
if x < 0 {
@ -669,7 +654,7 @@ func (g *Gui) drawTitle(v *View, fgColor, bgColor Attribute) error {
} else if x > v.x1-2 || x >= g.maxX {
break
}
if err := g.SetRune(x, v.y0, ch, fgColor, bgColor); err != nil {
if err := g.SetRune(x, v.y0, ch, st); err != nil {
return err
}
}

@ -9,6 +9,8 @@ import (
"errors"
"io"
"strings"
"github.com/gdamore/tcell/v2"
)
// A View is a window. It maintains its own internal buffer and cursor
@ -29,11 +31,11 @@ type View struct {
// BgColor and FgColor allow to configure the background and foreground
// colors of the View.
BgColor, FgColor Attribute
BgColor, FgColor Attribute // TODO: merge to tcell.Style
// SelBgColor and SelFgColor are used to configure the background and
// foreground colors of the selected line, when it is highlighted.
SelBgColor, SelFgColor Attribute
SelBgColor, SelFgColor Attribute // TODO: merge to tcell.Style
// If Editable is true, keystrokes will be added to the view's internal
// buffer at the cursor position.
@ -80,8 +82,9 @@ type viewLine struct {
}
type cell struct {
chr rune
bgColor, fgColor Attribute
chr rune
// bgColor, fgColor Attribute
style tcell.Style
}
type lineType []cell
@ -125,7 +128,7 @@ func (v *View) Name() string {
// setRune sets a rune at the given point relative to the view. It applies the
// specified colors, taking into account if the cell must be highlighted. Also,
// it checks if the position is valid.
func (v *View) setRune(x, y int, ch rune, fgColor, bgColor Attribute) error {
func (v *View) setRune(x, y int, ch rune, st tcell.Style) error {
maxX, maxY := v.Size()
if x < 0 || x >= maxX || y < 0 || y >= maxY {
return errors.New("invalid point")
@ -147,15 +150,13 @@ func (v *View) setRune(x, y int, ch rune, fgColor, bgColor Attribute) error {
}
if v.Mask != 0 {
fgColor = v.FgColor
bgColor = v.BgColor
st = v.g.MkStyle(v.FgColor, v.BgColor)
ch = v.Mask
} else if v.Highlight && ry == rcy {
fgColor = v.SelFgColor
bgColor = v.SelBgColor
st = v.g.MkStyle(v.SelFgColor, v.SelBgColor)
}
v.g.SetRune(v.x0+x+1, v.y0+y+1, ch, fgColor, bgColor)
v.g.SetRune(v.x0+x+1, v.y0+y+1, ch, st)
return nil
}
@ -241,9 +242,10 @@ func (v *View) parseInput(ch rune) []cell {
if err != nil {
for _, r := range v.ei.runes() {
c := cell{
fgColor: v.FgColor,
bgColor: v.BgColor,
chr: r,
// fgColor: v.FgColor,
// bgColor: v.BgColor,
style: v.g.MkStyle(v.FgColor, v.BgColor),
chr: r,
}
cells = append(cells, c)
}
@ -253,9 +255,10 @@ func (v *View) parseInput(ch rune) []cell {
return nil
}
c := cell{
fgColor: v.ei.curFgColor,
bgColor: v.ei.curBgColor,
chr: ch,
// fgColor: v.ei.curFgColor,
// bgColor: v.ei.curBgColor,
style: v.ei.curStyle,
chr: ch,
}
cells = append(cells, c)
}
@ -342,16 +345,16 @@ func (v *View) draw() error {
break
}
fgColor := c.fgColor
if fgColor == ColorDefault {
fgColor = v.FgColor
}
bgColor := c.bgColor
if bgColor == ColorDefault {
bgColor = v.BgColor
}
// fgColor := c.fgColor
// if fgColor == ColorDefault {
// fgColor = v.FgColor
// }
// bgColor := c.bgColor
// if bgColor == ColorDefault {
// bgColor = v.BgColor
// }
if err := v.setRune(x, y, c.chr, fgColor, bgColor); err != nil {
if err := v.setRune(x, y, c.chr, c.style); err != nil {
return err
}
x++
@ -401,9 +404,10 @@ func (v *View) Clear() {
// clearRunes erases all the cells in the view.
func (v *View) clearRunes() {
maxX, maxY := v.Size()
st := v.g.MkStyle(v.FgColor, v.BgColor) // TODO: push up
for x := 0; x < maxX; x++ {
for y := 0; y < maxY; y++ {
v.g.SetRune(v.x0+x+1, v.y0+y+1, ' ', v.FgColor, v.BgColor)
v.g.SetRune(v.x0+x+1, v.y0+y+1, ' ', st)
}
}
}

@ -42,11 +42,11 @@ const (
)
// Other attributes.
const (
AttrBold Attribute = 1 << (9 + iota)
AttrUnderline
AttrReverse
)
// const (
// AttrBold Attribute = 1 << (9 + iota)
// AttrUnderline
// AttrReverse
// )
// Clear clears the screen with the given attributes.
// func Clear(fg, bg Attribute) {

Loading…
Cancel
Save