From e1582b8323a70785d7ebefce993df7474a28e749 Mon Sep 17 00:00:00 2001 From: Junegunn Choi Date: Sat, 9 Sep 2017 13:50:07 +0900 Subject: [PATCH] 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). --- src/terminal.go | 19 +++++-------- src/tui/dummy.go | 1 - src/tui/light.go | 4 --- src/tui/tcell.go | 29 ++++++++++---------- src/tui/tui.go | 70 +++++++++++++++++++++--------------------------- 5 files changed, 53 insertions(+), 70 deletions(-) diff --git a/src/terminal.go b/src/terminal.go index 92e34259..7813680a 100644 --- a/src/terminal.go +++ b/src/terminal.go @@ -623,10 +623,8 @@ func (t *Terminal) resizeWindows() { width, height, tui.BorderNone) } - if !t.tui.IsOptimized() { - for i := 0; i < t.window.Height(); i++ { - t.window.MoveAndClear(i, 0) - } + for i := 0; i < t.window.Height(); i++ { + t.window.MoveAndClear(i, 0) } t.truncateQuery() } @@ -722,7 +720,7 @@ func (t *Terminal) printHeader() { t.move(line, 2, true) 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 } - // Optimized renderer can simply erase to the end of the window - t.move(line, 0, t.tui.IsOptimized()) + t.move(line, 0, false) t.window.CPrint(tui.ColCursor, t.strong, label) if current { if selected { @@ -793,11 +790,9 @@ func (t *Terminal) printItem(result Result, line int, i int, current bool) { } newLine.width = t.printHighlighted(result, 0, tui.ColNormal, tui.ColMatch, false, true) } - if !t.tui.IsOptimized() { - fillSpaces := prevLine.width - newLine.width - if fillSpaces > 0 { - t.window.Print(strings.Repeat(" ", fillSpaces)) - } + fillSpaces := prevLine.width - newLine.width + if fillSpaces > 0 { + t.window.Print(strings.Repeat(" ", fillSpaces)) } t.prevLines[i] = newLine } diff --git a/src/tui/dummy.go b/src/tui/dummy.go index c96ce8eb..3965c9ea 100644 --- a/src/tui/dummy.go +++ b/src/tui/dummy.go @@ -33,7 +33,6 @@ func (r *FullscreenRenderer) Refresh() {} func (r *FullscreenRenderer) Close() {} func (r *FullscreenRenderer) DoesAutoWrap() bool { return false } -func (r *FullscreenRenderer) IsOptimized() bool { return false } func (r *FullscreenRenderer) GetChar() Event { return Event{} } func (r *FullscreenRenderer) MaxX() int { return 0 } func (r *FullscreenRenderer) MaxY() int { return 0 } diff --git a/src/tui/light.go b/src/tui/light.go index fe9035ea..52e26edb 100644 --- a/src/tui/light.go +++ b/src/tui/light.go @@ -620,10 +620,6 @@ func (r *LightRenderer) DoesAutoWrap() bool { return false } -func (r *LightRenderer) IsOptimized() bool { - return false -} - func (r *LightRenderer) NewWindow(top int, left int, width int, height int, borderStyle BorderStyle) Window { w := &LightWindow{ renderer: r, diff --git a/src/tui/tcell.go b/src/tui/tcell.go index bed52801..7db37c4c 100644 --- a/src/tui/tcell.go +++ b/src/tui/tcell.go @@ -172,10 +172,6 @@ func (r *FullscreenRenderer) DoesAutoWrap() bool { return false } -func (r *FullscreenRenderer) IsOptimized() bool { - return false -} - func (r *FullscreenRenderer) Clear() { _screen.Sync() _screen.Clear() @@ -409,14 +405,13 @@ func (w *TcellWindow) Close() { func fill(x, y, w, h int, r rune) { for ly := 0; ly <= h; ly++ { 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() { - // TODO - fill(w.left, w.top, w.width, w.height, ' ') + fill(w.left-1, w.top, w.width+1, w.height, ' ') } 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) { w.Move(y, x) 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 } 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) { @@ -452,7 +447,7 @@ func (w *TcellWindow) printString(text string, pair ColorPair, a Attr) { Reverse(a&Attr(tcell.AttrReverse) != 0). Underline(a&Attr(tcell.AttrUnderline) != 0) } else { - style = ColDefault.style(). + style = ColNormal.style(). Reverse(a&Attr(tcell.AttrReverse) != 0 || pair == ColCurrent || 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 { style = pair.style() } else { - style = ColDefault.style() + style = ColNormal.style() } style = style. 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 { - 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 { - 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) { @@ -560,7 +561,7 @@ func (w *TcellWindow) drawBorder(around bool) { if w.color { style = ColBorder.style() } else { - style = ColDefault.style() + style = ColNormal.style() } for x := left; x < right; x++ { diff --git a/src/tui/tui.go b/src/tui/tui.go index 24206160..244e0b05 100644 --- a/src/tui/tui.go +++ b/src/tui/tui.go @@ -133,7 +133,7 @@ const ( type ColorPair struct { fg Color bg Color - id int16 + id int } func HexToColor(rrggbb string) Color { @@ -155,12 +155,8 @@ func (p ColorPair) Bg() Color { return p.bg } -func (p ColorPair) key() int { - return (int(p.Fg()) << 8) + int(p.Bg()) -} - func (p ColorPair) is24() bool { - return p.Fg().is24() || p.Bg().is24() + return p.fg.is24() || p.bg.is24() } type ColorTheme struct { @@ -179,10 +175,6 @@ type ColorTheme struct { Border Color } -func (t *ColorTheme) HasBg() bool { - return t.Bg != colDefault -} - type Event struct { Type int Char rune @@ -220,7 +212,6 @@ type Renderer interface { MaxX() int MaxY() int DoesAutoWrap() bool - IsOptimized() bool NewWindow(top int, left int, width int, height int, borderStyle BorderStyle) Window } @@ -271,7 +262,6 @@ var ( Dark256 *ColorTheme Light256 *ColorTheme - ColDefault ColorPair ColNormal ColorPair ColPrompt ColorPair ColMatch ColorPair @@ -283,7 +273,6 @@ var ( ColSelected ColorPair ColHeader ColorPair ColBorder ColorPair - ColUser ColorPair ) func EmptyTheme() *ColorTheme { @@ -387,33 +376,36 @@ func initTheme(theme *ColorTheme, baseTheme *ColorTheme, forceBlack bool) { } func initPalette(theme *ColorTheme) { - ColDefault = ColorPair{colDefault, colDefault, 0} - if theme != nil { - ColNormal = ColorPair{theme.Fg, theme.Bg, 1} - ColPrompt = ColorPair{theme.Prompt, theme.Bg, 2} - ColMatch = ColorPair{theme.Match, theme.Bg, 3} - ColCurrent = ColorPair{theme.Current, theme.DarkBg, 4} - ColCurrentMatch = ColorPair{theme.CurrentMatch, theme.DarkBg, 5} - ColSpinner = ColorPair{theme.Spinner, theme.Bg, 6} - ColInfo = ColorPair{theme.Info, theme.Bg, 7} - ColCursor = ColorPair{theme.Cursor, theme.DarkBg, 8} - ColSelected = ColorPair{theme.Selected, theme.DarkBg, 9} - ColHeader = ColorPair{theme.Header, theme.Bg, 10} - ColBorder = ColorPair{theme.Border, theme.Bg, 11} - } else { - ColNormal = ColorPair{colDefault, colDefault, 1} - ColPrompt = ColorPair{colDefault, colDefault, 2} - ColMatch = ColorPair{colDefault, colDefault, 3} - ColCurrent = ColorPair{colDefault, colDefault, 4} - ColCurrentMatch = ColorPair{colDefault, colDefault, 5} - ColSpinner = ColorPair{colDefault, colDefault, 6} - ColInfo = ColorPair{colDefault, colDefault, 7} - ColCursor = ColorPair{colDefault, colDefault, 8} - ColSelected = ColorPair{colDefault, colDefault, 9} - ColHeader = ColorPair{colDefault, colDefault, 10} - ColBorder = ColorPair{colDefault, colDefault, 11} + idx := 0 + pair := func(fg, bg Color) ColorPair { + idx++ + return ColorPair{fg, bg, idx} + } + if theme != nil { + ColNormal = pair(theme.Fg, theme.Bg) + ColPrompt = pair(theme.Prompt, theme.Bg) + ColMatch = pair(theme.Match, theme.Bg) + ColCurrent = pair(theme.Current, theme.DarkBg) + ColCurrentMatch = pair(theme.CurrentMatch, theme.DarkBg) + ColSpinner = pair(theme.Spinner, theme.Bg) + ColInfo = pair(theme.Info, theme.Bg) + ColCursor = pair(theme.Cursor, theme.DarkBg) + ColSelected = pair(theme.Selected, theme.DarkBg) + ColHeader = pair(theme.Header, theme.Bg) + ColBorder = pair(theme.Border, theme.Bg) + } else { + ColNormal = pair(colDefault, colDefault) + ColPrompt = pair(colDefault, colDefault) + ColMatch = pair(colDefault, colDefault) + ColCurrent = pair(colDefault, colDefault) + ColCurrentMatch = pair(colDefault, colDefault) + ColSpinner = pair(colDefault, colDefault) + 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 {