updated structure

This commit is contained in:
2024-10-12 22:36:09 +02:00
parent 039bc27999
commit 8ea250c1b1
10 changed files with 85 additions and 155 deletions
+3 -2
View File
@@ -7,7 +7,8 @@ import (
func main() { func main() {
screen := osui.NewScreen() screen := osui.NewScreen()
my_btn := ui.Menu(osui.Param{Style: osui.Style{Cursor: "> "}, OnClick: func() { my_btn := ui.Menu(osui.Param{Style: osui.Style{Cursor: "> "}},
}}, "item 1", "item 2", "item 3", "item 4") "item 1", "item 2", "item 3", "item 4",
)
screen.Run(my_btn) screen.Run(my_btn)
} }
-53
View File
@@ -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"
}
+16 -13
View File
@@ -56,10 +56,22 @@ type Param struct {
Width int `json:"width"` Width int `json:"width"`
Height int `json:"height"` Height int `json:"height"`
OnClick func() OnClick func()
OnUpdate func(ctx *Context) Keys map[string]func(string) bool
Toggle bool `json:"useToggle"` 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 { func (p *Param) UseParam(c Component) Component {
data := c.GetComponentData() data := c.GetComponentData()
@@ -80,20 +92,11 @@ func (p *Param) UseParam(c Component) Component {
data.OnClick = func() {} data.OnClick = func() {}
} }
if p.OnUpdate != nil {
data.OnUpdate = p.OnUpdate
} else {
data.OnUpdate = func(ctx *Context) { ctx.Response = 0 }
}
data.Toggle = p.Toggle data.Toggle = p.Toggle
return c data.Keys = p.Keys
}
type Context struct { return c
Key string
Response int
} }
type ComponentData struct { type ComponentData struct {
@@ -105,7 +108,7 @@ type ComponentData struct {
IsActive bool IsActive bool
Style Style Style Style
OnClick func() OnClick func()
OnUpdate func(ctx *Context) Keys map[string]func(string) bool
Toggle bool Toggle bool
Screen *Screen Screen *Screen
} }
-38
View File
@@ -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(&param.Style).(*MenuStyle)
return b
}
func Hello(name string) *HelloComponent {
return osui.NewComponent(&HelloComponent{
Name: name,
Style: osui.SetDefaults(&HelloStyle{}).(*HelloStyle),
}).(*HelloComponent)
}
```
+6 -2
View File
@@ -7,7 +7,7 @@ import (
"github.com/orus-dev/osui" "github.com/orus-dev/osui"
"github.com/orus-dev/osui/colors" "github.com/orus-dev/osui/colors"
"github.com/orus-dev/osui/isKey" "github.com/orus-dev/osui/keys"
) )
type ButtonComponent struct { type ButtonComponent struct {
@@ -49,7 +49,7 @@ func (b *ButtonComponent) Render() string {
} }
func (b *ButtonComponent) Update(key string) bool { func (b *ButtonComponent) Update(key string) bool {
if isKey.Enter(key) { if f, ok := b.Data.Keys["click"]; ok && f(key) {
b.Data.OnClick() b.Data.OnClick()
if b.Data.Toggle { if b.Data.Toggle {
b.clicked = !b.clicked b.clicked = !b.clicked
@@ -60,6 +60,7 @@ func (b *ButtonComponent) Update(key string) bool {
time.Sleep(time.Millisecond * 120) time.Sleep(time.Millisecond * 120)
b.clicked = false b.clicked = false
} }
return false return false
} }
@@ -68,6 +69,9 @@ func (b *ButtonComponent) GetComponentData() *osui.ComponentData {
} }
func Button(param osui.Param, text string) *ButtonComponent { func Button(param osui.Param, text string) *ButtonComponent {
param.SetDefaultBindings(map[string]func(string) bool{
"click": keys.Enter,
})
return param.UseParam(&ButtonComponent{Text: text, return param.UseParam(&ButtonComponent{Text: text,
Data: osui.ComponentData{ Data: osui.ComponentData{
Width: 20, Width: 20,
+14 -13
View File
@@ -5,7 +5,7 @@ import (
"github.com/orus-dev/osui" "github.com/orus-dev/osui"
"github.com/orus-dev/osui/colors" "github.com/orus-dev/osui/colors"
"github.com/orus-dev/osui/isKey" "github.com/orus-dev/osui/keys"
) )
type DivParams struct { type DivParams struct {
@@ -65,26 +65,21 @@ func (d *DivComponent) Render() string {
} }
func (d *DivComponent) Update(key string) bool { 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")) 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")) 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")) 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")) d.updateActive(findClosestComponent(d.Components, d.ActiveComponent, "right"))
} else { } else if len(d.Components) > 0 {
if len(d.Components) > 0 {
d.Components[d.ActiveComponent].GetComponentData().IsActive = d.Data.IsActive d.Components[d.ActiveComponent].GetComponentData().IsActive = d.Data.IsActive
if d.Components[d.ActiveComponent].Update(key) { if d.Components[d.ActiveComponent].Update(key) {
if d.ActiveComponent < len(d.Components)-1 { d.updateActive(findClosestComponent(d.Components, d.ActiveComponent, "down"))
d.updateActive(d.ActiveComponent + 1)
} else {
return true
}
}
} }
} }
return false return false
} }
@@ -95,6 +90,12 @@ func (d *DivComponent) updateActive(newIndex int) {
} }
func Div(param osui.Param, components ...osui.Component) *DivComponent { 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{ return param.UseParam(&DivComponent{
Components: components, Components: components,
}).(*DivComponent) }).(*DivComponent)
+11 -5
View File
@@ -6,7 +6,7 @@ import (
"github.com/orus-dev/osui" "github.com/orus-dev/osui"
"github.com/orus-dev/osui/colors" "github.com/orus-dev/osui/colors"
"github.com/orus-dev/osui/isKey" "github.com/orus-dev/osui/keys"
) )
type InputBoxParams struct { type InputBoxParams struct {
@@ -51,17 +51,17 @@ func (s InputBoxComponent) Render() string {
} }
func (s *InputBoxComponent) Update(key string) bool { func (s *InputBoxComponent) Update(key string) bool {
if isKey.Enter(key) { if f, ok := s.Data.Keys["done"]; ok && f(key) {
return true return true
} else if isKey.Backspace(key) { } else if f, ok := s.Data.Keys["remove"]; ok && f(key) {
if len(s.InputData) > 0 { if len(s.InputData) > 0 {
s.InputData = s.InputData[:len(s.InputData)-1] 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 { if s.cursor > 0 {
s.cursor-- s.cursor--
} }
} else if isKey.Right(key) { } else if f, ok := s.Data.Keys["right"]; ok && f(key) {
if s.cursor < s.max_size { if s.cursor < s.max_size {
s.cursor++ s.cursor++
} }
@@ -76,5 +76,11 @@ func (s *InputBoxComponent) Update(key string) bool {
} }
func InputBox(param osui.Param, max_size uint) *InputBoxComponent { 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) return param.UseParam(&InputBoxComponent{max_size: max_size}).(*InputBoxComponent)
} }
+15 -15
View File
@@ -5,7 +5,7 @@ import (
"github.com/orus-dev/osui" "github.com/orus-dev/osui"
"github.com/orus-dev/osui/colors" "github.com/orus-dev/osui/colors"
"github.com/orus-dev/osui/isKey" "github.com/orus-dev/osui/keys"
) )
type MenuComponent struct { type MenuComponent struct {
@@ -41,32 +41,32 @@ func (m *MenuComponent) Render() string {
} }
func (m *MenuComponent) Update(key string) bool { func (m *MenuComponent) Update(key string) bool {
ctx := osui.Context{Key: key} if f, ok := m.Data.Keys["up"]; ok && f(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 m.SelectedItem > 0 { if m.SelectedItem > 0 {
m.SelectedItem-- m.SelectedItem--
} else { } else {
m.SelectedItem = len(m.Items) - 1 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() m.Data.OnClick()
return true return true
} }
return false return false
} }
func Menu(param osui.Param, items ...string) *MenuComponent { 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{ return param.UseParam(&MenuComponent{
Items: items, Items: items,
}).(*MenuComponent) }).(*MenuComponent)
+9 -4
View File
@@ -6,7 +6,7 @@ import (
"github.com/orus-dev/osui" "github.com/orus-dev/osui"
"github.com/orus-dev/osui/colors" "github.com/orus-dev/osui/colors"
"github.com/orus-dev/osui/isKey" "github.com/orus-dev/osui/keys"
) )
type PaginatorParams struct { type PaginatorParams struct {
@@ -62,19 +62,19 @@ func (p *PaginatorComponent) Render() string {
} }
func (p *PaginatorComponent) Update(key string) bool { 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 { if p.ActiveComponent > 0 {
p.updateActive(p.ActiveComponent - 1) p.updateActive(p.ActiveComponent - 1)
} else { } else {
p.updateActive(len(p.Components) - 1) 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 { if p.ActiveComponent < len(p.Components)-1 {
p.updateActive(p.ActiveComponent + 1) p.updateActive(p.ActiveComponent + 1)
} else { } else {
p.updateActive(0) p.updateActive(0)
} }
} else if isKey.Escape(key) { } else if f, ok := p.Data.Keys["exit"]; ok && f(key) {
fmt.Print("\n\n") fmt.Print("\n\n")
return true return true
} else { } else {
@@ -99,6 +99,11 @@ func (p *PaginatorComponent) updateActive(newIndex int) {
} }
func Paginator(param osui.Param, pages ...osui.Component) *PaginatorComponent { 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{ return param.UseParam(&PaginatorComponent{
Components: pages, Components: pages,
}).(*PaginatorComponent) }).(*PaginatorComponent)
+2 -1
View File
@@ -9,7 +9,7 @@ type TextComponent struct {
Text string Text string
} }
func (t *TextComponent) Update(string) bool { func (t *TextComponent) Update(key string) bool {
return false return false
} }
@@ -22,5 +22,6 @@ func (t TextComponent) Render() string {
} }
func Text(param osui.Param, text string) *TextComponent { func Text(param osui.Param, text string) *TextComponent {
param.SetDefaultBindings(map[string]func(string) bool{})
return param.UseParam(&TextComponent{Text: text}).(*TextComponent) return param.UseParam(&TextComponent{Text: text}).(*TextComponent)
} }