From 944c43b396eaa6d8d02f24b6fe000bbf86ae89a9 Mon Sep 17 00:00:00 2001 From: Sameer Rahmani Date: Thu, 24 Nov 2022 15:34:17 +0000 Subject: [PATCH] Fix the flickering issue --- filter.go | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/filter.go b/filter.go index 8d856ae..8a9c0c5 100644 --- a/filter.go +++ b/filter.go @@ -18,6 +18,7 @@ package main import ( "fmt" "log" + "strings" "github.com/gdamore/tcell/v2" ) @@ -42,7 +43,7 @@ var helpStyle tcell.Style func drawText(s tcell.Screen, x1, y1, x2, y2 int, style tcell.Style, text string) { row := y1 col := x1 - for _, r := range []rune(text) { + for _, r := range text { s.SetContent(col, row, r, nil, style) col++ if col >= x2 { @@ -65,10 +66,10 @@ func Render(s tcell.Screen, inputs Items, lowerBound int, upperBound int, curren var txt string if item.Selected { - txt = fmt.Sprintf(" X %s", item.Text) + txt = fmt.Sprintf(" X %s", item.Text) style = selectedStyle } else { - txt = fmt.Sprintf(" %s", item.Text) + txt = fmt.Sprintf(" %s", item.Text) } if current == lowerBound+i { @@ -76,8 +77,16 @@ func Render(s tcell.Screen, inputs Items, lowerBound int, upperBound int, curren } drawText(s, 0, y, xmax-1, y+1, style, txt) + // Clean up the rest of the line that might've been filled with a previous render. + // This is a hack to NOT use `Clear` on each loop + drawText(s, len(txt), y, xmax-1, y+1, style, strings.Repeat(" ", xmax-1-len(txt))) + if current == lowerBound+i { + drawText(s, 0, y, 1, y+1, tcell.StyleDefault.Foreground(tcell.ColorGreen).Bold(true), ">") + } + y += 1 } + } func main() { @@ -88,7 +97,7 @@ func main() { // TODO: Make the default style configurable defStyle := tcell.StyleDefault.Background(tcell.ColorDefault).Foreground(tcell.ColorDefault) - highlight = tcell.StyleDefault.Background(tcell.ColorDarkGray).Foreground(tcell.ColorWhite) + highlight = tcell.StyleDefault.Background(tcell.ColorDefault).Foreground(tcell.ColorWhite).Bold(true) selectedStyle = tcell.StyleDefault.Foreground(tcell.ColorYellow) helpStyle = tcell.StyleDefault.Foreground(tcell.ColorDefault) @@ -129,8 +138,6 @@ func main() { // Event loop for !done { - //s.Clear() - Render(s, input, lowerBound, upperBound, currentLine) // Update screen