From d4f6c4104d6de709831069577fccc113ed4b7db1 Mon Sep 17 00:00:00 2001 From: Leo dev Date: Fri, 4 Oct 2024 22:55:30 +0200 Subject: [PATCH] fixed flickering on all platforms --- example/main.go | 2 +- osui.go | 11 ++++++++--- ui/div.go | 2 +- utils.go | 24 ------------------------ 4 files changed, 10 insertions(+), 29 deletions(-) diff --git a/example/main.go b/example/main.go index 5dce14c..3b92096 100644 --- a/example/main.go +++ b/example/main.go @@ -33,7 +33,7 @@ func App() *ui.PaginatorComponent { ui.WithPosition(0, 0, ui.Text("This blue square is a div. A div stores multiple components into one")), ui.WithPosition((w-30)/2, (h-4)/2, ui.WithSize(32, 7, - ui.Div(ui.WithPosition(6, 1, ui.Button("Hello, World!").Params(ui.ButtonParams{Width: 19}))).Params(ui.DivParams{Style: ui.DivStyle{Outline: colors.Blue}}), + ui.Div(ui.WithPosition(2, 2, ui.Text("This text is inside a div!"))).Params(ui.DivParams{Style: ui.DivStyle{Outline: colors.Blue, Foreground: colors.Red}}), )), keybindings.Class(ui.Text("Navigate: Ctrl+(W/A/S/D)")), ), diff --git a/osui.go b/osui.go index f21d868..aeab43f 100644 --- a/osui.go +++ b/osui.go @@ -2,9 +2,11 @@ package osui import ( "fmt" + "os" "strings" "github.com/orus-dev/osui/colors" + "golang.org/x/term" ) type ComponentData struct { @@ -48,11 +50,15 @@ func (s *Screen) Render() { data.DefaultColor = colors.Reset RenderOnFrame(s.component, &frame) Clear() - fmt.Print(strings.Join(frame, "\n")) + fmt.Print(strings.Join(frame, "")) } func (s *Screen) Run() { - DisableEcho() + oldState, err := term.MakeRaw(int(os.Stdin.Fd())) + if err != nil { + panic(err) + } + defer term.Restore(int(os.Stdin.Fd()), oldState) data := s.component.GetComponentData() data.Screen = s for { @@ -60,7 +66,6 @@ func (s *Screen) Run() { k, _ := ReadKey() if s.component.Update(k) { ShowCursor() - EnableEcho() return } } diff --git a/ui/div.go b/ui/div.go index cf5eee6..0020991 100644 --- a/ui/div.go +++ b/ui/div.go @@ -58,7 +58,7 @@ func (d *DivComponent) Render() string { return strings.Join(frame, "\n") } else { for i, f := range frame { - frame[i] = d.Style.Outline + "│" + colors.Combine(d.Style.Foreground, d.Style.Background) + f + colors.Reset + d.Style.Outline + "│" + colors.Reset + frame[i] = d.Style.Outline + "│" + colors.Reset + colors.Combine(d.Style.Foreground, d.Style.Background) + f + colors.Reset + d.Style.Outline + "│" + colors.Reset } } return " " + d.Style.Outline + strings.Repeat("_", d.Data.Width-2) + colors.Reset + "\n" + strings.Join(frame, "\n") + "\n " + d.Style.Outline + strings.Repeat("‾", d.Data.Width-2) + colors.Reset diff --git a/utils.go b/utils.go index 07eb256..14458d8 100644 --- a/utils.go +++ b/utils.go @@ -11,7 +11,6 @@ import ( "github.com/nathan-fiscaletti/consolesize-go" "github.com/orus-dev/osui/colors" - "golang.org/x/term" ) var re = regexp.MustCompile(`(\x1b\[([0-9;]*)[a-zA-Z])+`) @@ -71,11 +70,6 @@ func RenderOnFrame(c Component, frame *[]string) { } func ReadKey() (string, error) { - oldState, err := term.MakeRaw(int(os.Stdin.Fd())) - if err != nil { - return "", err - } - defer term.Restore(int(os.Stdin.Fd()), oldState) var b [3]byte n, err := os.Stdin.Read(b[:]) if err != nil { @@ -84,24 +78,6 @@ func ReadKey() (string, error) { return string(b[:n]), nil } -func DisableEcho() { - switch runtime.GOOS { - case "windows": - // Need to implement this later - default: - exec.Command("stty", "-F", "/dev/tty", "-echo").Run() - } -} - -func EnableEcho() { - switch runtime.GOOS { - case "windows": - // Need to implement this later - default: - exec.Command("stty", "-F", "/dev/tty", "echo").Run() - } -} - func NewFrame(width, height int) []string { frame := make([]string, height) for i := 0; i < height; i++ {