Compare commits
5
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
8f375efe41 | ||
|
|
905a77f8b0 | ||
|
|
3529f3dcc5 | ||
|
|
7546933e81 | ||
|
|
f216e04b15 |
@@ -0,0 +1,16 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/orus-dev/osui"
|
||||||
|
"github.com/orus-dev/osui/styles"
|
||||||
|
"github.com/orus-dev/osui/ui"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
colors := ui.Text(
|
||||||
|
styles.Red + "Red!\n" + styles.Green + "Green!\n" + styles.Blue + "Blue!" + styles.Reset,
|
||||||
|
)
|
||||||
|
paginator := ui.Paginator(ui.Div(colors), ui.Div(ui.InputBox(30)))
|
||||||
|
screen := osui.NewScreen(paginator)
|
||||||
|
screen.Run()
|
||||||
|
}
|
||||||
@@ -2,155 +2,62 @@ 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
|
Width int
|
||||||
Update func(*ComponentWrapper, any) UpdateOutput_
|
Height int
|
||||||
Data string
|
IsActive bool
|
||||||
|
Screen *Screen
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *ComponentWrapper) Run(a ...any) any {
|
type Component interface {
|
||||||
c.Render.Render()
|
Render() string
|
||||||
return c.Component.Run(c, a)
|
GetComponentData() *ComponentData
|
||||||
|
Update(string) bool
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewComponent(c Component) *ComponentWrapper {
|
type Screen struct {
|
||||||
return &ComponentWrapper{
|
component Component
|
||||||
Component: c,
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
type Render struct {
|
func NewScreen(c Component) *Screen {
|
||||||
components []*ComponentWrapper
|
HideCursor()
|
||||||
|
s := &Screen{component: c}
|
||||||
|
return s
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewRender() *Render {
|
func (s *Screen) Render() error {
|
||||||
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 {
|
||||||
return fmt.Errorf("error getting the terminal size: %s", err)
|
return fmt.Errorf("error getting the terminal size: %s", err)
|
||||||
}
|
}
|
||||||
frame := make([]string, height)
|
frame := NewFrame(width, height)
|
||||||
for i := 0; i < height; i++ {
|
data := s.component.GetComponentData()
|
||||||
frame[i] = strings.Repeat(" ", width)
|
data.Width = width
|
||||||
}
|
data.Height = height
|
||||||
for _, c := range r.components {
|
data.Screen = s
|
||||||
for i, line := range strings.Split(c.Component.Render(c), "\n") {
|
data.IsActive = true
|
||||||
if int(c.Y)+i < len(frame) {
|
RenderOnFrame(s.component, &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() {
|
func (s *Screen) Run() {
|
||||||
fmt.Print("\x1b[H\x1b[2J\x1b[3J")
|
s.component.GetComponentData().Screen = s
|
||||||
}
|
s.component.Update("OSUI:RENDER")
|
||||||
|
for {
|
||||||
func renderLine(frameLine, line string, x int) string {
|
s.Render()
|
||||||
result := ""
|
k, _ := ReadKey()
|
||||||
if x >= len(frameLine) {
|
if s.component.Update(k) {
|
||||||
return result
|
ShowCursor()
|
||||||
}
|
return
|
||||||
|
|
||||||
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",
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -0,0 +1,36 @@
|
|||||||
|
package styles
|
||||||
|
|
||||||
|
import "fmt"
|
||||||
|
|
||||||
|
const (
|
||||||
|
Reset = "\033[0m"
|
||||||
|
Bold = "\033[1m"
|
||||||
|
Underline = "\033[4m"
|
||||||
|
Italic = "\033[3m"
|
||||||
|
Reverse = "\033[7m"
|
||||||
|
Strike = "\033[9m"
|
||||||
|
|
||||||
|
// Font Colors
|
||||||
|
Black = "\033[30m"
|
||||||
|
Red = "\033[31m"
|
||||||
|
Green = "\033[32m"
|
||||||
|
Yellow = "\033[33m"
|
||||||
|
Blue = "\033[34m"
|
||||||
|
Magenta = "\033[35m"
|
||||||
|
Cyan = "\033[36m"
|
||||||
|
White = "\033[37m"
|
||||||
|
|
||||||
|
// Background Colors
|
||||||
|
BgBlack = "\033[40m"
|
||||||
|
BgRed = "\033[41m"
|
||||||
|
BgGreen = "\033[42m"
|
||||||
|
BgYellow = "\033[43m"
|
||||||
|
BgBlue = "\033[44m"
|
||||||
|
BgMagenta = "\033[45m"
|
||||||
|
BgCyan = "\033[46m"
|
||||||
|
BgWhite = "\033[47m"
|
||||||
|
)
|
||||||
|
|
||||||
|
func Rgb(r, g, b uint8) string {
|
||||||
|
return fmt.Sprintf("\x1b[38;2;%d;%d;%dm", r, g, b)
|
||||||
|
}
|
||||||
@@ -1,70 +0,0 @@
|
|||||||
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
|
|
||||||
}
|
|
||||||
@@ -0,0 +1,81 @@
|
|||||||
|
package ui
|
||||||
|
|
||||||
|
import (
|
||||||
|
"strings"
|
||||||
|
|
||||||
|
"github.com/orus-dev/osui"
|
||||||
|
)
|
||||||
|
|
||||||
|
type DivComponent struct {
|
||||||
|
Data osui.ComponentData
|
||||||
|
Components []osui.Component
|
||||||
|
ActiveComponent int
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p *DivComponent) GetComponentData() *osui.ComponentData {
|
||||||
|
return &p.Data
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p DivComponent) Render() string {
|
||||||
|
frame := osui.NewFrame(p.Data.Width, p.Data.Height)
|
||||||
|
for _, c := range p.Components {
|
||||||
|
data := c.GetComponentData()
|
||||||
|
data.Width = p.Data.Width
|
||||||
|
data.Height = p.Data.Height
|
||||||
|
data.Screen = p.Data.Screen
|
||||||
|
osui.RenderOnFrame(c, &frame)
|
||||||
|
}
|
||||||
|
return strings.Join(frame, "\n")
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p *DivComponent) Run() {
|
||||||
|
for _, c := range p.Components {
|
||||||
|
d := c.GetComponentData()
|
||||||
|
d.Screen = p.Data.Screen
|
||||||
|
}
|
||||||
|
for {
|
||||||
|
key, _ := osui.ReadKey()
|
||||||
|
if p.Update(key) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p *DivComponent) Update(key string) bool {
|
||||||
|
switch key {
|
||||||
|
|
||||||
|
case "":
|
||||||
|
p.updateActive(p.ActiveComponent)
|
||||||
|
case osui.Key.Up:
|
||||||
|
p.updateActive(p.ActiveComponent - 1)
|
||||||
|
case osui.Key.Down:
|
||||||
|
p.updateActive(p.ActiveComponent + 1)
|
||||||
|
default:
|
||||||
|
if len(p.Components) > 0 {
|
||||||
|
p.Components[p.ActiveComponent].GetComponentData().IsActive = p.Data.IsActive
|
||||||
|
if p.Components[p.ActiveComponent].Update(key) {
|
||||||
|
if p.ActiveComponent < len(p.Components)-1 {
|
||||||
|
p.updateActive(p.ActiveComponent + 1)
|
||||||
|
} else {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p *DivComponent) updateActive(newIndex int) {
|
||||||
|
if newIndex >= 0 && newIndex < len(p.Components) && len(p.Components) > 0 {
|
||||||
|
p.Components[p.ActiveComponent].GetComponentData().IsActive = false
|
||||||
|
p.ActiveComponent = newIndex
|
||||||
|
p.Components[p.ActiveComponent].GetComponentData().IsActive = p.Data.IsActive
|
||||||
|
p.Components[p.ActiveComponent].Update("")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func Div(components ...osui.Component) *DivComponent {
|
||||||
|
return &DivComponent{
|
||||||
|
Components: components,
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,76 @@
|
|||||||
|
package ui
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"strings"
|
||||||
|
|
||||||
|
"github.com/orus-dev/osui"
|
||||||
|
)
|
||||||
|
|
||||||
|
type InputBoxComponent struct {
|
||||||
|
Data osui.ComponentData
|
||||||
|
max_size uint
|
||||||
|
InputData string
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *InputBoxComponent) GetComponentData() *osui.ComponentData {
|
||||||
|
return &s.Data
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s InputBoxComponent) Render() string {
|
||||||
|
if s.max_size > uint(len(s.InputData)) {
|
||||||
|
return fmt.Sprintf(
|
||||||
|
" %s\n|%s%s|\n %s",
|
||||||
|
strings.Repeat("_", int(s.max_size)),
|
||||||
|
s.InputData+osui.LogicValue(s.Data.IsActive, "█", ""),
|
||||||
|
strings.Repeat(" ", int(s.max_size)-len(s.InputData)-osui.LogicValueInt(s.Data.IsActive, 1, 0)),
|
||||||
|
strings.Repeat("‾", int(s.max_size)),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
return fmt.Sprintf(
|
||||||
|
" %s\n|%s\n %s",
|
||||||
|
strings.Repeat("_", int(s.max_size)),
|
||||||
|
s.InputData+osui.LogicValue(s.Data.IsActive, "█", "|"),
|
||||||
|
strings.Repeat("‾", int(s.max_size)),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *InputBoxComponent) Read() error {
|
||||||
|
for {
|
||||||
|
key, err := osui.ReadKey()
|
||||||
|
if err != nil {
|
||||||
|
fmt.Print("\n\n")
|
||||||
|
fmt.Println(err)
|
||||||
|
break
|
||||||
|
}
|
||||||
|
if s.Update(key) {
|
||||||
|
fmt.Print("\n\n")
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *InputBoxComponent) Update(key string) bool {
|
||||||
|
switch key {
|
||||||
|
case "":
|
||||||
|
case osui.Key.Enter:
|
||||||
|
return true
|
||||||
|
case osui.Key.Backspace:
|
||||||
|
if len(s.InputData) > 0 {
|
||||||
|
s.InputData = s.InputData[:len(s.InputData)-1]
|
||||||
|
}
|
||||||
|
|
||||||
|
default:
|
||||||
|
if len(key) == 1 {
|
||||||
|
if int(s.max_size) > len(s.InputData) {
|
||||||
|
s.InputData += key
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
func InputBox(max_size uint) *InputBoxComponent {
|
||||||
|
return &InputBoxComponent{max_size: max_size}
|
||||||
|
}
|
||||||
@@ -0,0 +1,94 @@
|
|||||||
|
package ui
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"strings"
|
||||||
|
|
||||||
|
"github.com/orus-dev/osui"
|
||||||
|
"github.com/orus-dev/osui/styles"
|
||||||
|
)
|
||||||
|
|
||||||
|
type PaginatorStyle struct {
|
||||||
|
Active string `default:"\033[32m"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type PaginatorComponent struct {
|
||||||
|
Data osui.ComponentData
|
||||||
|
Style *PaginatorStyle
|
||||||
|
Components []osui.Component
|
||||||
|
ActiveComponent int
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p *PaginatorComponent) GetComponentData() *osui.ComponentData {
|
||||||
|
return &p.Data
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p PaginatorComponent) Render() string {
|
||||||
|
pgs := strings.Repeat(" ", p.Data.Width/2)
|
||||||
|
for page, c := range p.Components {
|
||||||
|
if page == p.ActiveComponent {
|
||||||
|
pgs += p.Style.Active + "•" + styles.Reset
|
||||||
|
} else {
|
||||||
|
pgs += styles.Reset + "•"
|
||||||
|
}
|
||||||
|
d := c.GetComponentData()
|
||||||
|
if d.Screen == nil {
|
||||||
|
d.Screen = p.Data.Screen
|
||||||
|
}
|
||||||
|
d.Height = p.Data.Height - 1
|
||||||
|
d.Width = p.Data.Width
|
||||||
|
}
|
||||||
|
frame := osui.NewFrame(p.Data.Width, p.Data.Height-3)
|
||||||
|
osui.RenderOnFrame(p.Components[p.ActiveComponent], &frame)
|
||||||
|
return fmt.Sprintf("%s\n%s", pgs, styles.Reset+strings.Join(frame, "\n"))
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p *PaginatorComponent) Update(key string) bool {
|
||||||
|
switch key {
|
||||||
|
case "":
|
||||||
|
p.updateActive(p.ActiveComponent)
|
||||||
|
case "\x1b[Z":
|
||||||
|
if p.ActiveComponent > 0 {
|
||||||
|
p.updateActive(p.ActiveComponent - 1)
|
||||||
|
} else {
|
||||||
|
p.updateActive(len(p.Components) - 1)
|
||||||
|
}
|
||||||
|
case osui.Key.Tab:
|
||||||
|
if p.ActiveComponent < len(p.Components)-1 {
|
||||||
|
p.updateActive(p.ActiveComponent + 1)
|
||||||
|
} else {
|
||||||
|
p.updateActive(0)
|
||||||
|
}
|
||||||
|
case osui.Key.Escape:
|
||||||
|
fmt.Print("\n\n")
|
||||||
|
return true
|
||||||
|
default:
|
||||||
|
if len(p.Components) > 0 {
|
||||||
|
p.Components[p.ActiveComponent].GetComponentData().IsActive = p.Data.IsActive
|
||||||
|
if p.Components[p.ActiveComponent].Update(key) {
|
||||||
|
if p.ActiveComponent < len(p.Components)-1 {
|
||||||
|
p.updateActive(p.ActiveComponent + 1)
|
||||||
|
} else {
|
||||||
|
p.updateActive(0)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p *PaginatorComponent) updateActive(newIndex int) {
|
||||||
|
if newIndex >= 0 && newIndex < len(p.Components) && len(p.Components) > 0 {
|
||||||
|
p.Components[p.ActiveComponent].GetComponentData().IsActive = false
|
||||||
|
p.ActiveComponent = newIndex
|
||||||
|
p.Components[p.ActiveComponent].GetComponentData().IsActive = p.Data.IsActive
|
||||||
|
p.Components[p.ActiveComponent].Update("")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func Paginator(pages ...osui.Component) *PaginatorComponent {
|
||||||
|
return &PaginatorComponent{
|
||||||
|
Components: pages,
|
||||||
|
Style: osui.SetDefaults(&PaginatorStyle{}).(*PaginatorStyle),
|
||||||
|
}
|
||||||
|
}
|
||||||
+26
@@ -0,0 +1,26 @@
|
|||||||
|
package ui
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/orus-dev/osui"
|
||||||
|
)
|
||||||
|
|
||||||
|
type TextComponent struct {
|
||||||
|
Data osui.ComponentData
|
||||||
|
Text string
|
||||||
|
}
|
||||||
|
|
||||||
|
func (t *TextComponent) Update(string) bool {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
func (t *TextComponent) GetComponentData() *osui.ComponentData {
|
||||||
|
return &t.Data
|
||||||
|
}
|
||||||
|
|
||||||
|
func (t TextComponent) Render() string {
|
||||||
|
return t.Text
|
||||||
|
}
|
||||||
|
|
||||||
|
func Text(text string) *TextComponent {
|
||||||
|
return &TextComponent{Text: text}
|
||||||
|
}
|
||||||
@@ -1,13 +0,0 @@
|
|||||||
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})
|
|
||||||
}
|
|
||||||
@@ -0,0 +1,165 @@
|
|||||||
|
package osui
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"os"
|
||||||
|
"os/exec"
|
||||||
|
"reflect"
|
||||||
|
"regexp"
|
||||||
|
"runtime"
|
||||||
|
"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, lo := range strings.Split(c.Render(), "\n") {
|
||||||
|
if int(componentData.Y)+i < len(*frame) {
|
||||||
|
fo := (*frame)[int(componentData.Y)+i]
|
||||||
|
f, fa := CompressString(fo, "\b")
|
||||||
|
line, la := CompressString(lo, "\t")
|
||||||
|
r := renderLine(f, line, int(componentData.X))
|
||||||
|
(*frame)[int(componentData.Y)+i] = DecompressString(DecompressString(r, la, "\t"), fa, "\b")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func CompressString(input, repl string) (string, []string) {
|
||||||
|
pattern := `\x1b\[([0-9;]*)m`
|
||||||
|
re := regexp.MustCompile(pattern)
|
||||||
|
matches := re.FindAllString(input, -1)
|
||||||
|
return re.ReplaceAllString(input, repl), matches
|
||||||
|
}
|
||||||
|
|
||||||
|
func DecompressString(modified string, outputArray []string, c string) string {
|
||||||
|
parts := strings.Split(modified, c)
|
||||||
|
reconstructed := ""
|
||||||
|
for i, part := range parts {
|
||||||
|
reconstructed += part
|
||||||
|
if i < len(outputArray) {
|
||||||
|
reconstructed += outputArray[i]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return reconstructed
|
||||||
|
}
|
||||||
|
|
||||||
|
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 NewFrame(width, height int) []string {
|
||||||
|
frame := make([]string, height)
|
||||||
|
for i := 0; i < height; i++ {
|
||||||
|
frame[i] = strings.Repeat(" ", width)
|
||||||
|
}
|
||||||
|
return frame
|
||||||
|
}
|
||||||
|
|
||||||
|
func Clear() {
|
||||||
|
switch runtime.GOOS {
|
||||||
|
case "windows":
|
||||||
|
cmd := exec.Command("cmd", "/c", "cls")
|
||||||
|
cmd.Stdout = os.Stdout
|
||||||
|
cmd.Run()
|
||||||
|
default:
|
||||||
|
cmd := exec.Command("clear")
|
||||||
|
cmd.Stdout = os.Stdout
|
||||||
|
cmd.Run()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func SetDefaults(p interface{}) interface{} {
|
||||||
|
v := reflect.ValueOf(p)
|
||||||
|
if v.Kind() != reflect.Ptr {
|
||||||
|
panic("setDefaults: expected a pointer to a struct")
|
||||||
|
}
|
||||||
|
val := v.Elem()
|
||||||
|
if val.Kind() != reflect.Struct {
|
||||||
|
panic("setDefaults: expected a pointer to a struct")
|
||||||
|
}
|
||||||
|
typ := val.Type()
|
||||||
|
for i := 0; i < val.NumField(); i++ {
|
||||||
|
field := val.Field(i)
|
||||||
|
structField := typ.Field(i)
|
||||||
|
if defaultValue, ok := structField.Tag.Lookup("default"); ok {
|
||||||
|
if field.Kind() == reflect.String && field.String() == "" {
|
||||||
|
field.SetString(defaultValue)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return p
|
||||||
|
}
|
||||||
|
|
||||||
|
func ShowCursor() {
|
||||||
|
fmt.Print("\033[?25h")
|
||||||
|
}
|
||||||
|
|
||||||
|
func HideCursor() {
|
||||||
|
fmt.Print("\033[?25l")
|
||||||
|
}
|
||||||
|
|
||||||
|
func LogicValue(b bool, _if, _else string) string {
|
||||||
|
if b {
|
||||||
|
return _if
|
||||||
|
}
|
||||||
|
return _else
|
||||||
|
}
|
||||||
|
|
||||||
|
func LogicValueInt(b bool, _if, _else int) int {
|
||||||
|
if b {
|
||||||
|
return _if
|
||||||
|
}
|
||||||
|
return _else
|
||||||
|
}
|
||||||
|
|
||||||
|
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