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 ( import (
"fmt" "fmt"
"log" "log"
"strings"
"github.com/gdamore/tcell/v2" "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) { func drawText(s tcell.Screen, x1, y1, x2, y2 int, style tcell.Style, text string) {
row := y1 row := y1
col := x1 col := x1
for _, r := range []rune(text) { for _, r := range text {
s.SetContent(col, row, r, nil, style) s.SetContent(col, row, r, nil, style)
col++ col++
if col >= x2 { if col >= x2 {
@ -65,10 +66,10 @@ func Render(s tcell.Screen, inputs Items, lowerBound int, upperBound int, curren
var txt string var txt string
if item.Selected { if item.Selected {
txt = fmt.Sprintf(" X %s", item.Text) txt = fmt.Sprintf(" X %s", item.Text)
style = selectedStyle style = selectedStyle
} else { } else {
txt = fmt.Sprintf(" %s", item.Text) txt = fmt.Sprintf(" %s", item.Text)
} }
if current == lowerBound+i { 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) 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 y += 1
} }
} }
func main() { func main() {
@ -88,7 +97,7 @@ func main() {
// TODO: Make the default style configurable // TODO: Make the default style configurable
defStyle := tcell.StyleDefault.Background(tcell.ColorDefault).Foreground(tcell.ColorDefault) 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) selectedStyle = tcell.StyleDefault.Foreground(tcell.ColorYellow)
helpStyle = tcell.StyleDefault.Foreground(tcell.ColorDefault) helpStyle = tcell.StyleDefault.Foreground(tcell.ColorDefault)
@ -129,8 +138,6 @@ func main() {
// Event loop // Event loop
for !done { for !done {
//s.Clear()
Render(s, input, lowerBound, upperBound, currentLine) Render(s, input, lowerBound, upperBound, currentLine)
// Update screen // Update screen