Compare commits
1
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
3529f3dcc5 |
@@ -2,60 +2,40 @@ package osui
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"golang.org/x/term"
|
"golang.org/x/term"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Component interface {
|
type ComponentData struct {
|
||||||
Render(*ComponentWrapper) string
|
|
||||||
Run(*ComponentWrapper) error
|
|
||||||
}
|
|
||||||
|
|
||||||
type ComponentWrapper struct {
|
|
||||||
Component Component
|
|
||||||
X uint16
|
X uint16
|
||||||
Y uint16
|
Y uint16
|
||||||
render *Render
|
Screen *Screen
|
||||||
Update func(*ComponentWrapper, any) UpdateOutput_
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *ComponentWrapper) Run() {
|
type Component interface {
|
||||||
c.Component.Run(c)
|
Render() string
|
||||||
|
GetComponentData() *ComponentData
|
||||||
}
|
}
|
||||||
|
|
||||||
func (cw *ComponentWrapper) Render(c Component) error {
|
type Screen struct {
|
||||||
cw.Component = c
|
components []Component
|
||||||
return cw.render.Render()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *ComponentWrapper) SetRender(r *Render) {
|
func NewScreen(c ...Component) *Screen {
|
||||||
c.render = r
|
s := &Screen{components: c}
|
||||||
|
for _, component := range s.components {
|
||||||
|
componentData := component.GetComponentData()
|
||||||
|
if componentData.Screen == nil {
|
||||||
|
componentData.Screen = s
|
||||||
|
} else {
|
||||||
|
componentData.Screen.components = s.components
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return s
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewComponent(c Component) *ComponentWrapper {
|
func (s *Screen) Render() error {
|
||||||
return &ComponentWrapper{
|
|
||||||
Component: c,
|
|
||||||
Update: func(cw *ComponentWrapper, a any) UpdateOutput_ { return UpdateOutput.Continue },
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
type Render struct {
|
|
||||||
components []*ComponentWrapper
|
|
||||||
}
|
|
||||||
|
|
||||||
func NewRender() *Render {
|
|
||||||
return &Render{components: []*ComponentWrapper{}}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (r *Render) Add(c *ComponentWrapper) *ComponentWrapper {
|
|
||||||
c.SetRender(r)
|
|
||||||
r.components = append(r.components, c)
|
|
||||||
return c
|
|
||||||
}
|
|
||||||
|
|
||||||
func (r *Render) Render() error {
|
|
||||||
Clear()
|
Clear()
|
||||||
width, height, err := term.GetSize(0)
|
width, height, err := term.GetSize(0)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -65,106 +45,9 @@ func (r *Render) Render() error {
|
|||||||
for i := 0; i < height; i++ {
|
for i := 0; i < height; i++ {
|
||||||
frame[i] = strings.Repeat(" ", width)
|
frame[i] = strings.Repeat(" ", width)
|
||||||
}
|
}
|
||||||
for _, c := range r.components {
|
for _, component := range s.components {
|
||||||
for i, line := range strings.Split(c.Component.Render(c), "\n") {
|
RenderOnFrame(component, &frame)
|
||||||
if int(c.Y)+i < len(frame) {
|
|
||||||
frame[int(c.Y)+i] = renderLine(frame[int(c.Y)+i], line, int(c.X))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
fmt.Print(strings.Join(frame, "\n"))
|
fmt.Print(strings.Join(frame, "\n"))
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *Render) Run() {
|
|
||||||
for _, c := range r.components {
|
|
||||||
c.Run()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func Clear() {
|
|
||||||
fmt.Print("\x1b[H\x1b[2J\x1b[3J")
|
|
||||||
}
|
|
||||||
|
|
||||||
func renderLine(frameLine, line string, x int) string {
|
|
||||||
result := ""
|
|
||||||
if x >= len(frameLine) {
|
|
||||||
return result
|
|
||||||
}
|
|
||||||
|
|
||||||
lChars := []rune(line)
|
|
||||||
for i, c := range frameLine {
|
|
||||||
if i >= x && len(lChars) > i-x {
|
|
||||||
result += string(lChars[i-x])
|
|
||||||
} else {
|
|
||||||
result += string(c)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return result
|
|
||||||
}
|
|
||||||
|
|
||||||
func ReadKey() (string, error) {
|
|
||||||
oldState, err := term.MakeRaw(int(os.Stdin.Fd()))
|
|
||||||
if err != nil {
|
|
||||||
return "", err
|
|
||||||
}
|
|
||||||
defer term.Restore(int(os.Stdin.Fd()), oldState)
|
|
||||||
var b [3]byte
|
|
||||||
n, err := os.Stdin.Read(b[:])
|
|
||||||
if err != nil {
|
|
||||||
return "", err
|
|
||||||
}
|
|
||||||
return string(b[:n]), nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func KeyUpdate(keys map[string]UpdateOutput_) func(c *ComponentWrapper, key any) UpdateOutput_ {
|
|
||||||
return func(c *ComponentWrapper, k any) UpdateOutput_ {
|
|
||||||
action, ok := keys[fmt.Sprint(k)]
|
|
||||||
if ok {
|
|
||||||
return action
|
|
||||||
} else {
|
|
||||||
return UpdateOutput.Continue
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
type UpdateOutput_ = int
|
|
||||||
|
|
||||||
type updateOutput struct {
|
|
||||||
/*Continue the default check*/
|
|
||||||
Continue UpdateOutput_
|
|
||||||
/*Jump to the next check*/
|
|
||||||
Jmp UpdateOutput_
|
|
||||||
/*Leave the loop and stop the function*/
|
|
||||||
Exit UpdateOutput_
|
|
||||||
}
|
|
||||||
|
|
||||||
var UpdateOutput = updateOutput{Continue: 0, Jmp: 1, Exit: 2}
|
|
||||||
|
|
||||||
/*
|
|
||||||
Keys
|
|
||||||
*/
|
|
||||||
|
|
||||||
type Key_ = string
|
|
||||||
|
|
||||||
type key struct {
|
|
||||||
Enter Key_
|
|
||||||
Tab Key_
|
|
||||||
Backspace Key_
|
|
||||||
Escape Key_
|
|
||||||
Up Key_
|
|
||||||
Down Key_
|
|
||||||
Right Key_
|
|
||||||
Left Key_
|
|
||||||
}
|
|
||||||
|
|
||||||
var Key = key{
|
|
||||||
Enter: "\r",
|
|
||||||
Tab: "\t",
|
|
||||||
Backspace: "\x7f",
|
|
||||||
Escape: "\x1b",
|
|
||||||
Up: "\x1b[A",
|
|
||||||
Down: "\x1b[B",
|
|
||||||
Right: "\x1b[C",
|
|
||||||
Left: "\x1b[D",
|
|
||||||
}
|
|
||||||
|
|||||||
+25
-28
@@ -7,67 +7,64 @@ import (
|
|||||||
"github.com/orus-dev/osui"
|
"github.com/orus-dev/osui"
|
||||||
)
|
)
|
||||||
|
|
||||||
type ComponentText struct {
|
type TextComponent struct {
|
||||||
|
Data osui.ComponentData
|
||||||
Text string
|
Text string
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t ComponentText) Render(cw *osui.ComponentWrapper) string {
|
func (t *TextComponent) GetComponentData() *osui.ComponentData {
|
||||||
|
return &t.Data
|
||||||
|
}
|
||||||
|
|
||||||
|
func (t TextComponent) Render() string {
|
||||||
return t.Text
|
return t.Text
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t ComponentText) Run(cw *osui.ComponentWrapper) error {
|
type InputBoxComponent struct {
|
||||||
return nil
|
Data osui.ComponentData
|
||||||
}
|
|
||||||
|
|
||||||
type ComponentInputBox struct {
|
|
||||||
max_size uint
|
max_size uint
|
||||||
Data string
|
InputData string
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s ComponentInputBox) Render(cw *osui.ComponentWrapper) string {
|
func (s *InputBoxComponent) GetComponentData() *osui.ComponentData {
|
||||||
|
return &s.Data
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s InputBoxComponent) Render() 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)),
|
||||||
s.Data,
|
s.InputData,
|
||||||
strings.Repeat(" ", int(s.max_size)-len(s.Data)),
|
strings.Repeat(" ", int(s.max_size)-len(s.InputData)),
|
||||||
strings.Repeat("‾", int(s.max_size)),
|
strings.Repeat("‾", int(s.max_size)),
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s ComponentInputBox) Run(cw *osui.ComponentWrapper) error {
|
func (s *InputBoxComponent) Read() error {
|
||||||
for {
|
for {
|
||||||
fmt.Printf("\x1B[%d;%dH", cw.Y+2, int(cw.X+2)+len(s.Data))
|
fmt.Printf("\x1B[%d;%dH", s.Data.Y+2, int(s.Data.X+2)+len(s.InputData))
|
||||||
key, err := osui.ReadKey()
|
key, err := osui.ReadKey()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
fmt.Print("\n\n")
|
||||||
fmt.Println(err)
|
fmt.Println(err)
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
switch cw.Update(cw, key) {
|
|
||||||
|
|
||||||
case osui.UpdateOutput.Jmp:
|
|
||||||
continue
|
|
||||||
case osui.UpdateOutput.Exit:
|
|
||||||
fmt.Print("\n\n")
|
|
||||||
return nil
|
|
||||||
|
|
||||||
}
|
|
||||||
switch key {
|
switch key {
|
||||||
|
|
||||||
case osui.Key.Enter:
|
case osui.Key.Enter:
|
||||||
fmt.Print("\n\n")
|
fmt.Print("\n\n")
|
||||||
return nil
|
return nil
|
||||||
|
|
||||||
case osui.Key.Backspace:
|
case osui.Key.Backspace:
|
||||||
if len(s.Data) > 0 {
|
if len(s.InputData) > 0 {
|
||||||
s.Data = s.Data[:len(s.Data)-1]
|
s.InputData = s.InputData[:len(s.InputData)-1]
|
||||||
}
|
}
|
||||||
|
|
||||||
default:
|
default:
|
||||||
if int(s.max_size) > len(s.Data) {
|
if int(s.max_size) > len(s.InputData) {
|
||||||
s.Data += key
|
s.InputData += key
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
cw.Render(s)
|
s.Data.Screen.Render()
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,13 +1,9 @@
|
|||||||
package ui
|
package ui
|
||||||
|
|
||||||
import (
|
func Text(text string) *TextComponent {
|
||||||
"github.com/orus-dev/osui"
|
return &TextComponent{Text: text}
|
||||||
)
|
|
||||||
|
|
||||||
func InputBox(max_size uint) *osui.ComponentWrapper {
|
|
||||||
return osui.NewComponent(ComponentInputBox{max_size: max_size})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func Text(text string) *osui.ComponentWrapper {
|
func InputBox(max_size uint) *InputBoxComponent {
|
||||||
return osui.NewComponent(ComponentText{Text: text})
|
return &InputBoxComponent{max_size: max_size}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,77 @@
|
|||||||
|
package osui
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"os"
|
||||||
|
"strings"
|
||||||
|
|
||||||
|
"golang.org/x/term"
|
||||||
|
)
|
||||||
|
|
||||||
|
func renderLine(frameLine, line string, x int) string {
|
||||||
|
result := ""
|
||||||
|
if x >= len(frameLine) {
|
||||||
|
return result
|
||||||
|
}
|
||||||
|
|
||||||
|
lChars := []rune(line)
|
||||||
|
for i, c := range frameLine {
|
||||||
|
if i >= x && len(lChars) > i-x {
|
||||||
|
result += string(lChars[i-x])
|
||||||
|
} else {
|
||||||
|
result += string(c)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return result
|
||||||
|
}
|
||||||
|
|
||||||
|
func RenderOnFrame(c Component, frame *[]string) {
|
||||||
|
componentData := c.GetComponentData()
|
||||||
|
for i, line := range strings.Split(c.Render(), "\n") {
|
||||||
|
if int(componentData.Y)+i < len(*frame) {
|
||||||
|
(*frame)[int(componentData.Y)+i] = renderLine((*frame)[int(componentData.Y)+i], line, int(componentData.X))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func ReadKey() (string, error) {
|
||||||
|
oldState, err := term.MakeRaw(int(os.Stdin.Fd()))
|
||||||
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
defer term.Restore(int(os.Stdin.Fd()), oldState)
|
||||||
|
var b [3]byte
|
||||||
|
n, err := os.Stdin.Read(b[:])
|
||||||
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
return string(b[:n]), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func Clear() {
|
||||||
|
fmt.Print("\x1b[H\x1b[2J\x1b[3J")
|
||||||
|
}
|
||||||
|
|
||||||
|
type Key_ = string
|
||||||
|
|
||||||
|
type key struct {
|
||||||
|
Enter Key_
|
||||||
|
Tab Key_
|
||||||
|
Backspace Key_
|
||||||
|
Escape Key_
|
||||||
|
Up Key_
|
||||||
|
Down Key_
|
||||||
|
Right Key_
|
||||||
|
Left Key_
|
||||||
|
}
|
||||||
|
|
||||||
|
var Key = key{
|
||||||
|
Enter: "\r",
|
||||||
|
Tab: "\t",
|
||||||
|
Backspace: "\x7f",
|
||||||
|
Escape: "\x1b",
|
||||||
|
Up: "\x1b[A",
|
||||||
|
Down: "\x1b[B",
|
||||||
|
Right: "\x1b[C",
|
||||||
|
Left: "\x1b[D",
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user