menus
This commit is contained in:
@@ -11,8 +11,12 @@ import (
|
||||
)
|
||||
|
||||
func main() {
|
||||
components := osui.NewRender()
|
||||
components.Add(ui.Text("Hello, World!"))
|
||||
components.Render()
|
||||
|
||||
app := ui.Div(
|
||||
ui.Text("Hello, World!")
|
||||
)
|
||||
|
||||
screen := osui.NewScreen(app)
|
||||
screen.Run()
|
||||
}
|
||||
```
|
||||
@@ -13,6 +13,7 @@ func main() {
|
||||
|
||||
func App() *PaginatorComponent {
|
||||
w, h := osui.GetTerminalSize()
|
||||
text := Text("")
|
||||
return Paginator(
|
||||
// Page 1
|
||||
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((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 "" }),
|
||||
)
|
||||
}
|
||||
|
||||
@@ -15,13 +15,6 @@ type ComponentData struct {
|
||||
DefaultColor string
|
||||
IsActive bool
|
||||
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 {
|
||||
@@ -71,7 +64,7 @@ func (s *Screen) Run() {
|
||||
for {
|
||||
s.Render()
|
||||
k, _ := ReadKey()
|
||||
if data.Update(k) {
|
||||
if s.component.Update(k) {
|
||||
ShowCursor()
|
||||
return
|
||||
}
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
```
|
||||
+4
-6
@@ -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(¶m.Style).(*ButtonStyle),
|
||||
return &ButtonComponent{Text: text, Style: osui.SetDefaults(¶m.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,
|
||||
return &ButtonComponent{Text: text,
|
||||
Style: osui.SetDefaults(&ButtonStyle{}).(*ButtonStyle),
|
||||
Data: osui.ComponentData{
|
||||
Width: 20,
|
||||
Height: 1,
|
||||
},
|
||||
OnClick: func(bc *ButtonComponent) bool { return false },
|
||||
}).(*ButtonComponent)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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
@@ -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
@@ -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
@@ -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
@@ -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
@@ -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
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user