From cf93b3383f5d1634c832ea14c1e7179c3f6752d8 Mon Sep 17 00:00:00 2001 From: Leo Date: Fri, 27 Sep 2024 14:38:39 +0200 Subject: [PATCH] bug fix (There is a small bug on inputBox again) --- example/main.go | 18 +++++++++------ isKey/key.go | 33 +++++++++++++++++++++++++++ ui/div.go | 24 +++++++++----------- ui/inputBox.go | 24 +++++++++++++------- ui/paginator.go | 10 ++++----- ui/text.go | 3 +-- utils.go | 60 +++++-------------------------------------------- 7 files changed, 83 insertions(+), 89 deletions(-) create mode 100644 isKey/key.go diff --git a/example/main.go b/example/main.go index b00d2ca..3732834 100644 --- a/example/main.go +++ b/example/main.go @@ -8,17 +8,21 @@ import ( ) func main() { - text := colors.Red + "This is a div!" width, h := osui.GetTerminalSize() paginator := ui.Paginator( ui.Div( - ui.Text("Welcome to the example! Press tab to go to the next page or press tab to go to a previous page"), + ui.Text("Welcome to the example! Press tab to go to the next page or press shift + tab to go to a previous page"), + ), + ui.Div( + cutils.WithPosition((width-30)/2, (h-4)/2, + cutils.WithSize(30, 4, + cutils.WithStyle(&ui.DivStyle{Background: colors.Blue, Foreground: colors.Red}, + ui.Div(cutils.WithPosition(0, 0, ui.Text("This is a test!"))), + )))), + ui.Div( + cutils.WithPosition(26, 2, ui.Text("This is a InputBox. It takes user input just like a GUI")), + cutils.WithPosition(0, 1, ui.InputBox(20)), ), - ui.Div(cutils.WithPosition((width-30)/2, (h-4)/2, cutils.WithSize(30, 4, cutils.WithStyle(&ui.DivStyle{Background: colors.Blue}, ui.Div(ui.Text(text)))))), - // ui.Div( - // ui.Text("This is a InputBox. It takes user input and"), - // cutils.WithPosition(0, 2, ui.InputBox(20)), - // ), ) screen := osui.NewScreen(paginator) screen.Run() diff --git a/isKey/key.go b/isKey/key.go new file mode 100644 index 0000000..7eff812 --- /dev/null +++ b/isKey/key.go @@ -0,0 +1,33 @@ +package isKey + +func Enter(s string) bool { + return s == "\r" +} + +func Tab(s string) bool { + return s == "\t" +} + +func Backspace(s string) bool { + return s == "\x7f" || s == "\b" +} + +func Escape(s string) bool { + return s == "\x1b" +} + +func Up(s string) bool { + return s == "\x1b[A" +} + +func Down(s string) bool { + return s == "\x1b[B" +} + +func Right(s string) bool { + return s == "\x1b[C" +} + +func Left(s string) bool { + return s == "\x1b[D" +} diff --git a/ui/div.go b/ui/div.go index 0f6655d..7e388a6 100644 --- a/ui/div.go +++ b/ui/div.go @@ -5,6 +5,7 @@ import ( "github.com/orus-dev/osui" "github.com/orus-dev/osui/colors" + "github.com/orus-dev/osui/isKey" ) type DivStyle struct { @@ -39,12 +40,12 @@ func (d *DivComponent) Render() string { if data.Height == 0 { data.Height = d.Data.Height } - data.DefaultColor = colors.Combine(d.Style.Foreground, d.Style.Background) + data.DefaultColor = colors.Combine(d.Style.Background, d.Style.Foreground) data.Screen = d.Data.Screen osui.RenderOnFrame(c, &frame) } for i, f := range frame { - frame[i] = colors.Reset + d.Data.DefaultColor + colors.Combine(d.Style.Foreground, d.Style.Background) + f + colors.Reset + d.Data.DefaultColor + frame[i] = colors.Combine(d.Style.Foreground, d.Style.Background) + f + colors.Reset } return strings.Join(frame, "\n") } @@ -63,22 +64,19 @@ func (d *DivComponent) Run() { } func (d *DivComponent) Update(key string) bool { - switch key { - - case osui.Key.Up: + if isKey.Up(key) { d.updateActive(d.ActiveComponent - 1) - case osui.Key.Down: + } else if isKey.Down(key) { d.updateActive(d.ActiveComponent + 1) - default: + } else { if len(d.Components) > 0 { d.Components[d.ActiveComponent].GetComponentData().IsActive = d.Data.IsActive if d.Components[d.ActiveComponent].Update(key) { - return true - // if d.ActiveComponent < len(d.Components)-1 { - // d.updateActive(d.ActiveComponent + 1) - // } else { - // return true - // } + if d.ActiveComponent < len(d.Components)-1 { + d.updateActive(d.ActiveComponent + 1) + } else { + return true + } } } } diff --git a/ui/inputBox.go b/ui/inputBox.go index 6cf60be..0034c92 100644 --- a/ui/inputBox.go +++ b/ui/inputBox.go @@ -6,6 +6,7 @@ import ( "github.com/orus-dev/osui" "github.com/orus-dev/osui/colors" + "github.com/orus-dev/osui/isKey" ) type InputBoxStyle struct { @@ -19,6 +20,7 @@ type InputBoxComponent struct { Data osui.ComponentData Style *InputBoxStyle max_size uint + cursor uint InputData string } @@ -30,7 +32,7 @@ func (s InputBoxComponent) Render() string { osui.UseStyle(s.Style) if s.max_size > uint(len(s.InputData)) { return fmt.Sprintf( - " %s\n%s|%s%s|%s\n %s", + " %s\n%s│%s%s│%s\n %s", colors.Reset+s.Style.Outline+strings.Repeat("_", int(s.max_size))+colors.Reset, colors.Reset+s.Style.Outline, colors.Combine(s.Style.Foreground, s.Style.Background)+s.InputData+osui.LogicValue(s.Data.IsActive, s.Style.Cursor+"█"+colors.Combine(s.Style.Foreground, s.Style.Background), ""), @@ -41,10 +43,10 @@ func (s InputBoxComponent) Render() string { } return fmt.Sprintf( - " %s\n%s|%s%s\n %s", + " %s\n%s│%s%s\n %s", colors.Reset+s.Style.Outline+strings.Repeat("_", int(s.max_size))+colors.Reset, colors.Reset+s.Style.Outline, - colors.Combine(s.Style.Foreground, s.Style.Background)+s.InputData+osui.LogicValue(s.Data.IsActive, s.Style.Cursor+"█", "")+colors.Reset+colors.Reset, + colors.Combine(s.Style.Foreground, s.Style.Background)+s.InputData+osui.LogicValue(s.Data.IsActive, s.Style.Cursor+"█"+colors.Reset+colors.Reset, colors.Reset+s.Style.Outline+"|"+colors.Reset), colors.Reset+s.Data.DefaultColor, colors.Reset+s.Style.Outline+strings.Repeat("‾", int(s.max_size))+colors.Reset+s.Data.DefaultColor, ) @@ -67,15 +69,21 @@ func (s *InputBoxComponent) Read() error { } func (s *InputBoxComponent) Update(key string) bool { - switch key { - case osui.Key.Enter: + if isKey.Enter(key) { return true - case osui.Key.Backspace: + } else if isKey.Backspace(key) { if len(s.InputData) > 0 { s.InputData = s.InputData[:len(s.InputData)-1] } - - default: + } else if isKey.Left(key) { + if s.cursor > 0 { + s.cursor-- + } + } else if isKey.Right(key) { + if s.cursor < s.max_size { + s.cursor++ + } + } else { if len(key) == 1 { if int(s.max_size) > len(s.InputData) { s.InputData += key diff --git a/ui/paginator.go b/ui/paginator.go index e2530b7..2221ba0 100644 --- a/ui/paginator.go +++ b/ui/paginator.go @@ -6,6 +6,7 @@ import ( "github.com/orus-dev/osui" "github.com/orus-dev/osui/colors" + "github.com/orus-dev/osui/isKey" ) type PaginatorStyle struct { @@ -55,23 +56,22 @@ func (p *PaginatorComponent) Render() string { } func (p *PaginatorComponent) Update(key string) bool { - switch key { - case "\x1b[Z": + if key == "\x1b[Z" { if p.ActiveComponent > 0 { p.updateActive(p.ActiveComponent - 1) } else { p.updateActive(len(p.Components) - 1) } - case osui.Key.Tab: + } else if isKey.Tab(key) { if p.ActiveComponent < len(p.Components)-1 { p.updateActive(p.ActiveComponent + 1) } else { p.updateActive(0) } - case osui.Key.Escape: + } else if isKey.Escape(key) { fmt.Print("\n\n") return true - default: + } else { if len(p.Components) > 0 { p.Components[p.ActiveComponent].GetComponentData().IsActive = p.Data.IsActive if p.Components[p.ActiveComponent].Update(key) { diff --git a/ui/text.go b/ui/text.go index d246a29..b90ae14 100644 --- a/ui/text.go +++ b/ui/text.go @@ -2,7 +2,6 @@ package ui import ( "github.com/orus-dev/osui" - "github.com/orus-dev/osui/colors" ) type TextComponent struct { @@ -19,7 +18,7 @@ func (t *TextComponent) GetComponentData() *osui.ComponentData { } func (t TextComponent) Render() string { - return colors.Reset + t.Data.DefaultColor + t.Text + return t.Text } func (t *TextComponent) SetStyle(interface{}) {} diff --git a/utils.go b/utils.go index 666ce79..47a4895 100644 --- a/utils.go +++ b/utils.go @@ -16,25 +16,15 @@ import ( func renderLine(frameLine, line string, x int) string { result := []rune(frameLine) lineRunes := []rune(line) - frameLen := len(result) - lineLen := len(lineRunes) + x += strings.Count(string(result[:(x+1)]), "\t") + strings.Count(string(result[:(x+1)]), "\b") - cl := 0 - for i, r := range result { - if r == '\b' || r == '\t' { - cl++ - } - if i > x+1 { - break - } + for range strings.Count(line, "\t") + strings.Count(line, "\b") { + result = append(result, ' ') } - x += cl - for i := 0; i < lineLen && (x+i)-cl < frameLen; i++ { - if x+i > frameLen { - result = append(result, lineRunes[i]) - continue - } + frameLen := len(result) + + for i := 0; i < len(lineRunes) && x+i < frameLen; i++ { result[x+i] = lineRunes[i] } @@ -49,7 +39,6 @@ func RenderOnFrame(c Component, frame *[]string) { f, fa := CompressString(fo, "\b") line, la := CompressString(lo, "\t") r := renderLine(f, line, int(componentData.X)) - // (*frame)[int(componentData.Y)+i] = fmt.Sprintf("%#v\n", DecompressString(DecompressString(r, la, "\t"), fa, "\b")) (*frame)[int(componentData.Y)+i] = DecompressString(DecompressString(r, la, "\t"), fa, "\b") } } @@ -179,43 +168,6 @@ func LogicValueInt(b bool, _if, _else int) int { return _else } -func DebugRender(c Component) { - s := NewScreen(c) - s.CustomRender = func() { - - } - // data := c.GetComponentData() - fmt.Println(colors.Bold + "---OBJ RENDER---" + colors.Reset) - for _, line := range strings.Split(c.Render(), "\n") { - fmt.Printf("%#v\n", line) - } - s.Run() -} - -type Key_ = string - -type key struct { - Enter Key_ - Tab Key_ - Backspace Key_ - Escape Key_ - Up Key_ - Down Key_ - Right Key_ - Left Key_ -} - -var Key = key{ - Enter: "\r", - Tab: "\t", - Backspace: "\x7f", - Escape: "\x1b", - Up: "\x1b[A", - Down: "\x1b[B", - Right: "\x1b[C", - Left: "\x1b[D", -} - func GetTerminalSize() (int, int) { w, h, err := term.GetSize(0) if err != nil {