replace Attribute by new tcell.Color and tcell.Style

pull/232/head
ѵµσɳɠ 3 years ago
parent c0d75adeb6
commit 9301c71ed6

1
.gitignore vendored

@ -57,6 +57,7 @@ wasm
.appimage_workspace
todo.txt
.vscode
docs/public
deploy_docs.sh

@ -7,6 +7,7 @@ import (
"github.com/cointop-sh/cointop/pkg/gocui"
fcolor "github.com/fatih/color"
"github.com/gdamore/tcell/v2"
"github.com/tomnomnom/xtermcolor"
)
@ -71,13 +72,13 @@ func NewColorscheme(colors ColorschemeColors) *Colorscheme {
}
// BaseFg ...
func (c *Colorscheme) BaseFg() gocui.Attribute {
return c.GocuiFgColor("base")
func (c *Colorscheme) BaseFg() tcell.Color {
return c.TcellFgColor("base")
}
// BaseBg ...
func (c *Colorscheme) BaseBg() gocui.Attribute {
return c.GocuiBgColor("base")
func (c *Colorscheme) BaseBg() tcell.Color {
return c.TcellBgColor("base")
}
// Chart ...
@ -275,13 +276,15 @@ func (c *Colorscheme) Color(name string, a ...interface{}) string {
return c.ToSprintf(name)(a...)
}
func (c *Colorscheme) GocuiFgColor(name string) gocui.Attribute {
func (c *Colorscheme) TcellFgColor(name string) tcell.Color {
// TODO: Parse config value to Tcell.Color and remove gocui.Attribute
var attrs []gocui.Attribute
if v, ok := c.colors[name+"_fg"].(string); ok {
if fg, ok := c.ToGocuiAttr(v); ok {
attrs = append(attrs, fg)
}
}
// TODO: fixme
// if v, ok := c.colors[name+"_bold"].(bool); ok {
// if v {
@ -293,25 +296,27 @@ func (c *Colorscheme) GocuiFgColor(name string) gocui.Attribute {
// attrs = append(attrs, gocui.AttrUnderline)
// }
// }
var combined gocui.Attribute
if len(attrs) > 0 {
var combined gocui.Attribute
for _, v := range attrs {
combined = combined ^ v
}
return combined
}
return gocui.ColorDefault
return convert2TcellColor(combined)
}
func (c *Colorscheme) GocuiBgColor(name string) gocui.Attribute {
func (c *Colorscheme) TcellBgColor(name string) tcell.Color {
// TODO: Parse config value to Tcell.Color and remove gocui.Attribute
var attr gocui.Attribute
if v, ok := c.colors[name+"_bg"].(string); ok {
if bg, ok := c.ToGocuiAttr(v); ok {
return bg
attr = bg
}
}
return gocui.ColorDefault
return convert2TcellColor(attr)
}
func (c *Colorscheme) ToFgAttr(v string) (fcolor.Attribute, bool) {
@ -383,3 +388,13 @@ func HexToAnsi(h string) (uint8, bool) {
}
// gocui can use xterm colors
// convert2TcellColor is temp converter for Attribute to TCell.Color
func convert2TcellColor(a gocui.Attribute) tcell.Color {
c := tcell.PaletteColor(int(a)&0x1ff - 1)
if c != tcell.ColorDefault {
c = tcell.PaletteColor(int(c) & 0xff)
}
return c
}

@ -58,8 +58,8 @@ func (ct *Cointop) layout() error {
} else {
if err := ct.ui.SetView(ct.Views.Marketbar, 0, topOffset-1, maxX, marketbarHeight+1); err != nil {
ct.Views.Marketbar.SetFrame(false)
ct.Views.Marketbar.SetFgColor(ct.colorscheme.GocuiFgColor(ct.Views.Marketbar.Name()))
ct.Views.Marketbar.SetBgColor(ct.colorscheme.GocuiBgColor(ct.Views.Marketbar.Name()))
ct.Views.Marketbar.SetFgColor(ct.colorscheme.TcellFgColor(ct.Views.Marketbar.Name()))
ct.Views.Marketbar.SetBgColor(ct.colorscheme.TcellBgColor(ct.Views.Marketbar.Name()))
go func() {
ct.UpdateMarketbar()
_, found := ct.cache.Get(ct.Views.Marketbar.Name())
@ -92,8 +92,8 @@ func (ct *Cointop) layout() error {
if err := ct.ui.SetView(ct.Views.Chart, 0, chartTopOffset, maxX, topOffset+chartHeight); err != nil {
ct.Views.Chart.Clear()
ct.Views.Chart.SetFrame(false)
ct.Views.Chart.SetFgColor(ct.colorscheme.GocuiFgColor(ct.Views.Chart.Name()))
ct.Views.Chart.SetBgColor(ct.colorscheme.GocuiBgColor(ct.Views.Chart.Name()))
ct.Views.Chart.SetFgColor(ct.colorscheme.TcellFgColor(ct.Views.Chart.Name()))
ct.Views.Chart.SetBgColor(ct.colorscheme.TcellBgColor(ct.Views.Chart.Name()))
go func() {
ct.UpdateChart()
cachekey := ct.CompositeCacheKey("globaldata", "", "", ct.State.selectedChartRange)
@ -124,8 +124,8 @@ func (ct *Cointop) layout() error {
topOffset = topOffset + chartHeight
if err := ct.ui.SetView(ct.Views.TableHeader, tableOffsetX, topOffset-1, maxX, topOffset+1); err != nil {
ct.Views.TableHeader.SetFrame(false)
ct.Views.TableHeader.SetFgColor(ct.colorscheme.GocuiFgColor(ct.Views.TableHeader.Name()))
ct.Views.TableHeader.SetBgColor(ct.colorscheme.GocuiBgColor(ct.Views.TableHeader.Name()))
ct.Views.TableHeader.SetFgColor(ct.colorscheme.TcellFgColor(ct.Views.TableHeader.Name()))
ct.Views.TableHeader.SetBgColor(ct.colorscheme.TcellBgColor(ct.Views.TableHeader.Name()))
go ct.UpdateTableHeader()
}
@ -133,8 +133,8 @@ func (ct *Cointop) layout() error {
if err := ct.ui.SetView(ct.Views.Table, tableOffsetX, topOffset-1, maxX, maxY-statusbarHeight); err != nil {
ct.Views.Table.SetFrame(false)
ct.Views.Table.SetHighlight(true)
ct.Views.Table.SetSelFgColor(ct.colorscheme.GocuiFgColor("table_row_active"))
ct.Views.Table.SetSelBgColor(ct.colorscheme.GocuiBgColor("table_row_active"))
ct.Views.Table.SetSelFgColor(ct.colorscheme.TcellFgColor("table_row_active"))
ct.Views.Table.SetSelBgColor(ct.colorscheme.TcellBgColor("table_row_active"))
_, found := ct.cache.Get("allCoinsSlugMap")
if found {
ct.cache.Delete("allCoinsSlugMap")
@ -149,8 +149,8 @@ func (ct *Cointop) layout() error {
if !ct.State.hideStatusbar {
if err := ct.ui.SetView(ct.Views.Statusbar, 0, maxY-statusbarHeight-1, maxX, maxY); err != nil {
ct.Views.Statusbar.SetFrame(false)
ct.Views.Statusbar.SetFgColor(ct.colorscheme.GocuiFgColor(ct.Views.Statusbar.Name()))
ct.Views.Statusbar.SetBgColor(ct.colorscheme.GocuiBgColor(ct.Views.Statusbar.Name()))
ct.Views.Statusbar.SetFgColor(ct.colorscheme.TcellFgColor(ct.Views.Statusbar.Name()))
ct.Views.Statusbar.SetBgColor(ct.colorscheme.TcellBgColor(ct.Views.Statusbar.Name()))
go ct.UpdateStatusbar("")
}
} else {
@ -166,22 +166,22 @@ func (ct *Cointop) layout() error {
ct.Views.SearchField.SetEditable(true)
ct.Views.SearchField.SetWrap(true)
ct.Views.SearchField.SetFrame(false)
ct.Views.SearchField.SetFgColor(ct.colorscheme.GocuiFgColor("searchbar"))
ct.Views.SearchField.SetBgColor(ct.colorscheme.GocuiBgColor("searchbar"))
ct.Views.SearchField.SetFgColor(ct.colorscheme.TcellFgColor("searchbar"))
ct.Views.SearchField.SetBgColor(ct.colorscheme.TcellBgColor("searchbar"))
}
if err := ct.ui.SetView(ct.Views.Menu, 1, 1, maxX-1, maxY-1); err != nil {
ct.Views.Menu.SetFrame(false)
ct.Views.Menu.SetFgColor(ct.colorscheme.GocuiFgColor("menu"))
ct.Views.Menu.SetBgColor(ct.colorscheme.GocuiBgColor("menu"))
ct.Views.Menu.SetFgColor(ct.colorscheme.TcellFgColor("menu"))
ct.Views.Menu.SetBgColor(ct.colorscheme.TcellBgColor("menu"))
}
if err := ct.ui.SetView(ct.Views.Input, 3, 6, 30, 8); err != nil {
ct.Views.Input.SetFrame(true)
ct.Views.Input.SetEditable(true)
ct.Views.Input.SetWrap(true)
ct.Views.Input.SetFgColor(ct.colorscheme.GocuiFgColor("menu"))
ct.Views.Input.SetBgColor(ct.colorscheme.GocuiBgColor("menu"))
ct.Views.Input.SetFgColor(ct.colorscheme.TcellFgColor("menu"))
ct.Views.Input.SetBgColor(ct.colorscheme.TcellBgColor("menu"))
// run only once on init.
// this bit of code should be at the bottom

@ -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.g.Style(v.FgColor, v.BgColor),
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
BgColor, FgColor tcell.Color
// SelBgColor and SelFgColor allow to configure the background and
// foreground colors of the frame of the current view.
SelBgColor, SelFgColor Attribute
SelBgColor, SelFgColor tcell.Color
// If Highlight is true, Sel{Bg,Fg}Colors will be used to draw the
// frame of the current view.
@ -75,17 +75,15 @@ type Gui struct {
// NewGui returns a new Gui object with a given output mode.
// func NewGui(mode OutputMode) (*Gui, error) {
func NewGui() (*Gui, error) {
g := &Gui{}
//outMode = OutputNormal
// outMode = OutputNormal
if s, e := tcell.NewScreen(); e != nil {
return nil, e
} else if e = s.Init(); e != nil {
return nil, e
} else {
g.screen = s
}
// g.outputMode = mode
@ -97,8 +95,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.BgColor, g.FgColor = tcell.ColorDefault, tcell.ColorDefault
g.SelBgColor, g.SelFgColor = tcell.ColorDefault, tcell.ColorDefault
return g, nil
}
@ -283,7 +281,7 @@ func (g *Gui) CurrentView() *View {
// be a rune or a Key.
// TODO: split into key/mouse bindings?
func (g *Gui) SetKeybinding(viewname string, key tcell.Key, ch rune, mod tcell.ModMask, handler func(*Gui, *View) error) error {
//var kb *eventBinding
// var kb *eventBinding
// k, ch, err := getKey(key)
// if err != nil {
@ -494,19 +492,9 @@ func (g *Gui) fixColor(c tcell.Color) tcell.Color {
return c
}
// 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
func (g *Gui) Style(fg, bg tcell.Color) tcell.Style {
st := tcell.StyleDefault.Foreground(fg).Background(bg)
// TODO: fixme
// if (fg|bg)&AttrBold != 0 {
// st = st.Bold(true)
@ -517,13 +505,14 @@ func (g *Gui) MkStyle(fg, bg Attribute) tcell.Style {
// 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)
st := g.Style(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++ {
@ -547,23 +536,19 @@ func (g *Gui) flush() error {
}
for _, v := range g.views {
if v.Frame {
var fgColor, bgColor Attribute
st := g.Style(v.FgColor, v.BgColor)
if g.Highlight && v == g.currentView {
fgColor = g.SelFgColor
bgColor = g.SelBgColor
} else {
fgColor = g.FgColor
bgColor = g.BgColor
st = g.Style(g.SelFgColor, g.SelBgColor)
}
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
}
}
@ -577,12 +562,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 {
@ -618,7 +602,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 = '+', '+', '+', '+'
@ -629,7 +613,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 {
@ -641,12 +624,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 {
@ -729,7 +711,6 @@ func (g *Gui) onEvent(ev tcell.Event) error {
// GetViewRelativeMousePosition returns the View and relative x/y for the provided mouse event.
func (g *Gui) GetViewRelativeMousePosition(ev tcell.Event) (*View, int, int, error) {
if kbe, ok := ev.(*tcell.EventMouse); ok {
mx, my := kbe.Position()
v, err := g.ViewByPosition(mx, my)

@ -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
BgColor, FgColor tcell.Color
// 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
SelBgColor, SelFgColor tcell.Color // TODO: merge to 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.g.Style(v.FgColor, v.BgColor)
ch = v.Mask
} else if v.Highlight && ry == rcy {
st = v.g.MkStyle(v.SelFgColor, v.SelBgColor)
st = v.g.Style(v.SelFgColor, v.SelBgColor)
}
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.g.Style(v.FgColor, v.BgColor),
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,
}
@ -404,7 +400,7 @@ 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
st := v.g.Style(v.FgColor, v.BgColor)
for x := 0; x < maxX; x++ {
for y := 0; y < maxY; y++ {
v.g.SetRune(v.x0+x+1, v.y0+y+1, ' ', st)

@ -2,6 +2,7 @@ package ui
import (
"github.com/cointop-sh/cointop/pkg/gocui"
"github.com/gdamore/tcell/v2"
)
// UI is the UI view struct
@ -27,12 +28,12 @@ func (ui *UI) GetGocui() *gocui.Gui {
}
// SetFgColor sets the foreground color
func (ui *UI) SetFgColor(fgColor gocui.Attribute) {
func (ui *UI) SetFgColor(fgColor tcell.Color) {
ui.g.FgColor = fgColor
}
// SetBgColor sets the background color
func (ui *UI) SetBgColor(bgColor gocui.Attribute) {
func (ui *UI) SetBgColor(bgColor tcell.Color) {
ui.g.BgColor = bgColor
}

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

Loading…
Cancel
Save