Clean up renderer code

Remove code that is no longer relevant after the removal of ncurses
renderer. This commit also fixes background color issue on tcell-based
FullscreenRenderer (Windows).
This commit is contained in:
Junegunn Choi 2017-09-09 13:50:07 +09:00
parent 7cfa6f0265
commit e1582b8323
No known key found for this signature in database
GPG Key ID: 254BC280FEF9C627
5 changed files with 53 additions and 70 deletions

View File

@ -623,11 +623,9 @@ func (t *Terminal) resizeWindows() {
width, width,
height, tui.BorderNone) height, tui.BorderNone)
} }
if !t.tui.IsOptimized() {
for i := 0; i < t.window.Height(); i++ { for i := 0; i < t.window.Height(); i++ {
t.window.MoveAndClear(i, 0) t.window.MoveAndClear(i, 0)
} }
}
t.truncateQuery() t.truncateQuery()
} }
@ -722,7 +720,7 @@ func (t *Terminal) printHeader() {
t.move(line, 2, true) t.move(line, 2, true)
t.printHighlighted(Result{item: item}, t.printHighlighted(Result{item: item},
tui.AttrRegular, tui.ColHeader, tui.ColDefault, false, false) tui.AttrRegular, tui.ColHeader, tui.ColHeader, false, false)
} }
} }
@ -775,8 +773,7 @@ func (t *Terminal) printItem(result Result, line int, i int, current bool) {
return return
} }
// Optimized renderer can simply erase to the end of the window t.move(line, 0, false)
t.move(line, 0, t.tui.IsOptimized())
t.window.CPrint(tui.ColCursor, t.strong, label) t.window.CPrint(tui.ColCursor, t.strong, label)
if current { if current {
if selected { if selected {
@ -793,12 +790,10 @@ func (t *Terminal) printItem(result Result, line int, i int, current bool) {
} }
newLine.width = t.printHighlighted(result, 0, tui.ColNormal, tui.ColMatch, false, true) newLine.width = t.printHighlighted(result, 0, tui.ColNormal, tui.ColMatch, false, true)
} }
if !t.tui.IsOptimized() {
fillSpaces := prevLine.width - newLine.width fillSpaces := prevLine.width - newLine.width
if fillSpaces > 0 { if fillSpaces > 0 {
t.window.Print(strings.Repeat(" ", fillSpaces)) t.window.Print(strings.Repeat(" ", fillSpaces))
} }
}
t.prevLines[i] = newLine t.prevLines[i] = newLine
} }

View File

@ -33,7 +33,6 @@ func (r *FullscreenRenderer) Refresh() {}
func (r *FullscreenRenderer) Close() {} func (r *FullscreenRenderer) Close() {}
func (r *FullscreenRenderer) DoesAutoWrap() bool { return false } func (r *FullscreenRenderer) DoesAutoWrap() bool { return false }
func (r *FullscreenRenderer) IsOptimized() bool { return false }
func (r *FullscreenRenderer) GetChar() Event { return Event{} } func (r *FullscreenRenderer) GetChar() Event { return Event{} }
func (r *FullscreenRenderer) MaxX() int { return 0 } func (r *FullscreenRenderer) MaxX() int { return 0 }
func (r *FullscreenRenderer) MaxY() int { return 0 } func (r *FullscreenRenderer) MaxY() int { return 0 }

View File

@ -620,10 +620,6 @@ func (r *LightRenderer) DoesAutoWrap() bool {
return false return false
} }
func (r *LightRenderer) IsOptimized() bool {
return false
}
func (r *LightRenderer) NewWindow(top int, left int, width int, height int, borderStyle BorderStyle) Window { func (r *LightRenderer) NewWindow(top int, left int, width int, height int, borderStyle BorderStyle) Window {
w := &LightWindow{ w := &LightWindow{
renderer: r, renderer: r,

View File

@ -172,10 +172,6 @@ func (r *FullscreenRenderer) DoesAutoWrap() bool {
return false return false
} }
func (r *FullscreenRenderer) IsOptimized() bool {
return false
}
func (r *FullscreenRenderer) Clear() { func (r *FullscreenRenderer) Clear() {
_screen.Sync() _screen.Sync()
_screen.Clear() _screen.Clear()
@ -409,14 +405,13 @@ func (w *TcellWindow) Close() {
func fill(x, y, w, h int, r rune) { func fill(x, y, w, h int, r rune) {
for ly := 0; ly <= h; ly++ { for ly := 0; ly <= h; ly++ {
for lx := 0; lx <= w; lx++ { for lx := 0; lx <= w; lx++ {
_screen.SetContent(x+lx, y+ly, r, nil, ColDefault.style()) _screen.SetContent(x+lx, y+ly, r, nil, ColNormal.style())
} }
} }
} }
func (w *TcellWindow) Erase() { func (w *TcellWindow) Erase() {
// TODO fill(w.left-1, w.top, w.width+1, w.height, ' ')
fill(w.left, w.top, w.width, w.height, ' ')
} }
func (w *TcellWindow) Enclose(y int, x int) bool { func (w *TcellWindow) Enclose(y int, x int) bool {
@ -433,13 +428,13 @@ func (w *TcellWindow) Move(y int, x int) {
func (w *TcellWindow) MoveAndClear(y int, x int) { func (w *TcellWindow) MoveAndClear(y int, x int) {
w.Move(y, x) w.Move(y, x)
for i := w.lastX; i < w.width; i++ { for i := w.lastX; i < w.width; i++ {
_screen.SetContent(i+w.left, w.lastY+w.top, rune(' '), nil, ColDefault.style()) _screen.SetContent(i+w.left, w.lastY+w.top, rune(' '), nil, ColNormal.style())
} }
w.lastX = x w.lastX = x
} }
func (w *TcellWindow) Print(text string) { func (w *TcellWindow) Print(text string) {
w.printString(text, ColDefault, 0) w.printString(text, ColNormal, 0)
} }
func (w *TcellWindow) printString(text string, pair ColorPair, a Attr) { func (w *TcellWindow) printString(text string, pair ColorPair, a Attr) {
@ -452,7 +447,7 @@ func (w *TcellWindow) printString(text string, pair ColorPair, a Attr) {
Reverse(a&Attr(tcell.AttrReverse) != 0). Reverse(a&Attr(tcell.AttrReverse) != 0).
Underline(a&Attr(tcell.AttrUnderline) != 0) Underline(a&Attr(tcell.AttrUnderline) != 0)
} else { } else {
style = ColDefault.style(). style = ColNormal.style().
Reverse(a&Attr(tcell.AttrReverse) != 0 || pair == ColCurrent || pair == ColCurrentMatch). Reverse(a&Attr(tcell.AttrReverse) != 0 || pair == ColCurrent || pair == ColCurrentMatch).
Underline(a&Attr(tcell.AttrUnderline) != 0 || pair == ColMatch || pair == ColCurrentMatch) Underline(a&Attr(tcell.AttrUnderline) != 0 || pair == ColMatch || pair == ColCurrentMatch)
} }
@ -503,7 +498,7 @@ func (w *TcellWindow) fillString(text string, pair ColorPair, a Attr) FillReturn
if w.color { if w.color {
style = pair.style() style = pair.style()
} else { } else {
style = ColDefault.style() style = ColNormal.style()
} }
style = style. style = style.
Blink(a&Attr(tcell.AttrBlink) != 0). Blink(a&Attr(tcell.AttrBlink) != 0).
@ -543,11 +538,17 @@ func (w *TcellWindow) fillString(text string, pair ColorPair, a Attr) FillReturn
} }
func (w *TcellWindow) Fill(str string) FillReturn { func (w *TcellWindow) Fill(str string) FillReturn {
return w.fillString(str, ColDefault, 0) return w.fillString(str, ColNormal, 0)
} }
func (w *TcellWindow) CFill(fg Color, bg Color, a Attr, str string) FillReturn { func (w *TcellWindow) CFill(fg Color, bg Color, a Attr, str string) FillReturn {
return w.fillString(str, ColorPair{fg, bg, -1}, a) if fg == colDefault {
fg = ColNormal.Fg()
}
if bg == colDefault {
bg = ColNormal.Bg()
}
return w.fillString(str, NewColorPair(fg, bg), a)
} }
func (w *TcellWindow) drawBorder(around bool) { func (w *TcellWindow) drawBorder(around bool) {
@ -560,7 +561,7 @@ func (w *TcellWindow) drawBorder(around bool) {
if w.color { if w.color {
style = ColBorder.style() style = ColBorder.style()
} else { } else {
style = ColDefault.style() style = ColNormal.style()
} }
for x := left; x < right; x++ { for x := left; x < right; x++ {

View File

@ -133,7 +133,7 @@ const (
type ColorPair struct { type ColorPair struct {
fg Color fg Color
bg Color bg Color
id int16 id int
} }
func HexToColor(rrggbb string) Color { func HexToColor(rrggbb string) Color {
@ -155,12 +155,8 @@ func (p ColorPair) Bg() Color {
return p.bg return p.bg
} }
func (p ColorPair) key() int {
return (int(p.Fg()) << 8) + int(p.Bg())
}
func (p ColorPair) is24() bool { func (p ColorPair) is24() bool {
return p.Fg().is24() || p.Bg().is24() return p.fg.is24() || p.bg.is24()
} }
type ColorTheme struct { type ColorTheme struct {
@ -179,10 +175,6 @@ type ColorTheme struct {
Border Color Border Color
} }
func (t *ColorTheme) HasBg() bool {
return t.Bg != colDefault
}
type Event struct { type Event struct {
Type int Type int
Char rune Char rune
@ -220,7 +212,6 @@ type Renderer interface {
MaxX() int MaxX() int
MaxY() int MaxY() int
DoesAutoWrap() bool DoesAutoWrap() bool
IsOptimized() bool
NewWindow(top int, left int, width int, height int, borderStyle BorderStyle) Window NewWindow(top int, left int, width int, height int, borderStyle BorderStyle) Window
} }
@ -271,7 +262,6 @@ var (
Dark256 *ColorTheme Dark256 *ColorTheme
Light256 *ColorTheme Light256 *ColorTheme
ColDefault ColorPair
ColNormal ColorPair ColNormal ColorPair
ColPrompt ColorPair ColPrompt ColorPair
ColMatch ColorPair ColMatch ColorPair
@ -283,7 +273,6 @@ var (
ColSelected ColorPair ColSelected ColorPair
ColHeader ColorPair ColHeader ColorPair
ColBorder ColorPair ColBorder ColorPair
ColUser ColorPair
) )
func EmptyTheme() *ColorTheme { func EmptyTheme() *ColorTheme {
@ -387,33 +376,36 @@ func initTheme(theme *ColorTheme, baseTheme *ColorTheme, forceBlack bool) {
} }
func initPalette(theme *ColorTheme) { func initPalette(theme *ColorTheme) {
ColDefault = ColorPair{colDefault, colDefault, 0} idx := 0
if theme != nil { pair := func(fg, bg Color) ColorPair {
ColNormal = ColorPair{theme.Fg, theme.Bg, 1} idx++
ColPrompt = ColorPair{theme.Prompt, theme.Bg, 2} return ColorPair{fg, bg, idx}
ColMatch = ColorPair{theme.Match, theme.Bg, 3} }
ColCurrent = ColorPair{theme.Current, theme.DarkBg, 4} if theme != nil {
ColCurrentMatch = ColorPair{theme.CurrentMatch, theme.DarkBg, 5} ColNormal = pair(theme.Fg, theme.Bg)
ColSpinner = ColorPair{theme.Spinner, theme.Bg, 6} ColPrompt = pair(theme.Prompt, theme.Bg)
ColInfo = ColorPair{theme.Info, theme.Bg, 7} ColMatch = pair(theme.Match, theme.Bg)
ColCursor = ColorPair{theme.Cursor, theme.DarkBg, 8} ColCurrent = pair(theme.Current, theme.DarkBg)
ColSelected = ColorPair{theme.Selected, theme.DarkBg, 9} ColCurrentMatch = pair(theme.CurrentMatch, theme.DarkBg)
ColHeader = ColorPair{theme.Header, theme.Bg, 10} ColSpinner = pair(theme.Spinner, theme.Bg)
ColBorder = ColorPair{theme.Border, theme.Bg, 11} ColInfo = pair(theme.Info, theme.Bg)
} else { ColCursor = pair(theme.Cursor, theme.DarkBg)
ColNormal = ColorPair{colDefault, colDefault, 1} ColSelected = pair(theme.Selected, theme.DarkBg)
ColPrompt = ColorPair{colDefault, colDefault, 2} ColHeader = pair(theme.Header, theme.Bg)
ColMatch = ColorPair{colDefault, colDefault, 3} ColBorder = pair(theme.Border, theme.Bg)
ColCurrent = ColorPair{colDefault, colDefault, 4} } else {
ColCurrentMatch = ColorPair{colDefault, colDefault, 5} ColNormal = pair(colDefault, colDefault)
ColSpinner = ColorPair{colDefault, colDefault, 6} ColPrompt = pair(colDefault, colDefault)
ColInfo = ColorPair{colDefault, colDefault, 7} ColMatch = pair(colDefault, colDefault)
ColCursor = ColorPair{colDefault, colDefault, 8} ColCurrent = pair(colDefault, colDefault)
ColSelected = ColorPair{colDefault, colDefault, 9} ColCurrentMatch = pair(colDefault, colDefault)
ColHeader = ColorPair{colDefault, colDefault, 10} ColSpinner = pair(colDefault, colDefault)
ColBorder = ColorPair{colDefault, colDefault, 11} ColInfo = pair(colDefault, colDefault)
ColCursor = pair(colDefault, colDefault)
ColSelected = pair(colDefault, colDefault)
ColHeader = pair(colDefault, colDefault)
ColBorder = pair(colDefault, colDefault)
} }
ColUser = ColorPair{colDefault, colDefault, 12}
} }
func attrFor(color ColorPair, attr Attr) Attr { func attrFor(color ColorPair, attr Attr) Attr {