rendering and input

This commit is contained in:
2024-09-22 22:59:57 +02:00
parent c017eb24f4
commit 04262bcec6
6 changed files with 267 additions and 0 deletions
+70
View File
@@ -0,0 +1,70 @@
package ui
import (
"fmt"
"strings"
"github.com/orus-dev/osui"
)
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 {
return nil
}
type ComponentInputBox struct {
max_size uint
}
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)),
strings.Repeat("‾", int(s.max_size)),
)
}
func (s ComponentInputBox) Run(cw *osui.ComponentWrapper, a ...any) any {
for {
fmt.Printf("\x1B[%d;%dH", cw.Y+2, int(cw.X+2)+len(cw.Data))
key, err := osui.ReadKey()
if err != nil {
fmt.Println(err)
break
}
switch cw.Update(cw, key) {
case osui.UpdateOutput.Jmp:
continue
case osui.UpdateOutput.Exit:
fmt.Print("\n\n")
return cw.Data
}
switch key {
case osui.Key.Enter:
fmt.Print("\n\n")
return cw.Data
case osui.Key.Backspace:
if len(cw.Data) > 0 {
cw.Data = cw.Data[:len(cw.Data)-1]
}
default:
if int(s.max_size) > len(cw.Data) {
cw.Data += key
}
}
cw.Render.Render()
}
return cw.Data
}
+13
View File
@@ -0,0 +1,13 @@
package ui
import (
"github.com/orus-dev/osui"
)
func InputBox(max_size uint) *osui.ComponentWrapper {
return osui.NewComponent(ComponentInputBox{max_size: max_size})
}
func Text(text string) *osui.ComponentWrapper {
return osui.NewComponent(ComponentText{Text: text})
}