Compare commits
2
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
7546933e81 | ||
|
|
f216e04b15 |
@@ -10,26 +10,34 @@ import (
|
||||
|
||||
type Component interface {
|
||||
Render(*ComponentWrapper) string
|
||||
Run(*ComponentWrapper, ...any) any
|
||||
Run(*ComponentWrapper) error
|
||||
}
|
||||
|
||||
type ComponentWrapper struct {
|
||||
Component Component
|
||||
X uint16
|
||||
Y uint16
|
||||
Render *Render
|
||||
render *Render
|
||||
Update func(*ComponentWrapper, any) UpdateOutput_
|
||||
Data string
|
||||
}
|
||||
|
||||
func (c *ComponentWrapper) Run(a ...any) any {
|
||||
c.Render.Render()
|
||||
return c.Component.Run(c, a)
|
||||
func (c *ComponentWrapper) Run() {
|
||||
c.Component.Run(c)
|
||||
}
|
||||
|
||||
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 {
|
||||
return &ComponentWrapper{
|
||||
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 {
|
||||
c.Render = r
|
||||
c.SetRender(r)
|
||||
r.components = append(r.components, c)
|
||||
return c
|
||||
}
|
||||
@@ -68,6 +76,12 @@ func (r *Render) Render() error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (r *Render) Run() {
|
||||
for _, c := range r.components {
|
||||
c.Run()
|
||||
}
|
||||
}
|
||||
|
||||
func Clear() {
|
||||
fmt.Print("\x1b[H\x1b[2J\x1b[3J")
|
||||
}
|
||||
|
||||
+17
-14
@@ -7,33 +7,36 @@ import (
|
||||
"github.com/orus-dev/osui"
|
||||
)
|
||||
|
||||
type ComponentText struct{ Text string }
|
||||
type ComponentText struct {
|
||||
Text string
|
||||
}
|
||||
|
||||
func (t ComponentText) Render(cw *osui.ComponentWrapper) string {
|
||||
return t.Text
|
||||
}
|
||||
|
||||
func (t ComponentText) Run(cw *osui.ComponentWrapper, a ...any) any {
|
||||
func (t ComponentText) Run(cw *osui.ComponentWrapper) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
type ComponentInputBox struct {
|
||||
max_size uint
|
||||
Data string
|
||||
}
|
||||
|
||||
func (s ComponentInputBox) Render(cw *osui.ComponentWrapper) string {
|
||||
return fmt.Sprintf(
|
||||
" %s\n|%s%s|\n %s",
|
||||
strings.Repeat("_", int(s.max_size)),
|
||||
cw.Data,
|
||||
strings.Repeat(" ", int(s.max_size)-len(cw.Data)),
|
||||
s.Data,
|
||||
strings.Repeat(" ", int(s.max_size)-len(s.Data)),
|
||||
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 {
|
||||
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()
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
@@ -45,26 +48,26 @@ func (s ComponentInputBox) Run(cw *osui.ComponentWrapper, a ...any) any {
|
||||
continue
|
||||
case osui.UpdateOutput.Exit:
|
||||
fmt.Print("\n\n")
|
||||
return cw.Data
|
||||
return nil
|
||||
|
||||
}
|
||||
switch key {
|
||||
|
||||
case osui.Key.Enter:
|
||||
fmt.Print("\n\n")
|
||||
return cw.Data
|
||||
return nil
|
||||
|
||||
case osui.Key.Backspace:
|
||||
if len(cw.Data) > 0 {
|
||||
cw.Data = cw.Data[:len(cw.Data)-1]
|
||||
if len(s.Data) > 0 {
|
||||
s.Data = s.Data[:len(s.Data)-1]
|
||||
}
|
||||
|
||||
default:
|
||||
if int(s.max_size) > len(cw.Data) {
|
||||
cw.Data += key
|
||||
if int(s.max_size) > len(s.Data) {
|
||||
s.Data += key
|
||||
}
|
||||
}
|
||||
cw.Render.Render()
|
||||
cw.Render(s)
|
||||
}
|
||||
return cw.Data
|
||||
return nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user