Fix the flickering issue

This commit is contained in:
Sameer Rahmani 2022-11-24 15:34:17 +00:00
parent b39584554c
commit 944c43b396
1 changed files with 13 additions and 6 deletions

View File

@ -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