Start getting rid of OutputMode. Move color-related compat into gui.go.

pull/232/head
Simon Roberts 3 years ago
parent 093e4e699f
commit 884159eab5
No known key found for this signature in database
GPG Key ID: 0F30F99E6B771FD4

@ -14,7 +14,7 @@ type escapeInterpreter struct {
curch rune
csiParam []string
curFgColor, curBgColor Attribute
mode OutputMode
// mode OutputMode
}
type escapeState int
@ -54,12 +54,12 @@ func (ei *escapeInterpreter) runes() []rune {
// newEscapeInterpreter returns an escapeInterpreter that will be able to parse
// terminal escape sequences.
func newEscapeInterpreter(mode OutputMode) *escapeInterpreter {
func newEscapeInterpreter() *escapeInterpreter {
ei := &escapeInterpreter{
state: stateNone,
curFgColor: ColorDefault,
curBgColor: ColorDefault,
mode: mode,
// mode: mode,
}
return ei
}
@ -120,12 +120,13 @@ func (ei *escapeInterpreter) parseOne(ch rune) (isEscape bool, err error) {
return true, nil
case ch == 'm':
var err error
switch ei.mode {
case OutputNormal:
err = ei.outputNormal()
case Output256:
err = ei.output256()
}
err = ei.output256()
// switch ei.mode {
// case OutputNormal:
// err = ei.outputNormal()
// case Output256:
// err = ei.output256()
// }
if err != nil {
return false, errCSIParseError
}

@ -7,7 +7,6 @@ package gocui
import (
"errors"
"github.com/cointop-sh/cointop/pkg/termbox"
"github.com/gdamore/tcell/v2"
)
@ -20,15 +19,15 @@ var (
)
// OutputMode represents the terminal's output mode (8 or 256 colors).
type OutputMode termbox.OutputMode // TODO: die
// type OutputMode termbox.OutputMode // TODO: die
const ( // TODO: die
// OutputNormal provides 8-colors terminal mode.
OutputNormal = OutputMode(termbox.OutputNormal)
// const ( // TODO: die
// // OutputNormal provides 8-colors terminal mode.
// OutputNormal = OutputMode(termbox.OutputNormal)
// Output256 provides 256-colors terminal mode.
Output256 = OutputMode(termbox.Output256)
)
// // Output256 provides 256-colors terminal mode.
// Output256 = OutputMode(termbox.Output256)
// )
// Gui represents the whole User Interface, including the views, layouts
// and eventBindings.
@ -40,7 +39,8 @@ type Gui struct {
managers []Manager
eventBindings []*eventBinding
maxX, maxY int
outputMode OutputMode // TODO: die
// outputMode OutputMode // TODO: die
screen tcell.Screen
// BgColor and FgColor allow to configure the background and foreground
// colors of the GUI.
@ -70,13 +70,11 @@ type Gui struct {
// The current event while in the handlers.
CurrentEvent tcell.Event
// The formerly-global Screen used by this gui.
Screen tcell.Screen
}
// NewGui returns a new Gui object with a given output mode.
func NewGui(mode OutputMode) (*Gui, error) {
// func NewGui(mode OutputMode) (*Gui, error) {
func NewGui() (*Gui, error) {
g := &Gui{}
@ -86,18 +84,18 @@ func NewGui(mode OutputMode) (*Gui, error) {
} else if e = s.Init(); e != nil {
return nil, e
} else {
g.Screen = s
g.screen = s
}
g.outputMode = mode
termbox.SetScreen(g.Screen) // ugly global
termbox.SetOutputMode(termbox.OutputMode(mode))
// g.outputMode = mode
// termbox.SetScreen(g.Screen) // ugly global
// termbox.SetOutputMode(termbox.OutputMode(mode))
g.tbEvents = make(chan tcell.Event, 20)
g.userEvents = make(chan userEvent, 20)
g.maxX, g.maxY = g.Screen.Size()
g.maxX, g.maxY = g.screen.Size()
g.BgColor, g.FgColor = ColorDefault, ColorDefault
g.SelBgColor, g.SelFgColor = ColorDefault, ColorDefault
@ -108,7 +106,7 @@ func NewGui(mode OutputMode) (*Gui, error) {
// Close finalizes the library. It should be called after a successful
// initialization and when gocui is not needed anymore.
func (g *Gui) Close() {
g.Screen.Fini()
g.screen.Fini()
}
// Size returns the terminal's size.
@ -123,7 +121,12 @@ func (g *Gui) SetRune(x, y int, ch rune, fgColor, bgColor Attribute) error {
if x < 0 || y < 0 || x >= g.maxX || y >= g.maxY {
return errors.New("invalid point")
}
termbox.SetCell(x, y, ch, termbox.Attribute(fgColor), termbox.Attribute(bgColor))
// compat
// termbox.SetCell(x, y, ch, termbox.Attribute(fgColor), termbox.Attribute(fgColor))
st := g.MkStyle(fgColor, bgColor)
st = st.Foreground(g.scaledColor(x, y))
g.screen.SetContent(x, y, ch, nil, st)
return nil
}
@ -159,7 +162,7 @@ func (g *Gui) SetView(name string, x0, y0, x1, y1 int) (*View, error) {
return v, nil
}
v := newView(name, x0, y0, x1, y1, g.outputMode)
v := newView(name, x0, y0, x1, y1, g)
v.BgColor, v.FgColor = g.BgColor, g.FgColor
v.SelBgColor, v.SelFgColor = g.SelBgColor, g.SelFgColor
g.views = append(g.views, v)
@ -390,12 +393,12 @@ func (g *Gui) SetManagerFunc(manager func(*Gui) error) {
func (g *Gui) MainLoop() error {
go func() {
for {
g.tbEvents <- termbox.PollEvent()
g.tbEvents <- g.screen.PollEvent()
}
}()
if g.Mouse {
g.Screen.EnableMouse()
g.screen.EnableMouse()
}
// s.EnablePaste()
@ -453,11 +456,69 @@ 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
}
// TODO: delete termbox compat
func (g *Gui) MkStyle(fg, bg Attribute) tcell.Style {
st := tcell.StyleDefault
f := tcell.PaletteColor(int(fg)&0x1ff - 1)
b := tcell.PaletteColor(int(bg)&0x1ff - 1)
f = g.fixColor(f)
b = g.fixColor(b)
st = st.Foreground(f).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)
}
return st
}
func (g *Gui) scaledColor(x, y int) tcell.Color {
w, h := g.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)
}
// flush updates the gui, re-drawing frames and buffers.
func (g *Gui) flush() error {
termbox.Clear(termbox.Attribute(g.FgColor), termbox.Attribute(g.BgColor))
// 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)
}
}
maxX, maxY := g.Screen.Size()
maxX, maxY := g.screen.Size()
// if GUI's size has changed, we need to redraw all views
if maxX != g.maxX || maxY != g.maxY {
for _, v := range g.views {
@ -498,7 +559,7 @@ func (g *Gui) flush() error {
return err
}
}
g.Screen.Show()
g.screen.Show()
return nil
}
@ -603,13 +664,13 @@ func (g *Gui) draw(v *View) error {
gMaxX, gMaxY := g.Size()
cx, cy := curview.x0+curview.cx+1, curview.y0+curview.cy+1
if cx >= 0 && cx < gMaxX && cy >= 0 && cy < gMaxY {
g.Screen.ShowCursor(cx, cy)
g.screen.ShowCursor(cx, cy)
} else {
g.Screen.ShowCursor(-1, -1) // HideCursor
g.screen.ShowCursor(-1, -1) // HideCursor
}
}
} else {
g.Screen.ShowCursor(-1, -1) // HideCursor
g.screen.ShowCursor(-1, -1) // HideCursor
}
v.clearRunes()

@ -9,8 +9,6 @@ import (
"errors"
"io"
"strings"
"github.com/cointop-sh/cointop/pkg/termbox"
)
// A View is a window. It maintains its own internal buffer and cursor
@ -71,6 +69,9 @@ type View struct {
// If Mask is true, the View will display the mask instead of the real
// content
Mask rune
// The gui that owns this view
g *Gui
}
type viewLine struct {
@ -95,7 +96,7 @@ func (l lineType) String() string {
}
// newView returns a new View object.
func newView(name string, x0, y0, x1, y1 int, mode OutputMode) *View {
func newView(name string, x0, y0, x1, y1 int, g *Gui) *View {
v := &View{
name: name,
x0: x0,
@ -105,7 +106,8 @@ func newView(name string, x0, y0, x1, y1 int, mode OutputMode) *View {
Frame: true,
Editor: DefaultEditor,
tainted: true,
ei: newEscapeInterpreter(mode),
ei: newEscapeInterpreter(),
g: g,
}
return v
}
@ -153,8 +155,7 @@ func (v *View) setRune(x, y int, ch rune, fgColor, bgColor Attribute) error {
bgColor = v.SelBgColor
}
termbox.SetCell(v.x0+x+1, v.y0+y+1, ch,
termbox.Attribute(fgColor), termbox.Attribute(bgColor))
v.g.SetRune(v.x0+x+1, v.y0+y+1, ch, fgColor, bgColor)
return nil
}
@ -402,8 +403,7 @@ func (v *View) clearRunes() {
maxX, maxY := v.Size()
for x := 0; x < maxX; x++ {
for y := 0; y < maxY; y++ {
termbox.SetCell(v.x0+x+1, v.y0+y+1, ' ',
termbox.Attribute(v.FgColor), termbox.Attribute(v.BgColor))
v.g.SetRune(v.x0+x+1, v.y0+y+1, ' ', v.FgColor, v.BgColor)
}
}
}

@ -16,17 +16,13 @@
// the github.com/nsf/termbox package.
package termbox
import (
"github.com/gdamore/tcell/v2"
)
// Ugly globals
var screen tcell.Screen
var outMode OutputMode
// var screen tcell.Screen
// var outMode OutputMode
func SetScreen(s tcell.Screen) {
screen = s
}
// func SetScreen(s tcell.Screen) {
// screen = s
// }
// Attribute affects the presentation of characters, such as color, boldness,
// and so forth.
@ -52,105 +48,56 @@ const (
AttrReverse
)
func fixColor(c tcell.Color) tcell.Color {
if c == tcell.ColorDefault {
return c
}
switch outMode {
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 mkStyle(fg, bg Attribute) tcell.Style {
st := tcell.StyleDefault
f := tcell.PaletteColor(int(fg)&0x1ff - 1)
b := tcell.PaletteColor(int(bg)&0x1ff - 1)
f = fixColor(f)
b = fixColor(b)
st = st.Foreground(f).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)
}
return st
}
// 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)
}
}
}
// 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
// type OutputMode int
// OutputMode values.
const (
OutputCurrent OutputMode = iota
OutputNormal
Output256
Output216
OutputGrayscale
)
// // 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
}
}
// 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
// }
// }
// 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)
}
// SetCell sets the character cell at a given location to the given
// content (rune) and attributes.
func SetCell(x, y int, ch rune, fg, bg Attribute) {
st := mkStyle(fg, bg)
// Set the foreground color to a scaled version of the coordinates
st = st.Foreground(scaledColor(x, y))
screen.SetContent(x, y, ch, nil, st)
}
// 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)
// }
// PollEvent blocks until an event is ready, and then returns it.
func PollEvent() tcell.Event {
return screen.PollEvent()
}
// func PollEvent() tcell.Event {
// return screen.PollEvent()
// }

@ -10,7 +10,6 @@ import (
"sync"
"time"
"github.com/cointop-sh/cointop/pkg/termbox"
"github.com/gdamore/tcell/v2"
log "github.com/sirupsen/logrus"
)
@ -128,18 +127,18 @@ type EvtMouse struct {
type EvtErr error
func hookTermboxEvt() {
log.Debugf("XXX hookTermboxEvt")
for {
e := termbox.PollEvent()
log.Debugf("XXX event %s", e)
for _, c := range sysEvtChs {
func(ch chan Event) {
ch <- crtTermboxEvt(e)
}(c)
}
}
}
// func hookTermboxEvt() {
// log.Debugf("XXX hookTermboxEvt")
// for {
// e := termbox.PollEvent()
// log.Debugf("XXX event %s", e)
// for _, c := range sysEvtChs {
// func(ch chan Event) {
// ch <- crtTermboxEvt(e)
// }(c)
// }
// }
// }
func NewSysEvtCh() chan Event {
ec := make(chan Event)

@ -11,7 +11,7 @@ type UI struct {
// NewUI returns a new UI instance
func NewUI() (*UI, error) {
g, err := gocui.NewGui(gocui.Output256)
g, err := gocui.NewGui()
if err != nil {
return nil, err
}

Loading…
Cancel
Save