From 8ea250c1b1c6eb1ff8f1b3eaf7349df7e1712940 Mon Sep 17 00:00:00 2001 From: Leo Date: Sat, 12 Oct 2024 22:36:09 +0200 Subject: [PATCH] updated structure --- example/main.go | 5 +++-- isKey/key.go | 53 ------------------------------------------------- osui.go | 43 ++++++++++++++++++++------------------- ui/README.md | 38 ----------------------------------- ui/button.go | 8 ++++++-- ui/div.go | 31 +++++++++++++++-------------- ui/inputBox.go | 16 ++++++++++----- ui/menu.go | 30 ++++++++++++++-------------- ui/paginator.go | 13 ++++++++---- ui/text.go | 3 ++- 10 files changed, 85 insertions(+), 155 deletions(-) delete mode 100644 isKey/key.go delete mode 100644 ui/README.md diff --git a/example/main.go b/example/main.go index 0e755f3..6827a17 100644 --- a/example/main.go +++ b/example/main.go @@ -7,7 +7,8 @@ import ( func main() { screen := osui.NewScreen() - my_btn := ui.Menu(osui.Param{Style: osui.Style{Cursor: "> "}, OnClick: func() { - }}, "item 1", "item 2", "item 3", "item 4") + my_btn := ui.Menu(osui.Param{Style: osui.Style{Cursor: "> "}}, + "item 1", "item 2", "item 3", "item 4", + ) screen.Run(my_btn) } diff --git a/isKey/key.go b/isKey/key.go deleted file mode 100644 index 4f94d6a..0000000 --- a/isKey/key.go +++ /dev/null @@ -1,53 +0,0 @@ -package isKey - -func Enter(s string) bool { - return s == "\r" || s == "\n" -} - -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" -} - -func Char(s string, s1 string) bool { - return s == s1 -} - -func CtrlW(s string) bool { - return s == "\x17" -} - -func CtrlS(s string) bool { - return s == "\x13" -} - -func CtrlA(s string) bool { - return s == "\x01" -} - -func CtrlD(s string) bool { - return s == "\x04" -} \ No newline at end of file diff --git a/osui.go b/osui.go index 9f6077f..6b048be 100644 --- a/osui.go +++ b/osui.go @@ -50,14 +50,26 @@ func (s *Style) UseStyle() { } type Param struct { - Style Style `json:"style"` - X int `json:"y"` - Y int `json:"x"` - Width int `json:"width"` - Height int `json:"height"` - OnClick func() - OnUpdate func(ctx *Context) - Toggle bool `json:"useToggle"` + Style Style `json:"style"` + X int `json:"y"` + Y int `json:"x"` + Width int `json:"width"` + Height int `json:"height"` + OnClick func() + Keys map[string]func(string) bool + Toggle bool `json:"useToggle"` +} + +func (p *Param) SetDefaultBindings(keys map[string]func(string) bool) { + if p.Keys == nil { + p.Keys = keys + return + } + for k, f := range keys { + if _, ok := p.Keys[k]; !ok { + p.Keys[k] = f + } + } } func (p *Param) UseParam(c Component) Component { @@ -80,20 +92,11 @@ func (p *Param) UseParam(c Component) Component { data.OnClick = func() {} } - if p.OnUpdate != nil { - data.OnUpdate = p.OnUpdate - } else { - data.OnUpdate = func(ctx *Context) { ctx.Response = 0 } - } - data.Toggle = p.Toggle - return c -} + data.Keys = p.Keys -type Context struct { - Key string - Response int + return c } type ComponentData struct { @@ -105,7 +108,7 @@ type ComponentData struct { IsActive bool Style Style OnClick func() - OnUpdate func(ctx *Context) + Keys map[string]func(string) bool Toggle bool Screen *Screen } diff --git a/ui/README.md b/ui/README.md deleted file mode 100644 index b8624e6..0000000 --- a/ui/README.md +++ /dev/null @@ -1,38 +0,0 @@ -# Component template (use rename symbol) - -```go -type HelloStyle struct { - Fg string `defaults:"" type:"fg"` -} - -type HelloComponent struct { - Data osui.ComponentData - Style *HelloStyle - Name string -} - -func (h *HelloComponent) GetComponentData() *osui.ComponentData { - return &h.Data -} - -func (h *HelloComponent) Render() string { - osui.UseStyle(h.Style) - return "Hello " + h.Style.Fg + h.Name + colors.Reset + "!" -} - -func (h *HelloComponent) Update(key string) bool { - return false -} - -func (b *MenuComponent) Params(param MenuParams) *MenuComponent { - b.Style = osui.SetDefaults(¶m.Style).(*MenuStyle) - return b -} - -func Hello(name string) *HelloComponent { - return osui.NewComponent(&HelloComponent{ - Name: name, - Style: osui.SetDefaults(&HelloStyle{}).(*HelloStyle), - }).(*HelloComponent) -} -``` \ No newline at end of file diff --git a/ui/button.go b/ui/button.go index 7906e36..24bcdd4 100644 --- a/ui/button.go +++ b/ui/button.go @@ -7,7 +7,7 @@ import ( "github.com/orus-dev/osui" "github.com/orus-dev/osui/colors" - "github.com/orus-dev/osui/isKey" + "github.com/orus-dev/osui/keys" ) type ButtonComponent struct { @@ -49,7 +49,7 @@ func (b *ButtonComponent) Render() string { } func (b *ButtonComponent) Update(key string) bool { - if isKey.Enter(key) { + if f, ok := b.Data.Keys["click"]; ok && f(key) { b.Data.OnClick() if b.Data.Toggle { b.clicked = !b.clicked @@ -60,6 +60,7 @@ func (b *ButtonComponent) Update(key string) bool { time.Sleep(time.Millisecond * 120) b.clicked = false } + return false } @@ -68,6 +69,9 @@ func (b *ButtonComponent) GetComponentData() *osui.ComponentData { } func Button(param osui.Param, text string) *ButtonComponent { + param.SetDefaultBindings(map[string]func(string) bool{ + "click": keys.Enter, + }) return param.UseParam(&ButtonComponent{Text: text, Data: osui.ComponentData{ Width: 20, diff --git a/ui/div.go b/ui/div.go index 82f4a30..77c1a49 100644 --- a/ui/div.go +++ b/ui/div.go @@ -5,7 +5,7 @@ import ( "github.com/orus-dev/osui" "github.com/orus-dev/osui/colors" - "github.com/orus-dev/osui/isKey" + "github.com/orus-dev/osui/keys" ) type DivParams struct { @@ -65,26 +65,21 @@ func (d *DivComponent) Render() string { } func (d *DivComponent) Update(key string) bool { - if isKey.CtrlW(key) { + if f, ok := d.Data.Keys["up"]; ok && f(key) { d.updateActive(findClosestComponent(d.Components, d.ActiveComponent, "up")) - } else if isKey.CtrlS(key) { + } else if f, ok := d.Data.Keys["down"]; ok && f(key) { d.updateActive(findClosestComponent(d.Components, d.ActiveComponent, "down")) - } else if isKey.CtrlA(key) { + } else if f, ok := d.Data.Keys["left"]; ok && f(key) { d.updateActive(findClosestComponent(d.Components, d.ActiveComponent, "left")) - } else if isKey.CtrlD(key) { + } else if f, ok := d.Data.Keys["right"]; ok && f(key) { d.updateActive(findClosestComponent(d.Components, d.ActiveComponent, "right")) - } else { - if len(d.Components) > 0 { - d.Components[d.ActiveComponent].GetComponentData().IsActive = d.Data.IsActive - if d.Components[d.ActiveComponent].Update(key) { - if d.ActiveComponent < len(d.Components)-1 { - d.updateActive(d.ActiveComponent + 1) - } else { - return true - } - } + } else if len(d.Components) > 0 { + d.Components[d.ActiveComponent].GetComponentData().IsActive = d.Data.IsActive + if d.Components[d.ActiveComponent].Update(key) { + d.updateActive(findClosestComponent(d.Components, d.ActiveComponent, "down")) } } + return false } @@ -95,6 +90,12 @@ func (d *DivComponent) updateActive(newIndex int) { } func Div(param osui.Param, components ...osui.Component) *DivComponent { + param.SetDefaultBindings(map[string]func(string) bool{ + "up": keys.CtrlW, + "down": keys.CtrlS, + "left": keys.CtrlA, + "right": keys.CtrlD, + }) return param.UseParam(&DivComponent{ Components: components, }).(*DivComponent) diff --git a/ui/inputBox.go b/ui/inputBox.go index 5197494..7d0cd5f 100644 --- a/ui/inputBox.go +++ b/ui/inputBox.go @@ -6,7 +6,7 @@ import ( "github.com/orus-dev/osui" "github.com/orus-dev/osui/colors" - "github.com/orus-dev/osui/isKey" + "github.com/orus-dev/osui/keys" ) type InputBoxParams struct { @@ -51,17 +51,17 @@ func (s InputBoxComponent) Render() string { } func (s *InputBoxComponent) Update(key string) bool { - if isKey.Enter(key) { + if f, ok := s.Data.Keys["done"]; ok && f(key) { return true - } else if isKey.Backspace(key) { + } else if f, ok := s.Data.Keys["remove"]; ok && f(key) { if len(s.InputData) > 0 { s.InputData = s.InputData[:len(s.InputData)-1] } - } else if isKey.Left(key) { + } else if f, ok := s.Data.Keys["left"]; ok && f(key) { if s.cursor > 0 { s.cursor-- } - } else if isKey.Right(key) { + } else if f, ok := s.Data.Keys["right"]; ok && f(key) { if s.cursor < s.max_size { s.cursor++ } @@ -76,5 +76,11 @@ func (s *InputBoxComponent) Update(key string) bool { } func InputBox(param osui.Param, max_size uint) *InputBoxComponent { + param.SetDefaultBindings(map[string]func(string) bool{ + "done": keys.Enter, + "remove": keys.Backspace, + "left": keys.Left, + "right": keys.Right, + }) return param.UseParam(&InputBoxComponent{max_size: max_size}).(*InputBoxComponent) } diff --git a/ui/menu.go b/ui/menu.go index 116f926..30b181b 100644 --- a/ui/menu.go +++ b/ui/menu.go @@ -5,7 +5,7 @@ import ( "github.com/orus-dev/osui" "github.com/orus-dev/osui/colors" - "github.com/orus-dev/osui/isKey" + "github.com/orus-dev/osui/keys" ) type MenuComponent struct { @@ -41,32 +41,32 @@ func (m *MenuComponent) Render() string { } func (m *MenuComponent) Update(key string) bool { - ctx := osui.Context{Key: key} - m.Data.OnUpdate(&ctx) - if ctx.Response != 0 { - return false - } - - if isKey.Down(key) { - if m.SelectedItem+1 < len(m.Items) { - m.SelectedItem++ - } else { - m.SelectedItem = 0 - } - } else if isKey.Up(key) { + if f, ok := m.Data.Keys["up"]; ok && f(key) { if m.SelectedItem > 0 { m.SelectedItem-- } else { m.SelectedItem = len(m.Items) - 1 } - } else if isKey.Enter(key) { + } else if f, ok := m.Data.Keys["down"]; ok && f(key) { + if m.SelectedItem+1 < len(m.Items) { + m.SelectedItem++ + } else { + m.SelectedItem = 0 + } + } else if f, ok := m.Data.Keys["select"]; ok && f(key) { m.Data.OnClick() return true } + return false } func Menu(param osui.Param, items ...string) *MenuComponent { + param.SetDefaultBindings(map[string]func(string) bool{ + "up": keys.Up, + "down": keys.Down, + "select": keys.Enter, + }) return param.UseParam(&MenuComponent{ Items: items, }).(*MenuComponent) diff --git a/ui/paginator.go b/ui/paginator.go index 8a7291b..8d1365c 100644 --- a/ui/paginator.go +++ b/ui/paginator.go @@ -6,7 +6,7 @@ import ( "github.com/orus-dev/osui" "github.com/orus-dev/osui/colors" - "github.com/orus-dev/osui/isKey" + "github.com/orus-dev/osui/keys" ) type PaginatorParams struct { @@ -62,19 +62,19 @@ func (p *PaginatorComponent) Render() string { } func (p *PaginatorComponent) Update(key string) bool { - if key == "\x1b[Z" { + if f, ok := p.Data.Keys["previous"]; ok && f(key) { if p.ActiveComponent > 0 { p.updateActive(p.ActiveComponent - 1) } else { p.updateActive(len(p.Components) - 1) } - } else if isKey.Tab(key) { + } else if f, ok := p.Data.Keys["next"]; ok && f(key) { if p.ActiveComponent < len(p.Components)-1 { p.updateActive(p.ActiveComponent + 1) } else { p.updateActive(0) } - } else if isKey.Escape(key) { + } else if f, ok := p.Data.Keys["exit"]; ok && f(key) { fmt.Print("\n\n") return true } else { @@ -99,6 +99,11 @@ func (p *PaginatorComponent) updateActive(newIndex int) { } func Paginator(param osui.Param, pages ...osui.Component) *PaginatorComponent { + param.SetDefaultBindings(map[string]func(string) bool{ + "previous": keys.ShiftTab, + "next": keys.Tab, + "exit": keys.Escape, + }) return param.UseParam(&PaginatorComponent{ Components: pages, }).(*PaginatorComponent) diff --git a/ui/text.go b/ui/text.go index 60e21c9..5e0be55 100644 --- a/ui/text.go +++ b/ui/text.go @@ -9,7 +9,7 @@ type TextComponent struct { Text string } -func (t *TextComponent) Update(string) bool { +func (t *TextComponent) Update(key string) bool { return false } @@ -22,5 +22,6 @@ func (t TextComponent) Render() string { } func Text(param osui.Param, text string) *TextComponent { + param.SetDefaultBindings(map[string]func(string) bool{}) return param.UseParam(&TextComponent{Text: text}).(*TextComponent) }