Compare commits
1
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
f216e04b15 |
@@ -10,26 +10,34 @@ import (
|
|||||||
|
|
||||||
type Component interface {
|
type Component interface {
|
||||||
Render(*ComponentWrapper) string
|
Render(*ComponentWrapper) string
|
||||||
Run(*ComponentWrapper, ...any) any
|
Run(*ComponentWrapper) error
|
||||||
}
|
}
|
||||||
|
|
||||||
type ComponentWrapper struct {
|
type ComponentWrapper struct {
|
||||||
Component Component
|
Component Component
|
||||||
X uint16
|
X uint16
|
||||||
Y uint16
|
Y uint16
|
||||||
Render *Render
|
render *Render
|
||||||
Update func(*ComponentWrapper, any) UpdateOutput_
|
Update func(*ComponentWrapper, any) UpdateOutput_
|
||||||
Data string
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *ComponentWrapper) Run(a ...any) any {
|
func (c *ComponentWrapper) Run() {
|
||||||
c.Render.Render()
|
c.Component.Run(c)
|
||||||
return c.Component.Run(c, a)
|
}
|
||||||
|
|
||||||
|
func (cw *ComponentWrapper) Render(c Component) error {
|
||||||
|
cw.Component = c
|
||||||
|
return cw.render.Render()
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *ComponentWrapper) SetRender(r *Render) {
|
||||||
|
c.render = r
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewComponent(c Component) *ComponentWrapper {
|
func NewComponent(c Component) *ComponentWrapper {
|
||||||
return &ComponentWrapper{
|
return &ComponentWrapper{
|
||||||
Component: c,
|
Component: c,
|
||||||
|
Update: func(cw *ComponentWrapper, a any) UpdateOutput_ { return UpdateOutput.Continue },
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -42,7 +50,7 @@ func NewRender() *Render {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (r *Render) Add(c *ComponentWrapper) *ComponentWrapper {
|
func (r *Render) Add(c *ComponentWrapper) *ComponentWrapper {
|
||||||
c.Render = r
|
c.SetRender(r)
|
||||||
r.components = append(r.components, c)
|
r.components = append(r.components, c)
|
||||||
return c
|
return c
|
||||||
}
|
}
|
||||||
|
|||||||
+17
-14
@@ -7,33 +7,36 @@ import (
|
|||||||
"github.com/orus-dev/osui"
|
"github.com/orus-dev/osui"
|
||||||
)
|
)
|
||||||
|
|
||||||
type ComponentText struct{ Text string }
|
type ComponentText struct {
|
||||||
|
Text string
|
||||||
|
}
|
||||||
|
|
||||||
func (t ComponentText) Render(cw *osui.ComponentWrapper) string {
|
func (t ComponentText) Render(cw *osui.ComponentWrapper) string {
|
||||||
return t.Text
|
return t.Text
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t ComponentText) Run(cw *osui.ComponentWrapper, a ...any) any {
|
func (t ComponentText) Run(cw *osui.ComponentWrapper) error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
type ComponentInputBox struct {
|
type ComponentInputBox struct {
|
||||||
max_size uint
|
max_size uint
|
||||||
|
data string
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s ComponentInputBox) Render(cw *osui.ComponentWrapper) string {
|
func (s ComponentInputBox) Render(cw *osui.ComponentWrapper) string {
|
||||||
return fmt.Sprintf(
|
return fmt.Sprintf(
|
||||||
" %s\n|%s%s|\n %s",
|
" %s\n|%s%s|\n %s",
|
||||||
strings.Repeat("_", int(s.max_size)),
|
strings.Repeat("_", int(s.max_size)),
|
||||||
cw.Data,
|
s.data,
|
||||||
strings.Repeat(" ", int(s.max_size)-len(cw.Data)),
|
strings.Repeat(" ", int(s.max_size)-len(s.data)),
|
||||||
strings.Repeat("‾", int(s.max_size)),
|
strings.Repeat("‾", int(s.max_size)),
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s ComponentInputBox) Run(cw *osui.ComponentWrapper, a ...any) any {
|
func (s ComponentInputBox) Run(cw *osui.ComponentWrapper) error {
|
||||||
for {
|
for {
|
||||||
fmt.Printf("\x1B[%d;%dH", cw.Y+2, int(cw.X+2)+len(cw.Data))
|
fmt.Printf("\x1B[%d;%dH", cw.Y+2, int(cw.X+2)+len(s.data))
|
||||||
key, err := osui.ReadKey()
|
key, err := osui.ReadKey()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println(err)
|
fmt.Println(err)
|
||||||
@@ -45,26 +48,26 @@ func (s ComponentInputBox) Run(cw *osui.ComponentWrapper, a ...any) any {
|
|||||||
continue
|
continue
|
||||||
case osui.UpdateOutput.Exit:
|
case osui.UpdateOutput.Exit:
|
||||||
fmt.Print("\n\n")
|
fmt.Print("\n\n")
|
||||||
return cw.Data
|
return nil
|
||||||
|
|
||||||
}
|
}
|
||||||
switch key {
|
switch key {
|
||||||
|
|
||||||
case osui.Key.Enter:
|
case osui.Key.Enter:
|
||||||
fmt.Print("\n\n")
|
fmt.Print("\n\n")
|
||||||
return cw.Data
|
return nil
|
||||||
|
|
||||||
case osui.Key.Backspace:
|
case osui.Key.Backspace:
|
||||||
if len(cw.Data) > 0 {
|
if len(s.data) > 0 {
|
||||||
cw.Data = cw.Data[:len(cw.Data)-1]
|
s.data = s.data[:len(s.data)-1]
|
||||||
}
|
}
|
||||||
|
|
||||||
default:
|
default:
|
||||||
if int(s.max_size) > len(cw.Data) {
|
if int(s.max_size) > len(s.data) {
|
||||||
cw.Data += key
|
s.data += key
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
cw.Render.Render()
|
cw.Render(s)
|
||||||
}
|
}
|
||||||
return cw.Data
|
return nil
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user