This commit is contained in:
2024-09-28 19:29:01 +02:00
parent 90f106f127
commit 8592b1cc50
12 changed files with 222 additions and 47 deletions
+37
View File
@@ -0,0 +1,37 @@
# 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 (h *HelloComponent) SetStyle(c interface{}) {
h.Style = osui.SetDefaults(c.(*HelloStyle)).(*HelloStyle)
}
func Hello(name string) *HelloComponent {
return osui.NewComponent(&HelloComponent{
Name: name,
Style: osui.SetDefaults(&HelloStyle{}).(*HelloStyle),
}).(*HelloComponent)
}
```
+14 -16
View File
@@ -99,23 +99,21 @@ type BtnParams struct {
func Button(text string, p ...BtnParams) *ButtonComponent {
if len(p) > 0 {
param := p[0]
return osui.NewComponent(
&ButtonComponent{Text: text, Style: osui.SetDefaults(&param.Style).(*ButtonStyle),
Data: osui.ComponentData{
Width: osui.LogicValueInt(param.Width == 0, 20, param.Width),
Height: 1,
},
OnClick: fnLogicValue(param.OnClick),
}).(*ButtonComponent)
}
return osui.NewComponent(
&ButtonComponent{Text: text,
Style: osui.SetDefaults(&ButtonStyle{}).(*ButtonStyle),
return &ButtonComponent{Text: text, Style: osui.SetDefaults(&param.Style).(*ButtonStyle),
Data: osui.ComponentData{
Width: 20,
Width: osui.LogicValueInt(param.Width == 0, 20, param.Width),
Height: 1,
},
OnClick: func(bc *ButtonComponent) bool { return false },
}).(*ButtonComponent)
OnClick: fnLogicValue(param.OnClick),
}
}
return &ButtonComponent{Text: text,
Style: osui.SetDefaults(&ButtonStyle{}).(*ButtonStyle),
Data: osui.ComponentData{
Width: 20,
Height: 1,
},
OnClick: func(bc *ButtonComponent) bool { return false },
}
}
+39 -4
View File
@@ -9,8 +9,8 @@ import (
)
type DivStyle struct {
Background string `defaults:"" type:"bg"`
Foreground string `defaults:"" type:"fg"`
Background string `default:"" type:"bg"`
Foreground string `default:"" type:"fg"`
}
type DivComponent struct {
@@ -18,6 +18,7 @@ type DivComponent struct {
Style *DivStyle
Components []osui.Component
ActiveComponent int
onKey func(*DivComponent, string) string
}
func (d *DivComponent) GetComponentData() *osui.ComponentData {
@@ -51,6 +52,30 @@ func (d *DivComponent) Render() string {
}
func (d *DivComponent) Update(key string) bool {
if d.onKey != nil {
o := d.onKey(d, key)
if o == "up" {
d.updateActive(d.ActiveComponent - 1)
} else if o == "down" {
d.updateActive(d.ActiveComponent + 1)
} else if o == "true" {
return true
} else if o == "false" {
return false
} 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
}
}
}
}
return false
}
if isKey.Up(key) {
d.updateActive(d.ActiveComponent - 1)
} else if isKey.Down(key) {
@@ -80,9 +105,19 @@ func (d *DivComponent) updateActive(newIndex int) {
}
}
func (d *DivComponent) OnKey(f func(*DivComponent, string) string) *DivComponent {
d.onKey = f
return d
}
func (d *DivComponent) AddTo(c *DivComponent) *DivComponent {
*c = *d
return d
}
func Div(components ...osui.Component) *DivComponent {
return osui.NewComponent(&DivComponent{
return &DivComponent{
Components: components,
Style: osui.SetDefaults(&DivStyle{}).(*DivStyle),
}).(*DivComponent)
}
}
+5 -5
View File
@@ -10,10 +10,10 @@ import (
)
type InputBoxStyle struct {
Background string `defaults:"" type:"bg"`
Foreground string `defaults:"" type:"fg"`
Outline string `defaults:"" type:"fg"`
Cursor string `defaults:"" type:"fg"`
Background string `default:"" type:"bg"`
Foreground string `default:"" type:"fg"`
Outline string `default:"" type:"fg"`
Cursor string `default:"" type:"fg"`
}
type InputBoxComponent struct {
@@ -82,5 +82,5 @@ func (d *InputBoxComponent) SetStyle(c interface{}) {
}
func InputBox(max_size uint) *InputBoxComponent {
return osui.NewComponent(&InputBoxComponent{max_size: max_size, Style: osui.SetDefaults(&InputBoxStyle{}).(*InputBoxStyle)}).(*InputBoxComponent)
return &InputBoxComponent{max_size: max_size, Style: osui.SetDefaults(&InputBoxStyle{}).(*InputBoxStyle)}
}
+91
View File
@@ -0,0 +1,91 @@
package ui
import (
"strings"
"github.com/orus-dev/osui"
"github.com/orus-dev/osui/colors"
"github.com/orus-dev/osui/isKey"
)
type MenuStyle struct {
Fg string `default:"" type:"fg"`
Bg string `default:"" type:"bg"`
SelectedFg string `default:"\x1b[34m" type:"fg"`
SelectedBg string `default:"" type:"bg"`
Cursor string `default:"> " type:"fg"`
CursorInactive string `default:" " type:"fg"`
}
type MenuComponent struct {
Data osui.ComponentData
Style *MenuStyle
Items []string
SelectedItem int
onSelected func(*MenuComponent, bool)
}
func (m *MenuComponent) GetComponentData() *osui.ComponentData {
return &m.Data
}
func (m *MenuComponent) Render() string {
osui.UseStyle(m.Style)
res := []string{}
cursor := m.Style.Cursor
if !m.Data.IsActive {
cursor = m.Style.CursorInactive
}
d, _ := osui.CompressString(m.Style.Cursor, "")
empty := strings.Repeat(" ", len(d))
for i, item := range m.Items {
if i == m.SelectedItem {
res = append(res, cursor+colors.Combine(m.Style.SelectedFg, m.Style.SelectedBg)+item+colors.Reset+m.Data.DefaultColor)
} else {
res = append(res, empty+colors.Combine(m.Style.Fg, m.Style.Bg)+item+colors.Reset+m.Data.DefaultColor)
}
}
return strings.Join(res, "\n")
}
func (m *MenuComponent) Update(key string) bool {
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 {
m.SelectedItem--
} else {
m.SelectedItem = len(m.Items) - 1
}
} else if isKey.Enter(key) {
m.onSelected(m, true)
return true
} else if isKey.Escape(key) {
m.onSelected(m, false)
return true
}
return false
}
func (m *MenuComponent) SetStyle(c interface{}) {
m.Style = osui.SetDefaults(c.(*MenuStyle)).(*MenuStyle)
}
func (m *MenuComponent) OnSelected(f func(*MenuComponent, bool)) *MenuComponent {
m.onSelected = f
return m
}
func Menu(items ...string) *MenuComponent {
return &MenuComponent{
Items: items,
Style: osui.SetDefaults(&MenuStyle{}).(*MenuStyle),
}
}
+2 -2
View File
@@ -97,8 +97,8 @@ func (p *PaginatorComponent) SetStyle(c interface{}) {
}
func Paginator(pages ...osui.Component) *PaginatorComponent {
return osui.NewComponent(&PaginatorComponent{
return &PaginatorComponent{
Components: pages,
Style: osui.SetDefaults(&PaginatorStyle{}).(*PaginatorStyle),
}).(*PaginatorComponent)
}
}
+1 -1
View File
@@ -24,5 +24,5 @@ func (t TextComponent) Render() string {
func (t *TextComponent) SetStyle(interface{}) {}
func Text(text string) *TextComponent {
return osui.NewComponent(&TextComponent{Text: text}).(*TextComponent)
return &TextComponent{Text: text}
}
+8 -8
View File
@@ -18,6 +18,14 @@ func centerText(text string, width int) string {
return fmt.Sprintf("%s%s%s", leftPadding, text, rightPadding)
}
func fnLogicValue(f func(*ButtonComponent) bool) func(*ButtonComponent) bool {
if f == nil {
return func(*ButtonComponent) bool { return false }
} else {
return f
}
}
func WithPosition(x, y int, c osui.Component) osui.Component {
data := c.GetComponentData()
data.X = x
@@ -36,11 +44,3 @@ func WithSize(width, height int, c osui.Component) osui.Component {
data.Height = height
return c
}
func fnLogicValue(f func(*ButtonComponent) bool) func(*ButtonComponent) bool {
if f == nil {
return func(*ButtonComponent) bool { return false }
} else {
return f
}
}