Author SHA1 Message Date
selimaj-dev 3529f3dcc5 structure change 2024-09-24 07:24:53 +02:00
selimaj-dev 7546933e81 bug fix and Run all components 2024-09-23 16:42:40 +02:00
selimaj-dev f216e04b15 better structure 2024-09-23 10:19:50 +02:00
4 changed files with 132 additions and 162 deletions
+22 -125
View File
@@ -2,52 +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, ...any) any
}
type ComponentWrapper struct {
Component Component
X uint16 X uint16
Y uint16 Y uint16
Render *Render Screen *Screen
Update func(*ComponentWrapper, any) UpdateOutput_
Data string
} }
func (c *ComponentWrapper) Run(a ...any) any { type Component interface {
c.Render.Render() Render() string
return c.Component.Run(c, a) GetComponentData() *ComponentData
} }
func NewComponent(c Component) *ComponentWrapper { type Screen struct {
return &ComponentWrapper{ components []Component
Component: c, }
func NewScreen(c ...Component) *Screen {
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
} }
type Render struct { func (s *Screen) Render() error {
components []*ComponentWrapper
}
func NewRender() *Render {
return &Render{components: []*ComponentWrapper{}}
}
func (r *Render) Add(c *ComponentWrapper) *ComponentWrapper {
c.Render = 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 {
@@ -57,100 +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 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",
}
+29 -29
View File
@@ -7,64 +7,64 @@ import (
"github.com/orus-dev/osui" "github.com/orus-dev/osui"
) )
type ComponentText struct{ Text string } type TextComponent struct {
Data osui.ComponentData
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, a ...any) any { type InputBoxComponent struct {
return nil Data osui.ComponentData
}
type ComponentInputBox struct {
max_size uint max_size uint
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)),
cw.Data, s.InputData,
strings.Repeat(" ", int(s.max_size)-len(cw.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, a ...any) any { func (s *InputBoxComponent) Read() error {
for { for {
fmt.Printf("\x1B[%d;%dH", cw.Y+2, int(cw.X+2)+len(cw.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 cw.Data
}
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.InputData) > 0 {
cw.Data = cw.Data[:len(cw.Data)-1] s.InputData = s.InputData[:len(s.InputData)-1]
} }
default: default:
if int(s.max_size) > len(cw.Data) { if int(s.max_size) > len(s.InputData) {
cw.Data += key s.InputData += key
} }
} }
cw.Render.Render() s.Data.Screen.Render()
} }
return cw.Data return nil
} }
+4 -8
View File
@@ -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}
} }
+77
View File
@@ -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",
}