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
+7 -3
View File
@@ -11,8 +11,12 @@ import (
) )
func main() { func main() {
components := osui.NewRender()
components.Add(ui.Text("Hello, World!")) app := ui.Div(
components.Render() ui.Text("Hello, World!")
)
screen := osui.NewScreen(app)
screen.Run()
} }
``` ```
+16
View File
@@ -13,6 +13,7 @@ func main() {
func App() *PaginatorComponent { func App() *PaginatorComponent {
w, h := osui.GetTerminalSize() w, h := osui.GetTerminalSize()
text := Text("")
return Paginator( return Paginator(
// Page 1 // Page 1
Div( Div(
@@ -40,5 +41,20 @@ func App() *PaginatorComponent {
WithPosition(0, 0, Text("This is a Button. To click it press Enter. "+colors.Red+colors.Bold+"NOTE: This Button is inside a div")), WithPosition(0, 0, Text("This is a Button. To click it press Enter. "+colors.Red+colors.Bold+"NOTE: This Button is inside a div")),
WithPosition((w-20)/2, (h-3)/2, Button("This is a button", BtnParams{OnClick: func(bc *ButtonComponent) bool { return false }})), WithPosition((w-20)/2, (h-3)/2, Button("This is a button", BtnParams{OnClick: func(bc *ButtonComponent) bool { return false }})),
), ),
// Page 5
Div(
WithPosition(0, 2, Menu("Item 1", "Item 2", "Item 3").OnSelected(func(m *MenuComponent, b bool) {
if b {
text.Text = m.Items[m.SelectedItem]
}
})),
WithPosition(0, 6, Menu("Item A", "Item B", "Item C").OnSelected(func(m *MenuComponent, b bool) {
if b {
text.Text = m.Items[m.SelectedItem]
}
})),
text,
).OnKey(func(d *DivComponent, k string) string { return "" }),
) )
} }
+1 -8
View File
@@ -15,13 +15,6 @@ type ComponentData struct {
DefaultColor string DefaultColor string
IsActive bool IsActive bool
Screen *Screen Screen *Screen
Update func(string) bool
}
func NewComponent(c Component) Component {
data := c.GetComponentData()
data.Update = func(s string) bool { return c.Update(s) }
return c
} }
type Component interface { type Component interface {
@@ -71,7 +64,7 @@ func (s *Screen) Run() {
for { for {
s.Render() s.Render()
k, _ := ReadKey() k, _ := ReadKey()
if data.Update(k) { if s.component.Update(k) {
ShowCursor() ShowCursor()
return return
} }
+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 { func Button(text string, p ...BtnParams) *ButtonComponent {
if len(p) > 0 { if len(p) > 0 {
param := p[0] param := p[0]
return osui.NewComponent( return &ButtonComponent{Text: text, Style: osui.SetDefaults(&param.Style).(*ButtonStyle),
&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),
Data: osui.ComponentData{ Data: osui.ComponentData{
Width: 20, Width: osui.LogicValueInt(param.Width == 0, 20, param.Width),
Height: 1, Height: 1,
}, },
OnClick: func(bc *ButtonComponent) bool { return false }, OnClick: fnLogicValue(param.OnClick),
}).(*ButtonComponent) }
}
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 { type DivStyle struct {
Background string `defaults:"" type:"bg"` Background string `default:"" type:"bg"`
Foreground string `defaults:"" type:"fg"` Foreground string `default:"" type:"fg"`
} }
type DivComponent struct { type DivComponent struct {
@@ -18,6 +18,7 @@ type DivComponent struct {
Style *DivStyle Style *DivStyle
Components []osui.Component Components []osui.Component
ActiveComponent int ActiveComponent int
onKey func(*DivComponent, string) string
} }
func (d *DivComponent) GetComponentData() *osui.ComponentData { func (d *DivComponent) GetComponentData() *osui.ComponentData {
@@ -51,6 +52,30 @@ func (d *DivComponent) Render() string {
} }
func (d *DivComponent) Update(key string) bool { 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) { if isKey.Up(key) {
d.updateActive(d.ActiveComponent - 1) d.updateActive(d.ActiveComponent - 1)
} else if isKey.Down(key) { } 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 { func Div(components ...osui.Component) *DivComponent {
return osui.NewComponent(&DivComponent{ return &DivComponent{
Components: components, Components: components,
Style: osui.SetDefaults(&DivStyle{}).(*DivStyle), Style: osui.SetDefaults(&DivStyle{}).(*DivStyle),
}).(*DivComponent) }
} }
+5 -5
View File
@@ -10,10 +10,10 @@ import (
) )
type InputBoxStyle struct { type InputBoxStyle struct {
Background string `defaults:"" type:"bg"` Background string `default:"" type:"bg"`
Foreground string `defaults:"" type:"fg"` Foreground string `default:"" type:"fg"`
Outline string `defaults:"" type:"fg"` Outline string `default:"" type:"fg"`
Cursor string `defaults:"" type:"fg"` Cursor string `default:"" type:"fg"`
} }
type InputBoxComponent struct { type InputBoxComponent struct {
@@ -82,5 +82,5 @@ func (d *InputBoxComponent) SetStyle(c interface{}) {
} }
func InputBox(max_size uint) *InputBoxComponent { 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 { func Paginator(pages ...osui.Component) *PaginatorComponent {
return osui.NewComponent(&PaginatorComponent{ return &PaginatorComponent{
Components: pages, Components: pages,
Style: osui.SetDefaults(&PaginatorStyle{}).(*PaginatorStyle), 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 (t *TextComponent) SetStyle(interface{}) {}
func Text(text string) *TextComponent { 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) 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 { func WithPosition(x, y int, c osui.Component) osui.Component {
data := c.GetComponentData() data := c.GetComponentData()
data.X = x data.X = x
@@ -36,11 +44,3 @@ func WithSize(width, height int, c osui.Component) osui.Component {
data.Height = height data.Height = height
return c return c
} }
func fnLogicValue(f func(*ButtonComponent) bool) func(*ButtonComponent) bool {
if f == nil {
return func(*ButtonComponent) bool { return false }
} else {
return f
}
}
+1
View File
@@ -155,6 +155,7 @@ func SetDefaults(p interface{}) interface{} {
} }
func UseStyle(p interface{}) { func UseStyle(p interface{}) {
SetDefaults(p)
v := reflect.ValueOf(p) v := reflect.ValueOf(p)
if v.Kind() != reflect.Ptr { if v.Kind() != reflect.Ptr {
panic("SetStyle: expected a pointer to a struct") panic("SetStyle: expected a pointer to a struct")