Merge fg/bg color into Style in Gui and View

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

@ -5,7 +5,7 @@ import (
"strconv"
"sync"
"github.com/cointop-sh/cointop/pkg/gocui"
"github.com/cointop-sh/cointop/pkg/termbox"
fcolor "github.com/fatih/color"
"github.com/tomnomnom/xtermcolor"
)
@ -50,15 +50,15 @@ var BgColorschemeColorsMap = map[string]fcolor.Attribute{
"yellow": fcolor.BgYellow,
}
var GocuiColorschemeColorsMap = map[string]gocui.Attribute{
"black": gocui.ColorBlack,
"blue": gocui.ColorBlue,
"cyan": gocui.ColorCyan,
"green": gocui.ColorGreen,
"magenta": gocui.ColorMagenta,
"red": gocui.ColorRed,
"white": gocui.ColorWhite,
"yellow": gocui.ColorYellow,
var GocuiColorschemeColorsMap = map[string]termbox.Attribute{
"black": termbox.ColorBlack,
"blue": termbox.ColorBlue,
"cyan": termbox.ColorCyan,
"green": termbox.ColorGreen,
"magenta": termbox.ColorMagenta,
"red": termbox.ColorRed,
"white": termbox.ColorWhite,
"yellow": termbox.ColorYellow,
}
// NewColorscheme ...
@ -71,12 +71,12 @@ func NewColorscheme(colors ColorschemeColors) *Colorscheme {
}
// BaseFg ...
func (c *Colorscheme) BaseFg() gocui.Attribute {
func (c *Colorscheme) BaseFg() termbox.Attribute {
return c.GocuiFgColor("base")
}
// BaseBg ...
func (c *Colorscheme) BaseBg() gocui.Attribute {
func (c *Colorscheme) BaseBg() termbox.Attribute {
return c.GocuiBgColor("base")
}
@ -275,8 +275,8 @@ func (c *Colorscheme) Color(name string, a ...interface{}) string {
return c.ToSprintf(name)(a...)
}
func (c *Colorscheme) GocuiFgColor(name string) gocui.Attribute {
var attrs []gocui.Attribute
func (c *Colorscheme) GocuiFgColor(name string) termbox.Attribute {
var attrs []termbox.Attribute
if v, ok := c.colors[name+"_fg"].(string); ok {
if fg, ok := c.ToGocuiAttr(v); ok {
attrs = append(attrs, fg)
@ -294,24 +294,24 @@ func (c *Colorscheme) GocuiFgColor(name string) gocui.Attribute {
// }
// }
if len(attrs) > 0 {
var combined gocui.Attribute
var combined termbox.Attribute
for _, v := range attrs {
combined = combined ^ v
}
return combined
}
return gocui.ColorDefault
return termbox.ColorDefault
}
func (c *Colorscheme) GocuiBgColor(name string) gocui.Attribute {
func (c *Colorscheme) GocuiBgColor(name string) termbox.Attribute {
if v, ok := c.colors[name+"_bg"].(string); ok {
if bg, ok := c.ToGocuiAttr(v); ok {
return bg
}
}
return gocui.ColorDefault
return termbox.ColorDefault
}
func (c *Colorscheme) ToFgAttr(v string) (fcolor.Attribute, bool) {
@ -349,13 +349,13 @@ func (c *Colorscheme) ToUnderlineAttr(v bool) (fcolor.Attribute, bool) {
}
// ToGocuiAttr converts a color string name to a gocui Attribute type
func (c *Colorscheme) ToGocuiAttr(v string) (gocui.Attribute, bool) {
func (c *Colorscheme) ToGocuiAttr(v string) (termbox.Attribute, bool) {
if attr, ok := GocuiColorschemeColorsMap[v]; ok {
return attr, true
}
if code, ok := HexToAnsi(v); ok {
return gocui.Attribute(code), true
return termbox.Attribute(code), true
}
return 0, false

@ -1,32 +0,0 @@
// Copyright 2014 The gocui Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package gocui
import "github.com/cointop-sh/cointop/pkg/termbox"
// Attribute represents a terminal attribute, like color, font style, etc. They
// can be combined using bitwise OR (|). Note that it is not possible to
// combine multiple color attributes.
type Attribute termbox.Attribute
// Color attributes.
const (
ColorDefault Attribute = Attribute(termbox.ColorDefault)
ColorBlack = Attribute(termbox.ColorBlack)
ColorRed = Attribute(termbox.ColorRed)
ColorGreen = Attribute(termbox.ColorGreen)
ColorYellow = Attribute(termbox.ColorYellow)
ColorBlue = Attribute(termbox.ColorBlue)
ColorMagenta = Attribute(termbox.ColorMagenta)
ColorCyan = Attribute(termbox.ColorCyan)
ColorWhite = Attribute(termbox.ColorWhite)
)
// Text style attributes.
// const (
// AttrBold Attribute = Attribute(termbox.AttrBold)
// AttrUnderline = Attribute(termbox.AttrUnderline)
// AttrReverse = Attribute(termbox.AttrReverse)
// )

@ -269,7 +269,7 @@ 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{
style: v.g.MkStyle(v.FgColor, v.BgColor),
style: v.Style,
chr: ch,
}

@ -44,11 +44,11 @@ type Gui struct {
// BgColor and FgColor allow to configure the background and foreground
// colors of the GUI.
BgColor, FgColor Attribute
Style tcell.Style
// SelBgColor and SelFgColor allow to configure the background and
// foreground colors of the frame of the current view.
SelBgColor, SelFgColor Attribute
SelStyle tcell.Style
// If Highlight is true, Sel{Bg,Fg}Colors will be used to draw the
// frame of the current view.
@ -97,8 +97,8 @@ func NewGui() (*Gui, error) {
g.maxX, g.maxY = g.screen.Size()
g.BgColor, g.FgColor = ColorDefault, ColorDefault
g.SelBgColor, g.SelFgColor = ColorDefault, ColorDefault
g.Style = tcell.StyleDefault
g.SelStyle = tcell.StyleDefault
return g, nil
}
@ -180,8 +180,8 @@ func (g *Gui) SetView(name string, x0, y0, x1, y1 int) (*View, error) {
}
v := newView(name, x0, y0, x1, y1, g)
v.BgColor, v.FgColor = g.BgColor, g.FgColor
v.SelBgColor, v.SelFgColor = g.SelBgColor, g.SelFgColor
v.Style = g.Style
v.SelStyle = g.SelStyle
g.views = append(g.views, v)
return v, ErrUnknownView
}
@ -473,69 +473,13 @@ func (g *Gui) handleEvent(ev tcell.Event) error {
}
}
// TODO: delete termbox compat
func (g *Gui) fixColor(c tcell.Color) tcell.Color {
if c == tcell.ColorDefault {
return c
}
c = tcell.PaletteColor(int(c) & 0xff)
// switch g.outputMode {
// case OutputNormal:
// c = tcell.PaletteColor(int(c) & 0xf)
// case Output256:
// c = tcell.PaletteColor(int(c) & 0xff)
// case Output216:
// c = tcell.PaletteColor(int(c)%216 + 16)
// case OutputGrayscale:
// c %= tcell.PaletteColor(int(c)%24 + 232)
// default:
// c = tcell.ColorDefault
// }
return c
}
func (g *Gui) MkColor(color Attribute) tcell.Color {
if color == ColorDefault {
return tcell.ColorDefault
} else {
return g.fixColor(tcell.PaletteColor(int(color)&0x1ff - 1))
}
}
// TODO: delete termbox compat
func (g *Gui) MkStyle(fg, bg Attribute) tcell.Style {
st := tcell.StyleDefault
if fg != ColorDefault {
f := tcell.PaletteColor(int(fg)&0x1ff - 1)
f = g.fixColor(f)
st = st.Foreground(f)
}
if bg != ColorDefault {
b := tcell.PaletteColor(int(bg)&0x1ff - 1)
b = g.fixColor(b)
st = st.Background(b)
}
// 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
}
// flush updates the gui, re-drawing frames and buffers.
func (g *Gui) flush() error {
// termbox.Clear(termbox.Attribute(g.FgColor), termbox.Attribute(g.BgColor))
st := g.MkStyle(g.FgColor, g.BgColor)
w, h := g.screen.Size() // TODO: merge with maxX, maxY below
for row := 0; row < h; row++ {
for col := 0; col < w; col++ {
g.screen.SetContent(col, row, ' ', nil, st)
g.screen.SetContent(col, row, ' ', nil, g.Style)
}
}
@ -555,23 +499,20 @@ func (g *Gui) flush() error {
}
for _, v := range g.views {
if v.Frame {
var fgColor, bgColor Attribute
// var fgColor, bgColor Attribute
st := g.Style
if g.Highlight && v == g.currentView {
fgColor = g.SelFgColor
bgColor = g.SelBgColor
} else {
fgColor = g.FgColor
bgColor = g.BgColor
st = g.SelStyle
}
if err := g.drawFrameEdges(v, fgColor, bgColor); err != nil {
if err := g.drawFrameEdges(v, st); err != nil {
return err
}
if err := g.drawFrameCorners(v, fgColor, bgColor); err != nil {
if err := g.drawFrameCorners(v, st); err != nil {
return err
}
if v.Title != "" {
if err := g.drawTitle(v, fgColor, bgColor); err != nil {
if err := g.drawTitle(v, st); err != nil {
return err
}
}
@ -585,12 +526,11 @@ func (g *Gui) flush() error {
}
// drawFrameEdges draws the horizontal and vertical edges of a view.
func (g *Gui) drawFrameEdges(v *View, fgColor, bgColor Attribute) error {
func (g *Gui) drawFrameEdges(v *View, st tcell.Style) error {
runeH, runeV := '─', '│'
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 {
@ -626,7 +566,7 @@ func (g *Gui) drawFrameEdges(v *View, fgColor, bgColor Attribute) error {
}
// drawFrameCorners draws the corners of the view.
func (g *Gui) drawFrameCorners(v *View, fgColor, bgColor Attribute) error {
func (g *Gui) drawFrameCorners(v *View, st tcell.Style) error {
runeTL, runeTR, runeBL, runeBR := '┌', '┐', '└', '┘'
if g.ASCII {
runeTL, runeTR, runeBL, runeBR = '+', '+', '+', '+'
@ -637,7 +577,6 @@ 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, st); err != nil {
@ -649,12 +588,11 @@ func (g *Gui) drawFrameCorners(v *View, fgColor, bgColor Attribute) error {
}
// drawTitle draws the title of the view.
func (g *Gui) drawTitle(v *View, fgColor, bgColor Attribute) error {
func (g *Gui) drawTitle(v *View, st tcell.Style) error {
if v.y0 < 0 || v.y0 >= g.maxY {
return nil
}
st := g.MkStyle(fgColor, bgColor) // TODO: push up
for i, ch := range v.Title {
x := v.x0 + i + 2
if x < 0 {

@ -31,11 +31,11 @@ type View struct {
// BgColor and FgColor allow to configure the background and foreground
// colors of the View.
BgColor, FgColor Attribute // TODO: merge to tcell.Style
Style 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 // TODO: merge to tcell.Style
SelStyle tcell.Style
// If Editable is true, keystrokes will be added to the view's internal
// buffer at the cursor position.
@ -150,10 +150,10 @@ func (v *View) setRune(x, y int, ch rune, st tcell.Style) error {
}
if v.Mask != 0 {
st = v.g.MkStyle(v.FgColor, v.BgColor)
st = v.Style
ch = v.Mask
} else if v.Highlight && ry == rcy {
st = v.g.MkStyle(v.SelFgColor, v.SelBgColor)
st = v.SelStyle
}
v.g.SetRune(v.x0+x+1, v.y0+y+1, ch, st)
@ -242,9 +242,7 @@ func (v *View) parseInput(ch rune) []cell {
if err != nil {
for _, r := range v.ei.runes() {
c := cell{
// fgColor: v.FgColor,
// bgColor: v.BgColor,
style: v.g.MkStyle(v.FgColor, v.BgColor),
style: v.Style,
chr: r,
}
cells = append(cells, c)
@ -255,8 +253,6 @@ func (v *View) parseInput(ch rune) []cell {
return nil
}
c := cell{
// fgColor: v.ei.curFgColor,
// bgColor: v.ei.curBgColor,
style: v.ei.curStyle,
chr: ch,
}
@ -356,11 +352,12 @@ func (v *View) draw() error {
st := c.style
fgColor, bgColor, _ := c.style.Decompose()
vfgColor, vbgColor, _ := v.Style.Decompose()
if fgColor == tcell.ColorDefault {
st = st.Foreground(v.g.MkColor(v.FgColor))
st = st.Foreground(vfgColor)
}
if bgColor == tcell.ColorDefault {
st = st.Background(v.g.MkColor(v.BgColor))
st = st.Background(vbgColor)
}
if err := v.setRune(x, y, c.chr, st); err != nil {
return err
@ -412,10 +409,9 @@ 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, ' ', st)
v.g.SetRune(v.x0+x+1, v.y0+y+1, ' ', v.Style)
}
}
}

@ -16,13 +16,9 @@
// the github.com/nsf/termbox package.
package termbox
// Ugly globals
// var screen tcell.Screen
// var outMode OutputMode
// func SetScreen(s tcell.Screen) {
// screen = s
// }
import (
"github.com/gdamore/tcell/v2"
)
// Attribute affects the presentation of characters, such as color, boldness,
// and so forth.
@ -41,63 +37,57 @@ const (
ColorWhite
)
// Other attributes.
// const (
// AttrBold Attribute = 1 << (9 + iota)
// AttrUnderline
// AttrReverse
// )
// Clear clears the screen with the given attributes.
// func Clear(fg, bg Attribute) {
// st := MkStyle(fg, bg)
// w, h := screen.Size()
// for row := 0; row < h; row++ {
// for col := 0; col < w; col++ {
// screen.SetContent(col, row, ' ', nil, st)
// }
// }
// }
// OutputMode represents an output mode, which determines how colors
// are used. See the termbox documentation for an explanation.
// type OutputMode int
// // OutputMode values.
// const (
// OutputCurrent OutputMode = iota
// OutputNormal
// Output256
// Output216
// OutputGrayscale
// )
// SetOutputMode is used to set the color palette used.
// func SetOutputMode(mode OutputMode) OutputMode {
// if screen.Colors() < 256 {
// mode = OutputNormal
// }
// switch mode {
// case OutputCurrent:
// return outMode
// case OutputNormal, Output256, Output216, OutputGrayscale:
// outMode = mode
// return mode
// default:
// return outMode
// }
// }
// TODO: remove compatability layer
func FixColor(c tcell.Color) tcell.Color {
if c == tcell.ColorDefault {
return c
}
c = tcell.PaletteColor(int(c) & 0xff)
// switch g.outputMode {
// case OutputNormal:
// c = tcell.PaletteColor(int(c) & 0xf)
// case Output256:
// c = tcell.PaletteColor(int(c) & 0xff)
// case Output216:
// c = tcell.PaletteColor(int(c)%216 + 16)
// case OutputGrayscale:
// c %= tcell.PaletteColor(int(c)%24 + 232)
// default:
// c = tcell.ColorDefault
// }
return c
}
// scaledColor returns a Color that is proportional to the x/y coordinates
// func scaledColor(x, y int) tcell.Color {
// w, h := screen.Size()
// blu := int32(255 * float64(x) / float64(w))
// grn := int32(255 * float64(y) / float64(h))
// red := int32(200)
// return tcell.NewRGBColor(red, grn, blu)
// }
// TODO: remove compatability layer
func MkColor(color Attribute) tcell.Color {
if color == ColorDefault {
return tcell.ColorDefault
} else {
return FixColor(tcell.PaletteColor(int(color)&0x1ff - 1))
}
}
// PollEvent blocks until an event is ready, and then returns it.
// func PollEvent() tcell.Event {
// return screen.PollEvent()
// }
func MkStyle(fg, bg Attribute) tcell.Style {
st := tcell.StyleDefault
if fg != ColorDefault {
// f := tcell.PaletteColor(int(fg)&0x1ff - 1)
// f = g.fixColor(f)
st = st.Foreground(MkColor(fg))
}
if bg != ColorDefault {
// b := tcell.PaletteColor(int(bg)&0x1ff - 1)
// b = g.fixColor(b)
st = st.Background(MkColor(bg))
}
// 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
}

@ -8,8 +8,6 @@ import (
"regexp"
"strings"
tm "github.com/cointop-sh/cointop/pkg/termbox"
rw "github.com/mattn/go-runewidth"
)
@ -48,9 +46,9 @@ var (
/* ----------------------- End ----------------------------- */
func toTmAttr(x Attribute) tm.Attribute {
return tm.Attribute(x)
}
// func toTmAttr(x Attribute) tm.Attribute {
// return tm.Attribute(x)
// }
func str2runes(s string) []rune {
return []rune(s)

@ -2,6 +2,7 @@ package ui
import (
"github.com/cointop-sh/cointop/pkg/gocui"
"github.com/cointop-sh/cointop/pkg/termbox"
)
// UI is the UI view struct
@ -27,13 +28,13 @@ func (ui *UI) GetGocui() *gocui.Gui {
}
// SetFgColor sets the foreground color
func (ui *UI) SetFgColor(fgColor gocui.Attribute) {
ui.g.FgColor = fgColor
func (ui *UI) SetFgColor(fgColor termbox.Attribute) {
ui.g.Style = ui.g.Style.Foreground(termbox.MkColor(fgColor))
}
// SetBgColor sets the background color
func (ui *UI) SetBgColor(bgColor gocui.Attribute) {
ui.g.BgColor = bgColor
func (ui *UI) SetBgColor(bgColor termbox.Attribute) {
ui.g.Style = ui.g.Style.Background(termbox.MkColor(bgColor))
}
// SetInputEsc enables the escape key

@ -4,6 +4,7 @@ import (
"fmt"
"github.com/cointop-sh/cointop/pkg/gocui"
"github.com/cointop-sh/cointop/pkg/termbox"
)
// IView is the view interface
@ -214,30 +215,31 @@ func (view *View) SetWrap(enabled bool) error {
}
// SetFgColor sets the foreground color
func (view *View) SetFgColor(color gocui.Attribute) {
func (view *View) SetFgColor(color termbox.Attribute) {
if view.HasBacking() {
view.backing.FgColor = color
view.backing.Style = view.backing.Style.Foreground(termbox.MkColor(color))
}
}
// SetBgColor sets the background color
func (view *View) SetBgColor(color gocui.Attribute) {
func (view *View) SetBgColor(color termbox.Attribute) {
if view.HasBacking() {
view.backing.BgColor = color
// view.backing.BgColor = color
view.backing.Style = view.backing.Style.Background(termbox.MkColor(color))
}
}
// SetSelFgColor sets the foreground color for selection
func (view *View) SetSelFgColor(color gocui.Attribute) {
func (view *View) SetSelFgColor(color termbox.Attribute) {
if view.HasBacking() {
view.backing.SelFgColor = color
view.backing.SelStyle = view.backing.SelStyle.Foreground(termbox.MkColor(color))
}
}
// SetSelBgColor sets the background color for selection
func (view *View) SetSelBgColor(color gocui.Attribute) {
func (view *View) SetSelBgColor(color termbox.Attribute) {
if view.HasBacking() {
view.backing.SelBgColor = color
view.backing.SelStyle = view.backing.SelStyle.Background(termbox.MkColor(color))
}
}

Loading…
Cancel
Save