This commit is contained in:
2024-09-26 17:49:03 +02:00
parent 8f375efe41
commit 179de20dba
9 changed files with 253 additions and 127 deletions
+60
View File
@@ -0,0 +1,60 @@
package colors
import (
"fmt"
"regexp"
"strings"
)
const (
Reset = "\033[0m"
Bold = "\033[1m"
Underline = "\033[4m"
Italic = "\033[3m"
Reverse = "\033[7m"
Strike = "\033[9m"
Black = "\033[30m"
Red = "\033[31m"
Green = "\033[32m"
Yellow = "\033[33m"
Blue = "\033[34m"
Magenta = "\033[35m"
Cyan = "\033[36m"
White = "\033[37m"
)
func Rgb(r, g, b uint8) string {
return fmt.Sprintf("\x1b[38;2;%d;%d;%dm", r, g, b)
}
func AsBg(s string) string {
re, _ := regexp.Compile(`\x1b\[3([0-9]+)`)
return re.ReplaceAllStringFunc(s, func(match string) string {
groups := re.FindStringSubmatch(match)
if len(groups) >= 2 {
return fmt.Sprintf("\x1b[4%s", groups[1])
}
return match
})
}
func AsFg(s string) string {
re, _ := regexp.Compile(`\x1b\[4([0-9]+)`)
return re.ReplaceAllStringFunc(s, func(match string) string {
groups := re.FindStringSubmatch(match)
if len(groups) >= 2 {
return fmt.Sprintf("\x1b[3%s", groups[1])
}
return match
})
}
func Combine(s, s1 string) string {
if strings.Contains(s, Reset) {
return s + s1
} else {
return s1 + s
}
}
+32
View File
@@ -0,0 +1,32 @@
package main
// IMPORTANT NOTE: Needs bugfix. "Some text here" is distorted
import (
"github.com/orus-dev/osui"
"github.com/orus-dev/osui/colors"
"github.com/orus-dev/osui/ui"
)
func main() {
// innerDiv := ui.Div(ui.Text("ABC"))
// innerDiv.Data.X = 4
// innerDiv.Style.BackgroundColor = colors.Red
// innerDiv.Data.Width = 5
// innerDiv.Data.Height = 3
input := ui.InputBox(30)
input.Style.Cursor = colors.Green
input.Style.Background = colors.Red
input.Style.Outline = colors.Blue
someText := ui.Text("Some text here")
someText.Data.X = 40
someText.Data.Y = 1
div := ui.Div(input, someText)
screen := osui.NewScreen(div)
screen.Run()
}
-16
View File
@@ -1,16 +0,0 @@
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()
}
+13 -2
View File
@@ -4,6 +4,7 @@ import (
"fmt" "fmt"
"strings" "strings"
"github.com/orus-dev/osui/colors"
"golang.org/x/term" "golang.org/x/term"
) )
@@ -12,6 +13,7 @@ type ComponentData struct {
Y uint16 Y uint16
Width int Width int
Height int Height int
DefaultColor string
IsActive bool IsActive bool
Screen *Screen Screen *Screen
} }
@@ -24,6 +26,7 @@ type Component interface {
type Screen struct { type Screen struct {
component Component component Component
CustomRender func()
} }
func NewScreen(c Component) *Screen { func NewScreen(c Component) *Screen {
@@ -33,6 +36,10 @@ func NewScreen(c Component) *Screen {
} }
func (s *Screen) Render() error { func (s *Screen) Render() error {
if s.CustomRender != nil {
s.CustomRender()
return nil
}
Clear() Clear()
width, height, err := term.GetSize(0) width, height, err := term.GetSize(0)
if err != nil { if err != nil {
@@ -40,10 +47,15 @@ func (s *Screen) Render() error {
} }
frame := NewFrame(width, height) frame := NewFrame(width, height)
data := s.component.GetComponentData() data := s.component.GetComponentData()
data.Width = width if data.Height == 0 {
data.Height = height data.Height = height
}
if data.Width == 0 {
data.Width = width
}
data.Screen = s data.Screen = s
data.IsActive = true data.IsActive = true
data.DefaultColor = colors.Reset
RenderOnFrame(s.component, &frame) RenderOnFrame(s.component, &frame)
fmt.Print(strings.Join(frame, "\n")) fmt.Print(strings.Join(frame, "\n"))
return nil return nil
@@ -51,7 +63,6 @@ func (s *Screen) Render() error {
func (s *Screen) Run() { func (s *Screen) Run() {
s.component.GetComponentData().Screen = s s.component.GetComponentData().Screen = s
s.component.Update("OSUI:RENDER")
for { for {
s.Render() s.Render()
k, _ := ReadKey() k, _ := ReadKey()
-36
View File
@@ -1,36 +0,0 @@
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)
}
+46 -29
View File
@@ -4,58 +4,77 @@ import (
"strings" "strings"
"github.com/orus-dev/osui" "github.com/orus-dev/osui"
"github.com/orus-dev/osui/colors"
) )
type DivStyle struct {
Background string `defaults:"" type:"bg"`
Foreground string `defaults:"" type:"fg"`
}
type DivComponent struct { type DivComponent struct {
Data osui.ComponentData Data osui.ComponentData
Style *DivStyle
Components []osui.Component Components []osui.Component
ActiveComponent int ActiveComponent int
} }
func (p *DivComponent) GetComponentData() *osui.ComponentData { func (d *DivComponent) GetComponentData() *osui.ComponentData {
return &p.Data return &d.Data
} }
func (p DivComponent) Render() string { func (d *DivComponent) Render() string {
frame := osui.NewFrame(p.Data.Width, p.Data.Height) osui.UseStyle(d.Style)
for _, c := range p.Components { frame := osui.NewFrame(d.Data.Width, d.Data.Height)
for i, c := range d.Components {
data := c.GetComponentData() data := c.GetComponentData()
data.Width = p.Data.Width if i == d.ActiveComponent {
data.Height = p.Data.Height data.IsActive = true
data.Screen = p.Data.Screen } else {
data.IsActive = false
}
if data.Width == 0 {
data.Width = d.Data.Width
}
if data.Height == 0 {
data.Height = d.Data.Height
}
data.DefaultColor = colors.Combine(d.Style.Foreground, d.Style.Background)
data.Screen = d.Data.Screen
osui.RenderOnFrame(c, &frame) osui.RenderOnFrame(c, &frame)
} }
for i, f := range frame {
frame[i] = colors.Combine(d.Style.Foreground, d.Style.Background) + f + d.Data.DefaultColor
}
return strings.Join(frame, "\n") return strings.Join(frame, "\n")
} }
func (p *DivComponent) Run() { func (d *DivComponent) Run() {
for _, c := range p.Components { for _, c := range d.Components {
d := c.GetComponentData() data := c.GetComponentData()
d.Screen = p.Data.Screen data.Screen = d.Data.Screen
} }
for { for {
key, _ := osui.ReadKey() key, _ := osui.ReadKey()
if p.Update(key) { if d.Update(key) {
return return
} }
} }
} }
func (p *DivComponent) Update(key string) bool { func (d *DivComponent) Update(key string) bool {
switch key { switch key {
case "":
p.updateActive(p.ActiveComponent)
case osui.Key.Up: case osui.Key.Up:
p.updateActive(p.ActiveComponent - 1) d.updateActive(d.ActiveComponent - 1)
case osui.Key.Down: case osui.Key.Down:
p.updateActive(p.ActiveComponent + 1) d.updateActive(d.ActiveComponent + 1)
default: default:
if len(p.Components) > 0 { if len(d.Components) > 0 {
p.Components[p.ActiveComponent].GetComponentData().IsActive = p.Data.IsActive d.Components[d.ActiveComponent].GetComponentData().IsActive = d.Data.IsActive
if p.Components[p.ActiveComponent].Update(key) { if d.Components[d.ActiveComponent].Update(key) {
if p.ActiveComponent < len(p.Components)-1 { if d.ActiveComponent < len(d.Components)-1 {
p.updateActive(p.ActiveComponent + 1) d.updateActive(d.ActiveComponent + 1)
} else { } else {
return true return true
} }
@@ -65,17 +84,15 @@ func (p *DivComponent) Update(key string) bool {
return false return false
} }
func (p *DivComponent) updateActive(newIndex int) { func (d *DivComponent) updateActive(newIndex int) {
if newIndex >= 0 && newIndex < len(p.Components) && len(p.Components) > 0 { if newIndex >= 0 && newIndex < len(d.Components) && len(d.Components) > 0 {
p.Components[p.ActiveComponent].GetComponentData().IsActive = false d.ActiveComponent = newIndex
p.ActiveComponent = newIndex
p.Components[p.ActiveComponent].GetComponentData().IsActive = p.Data.IsActive
p.Components[p.ActiveComponent].Update("")
} }
} }
func Div(components ...osui.Component) *DivComponent { func Div(components ...osui.Component) *DivComponent {
return &DivComponent{ return &DivComponent{
Components: components, Components: components,
Style: osui.SetDefaults(&DivStyle{}).(*DivStyle),
} }
} }
+18 -7
View File
@@ -5,10 +5,19 @@ import (
"strings" "strings"
"github.com/orus-dev/osui" "github.com/orus-dev/osui"
"github.com/orus-dev/osui/colors"
) )
type InputBoxStyle struct {
Background string `defaults:"" type:"bg"`
Foreground string `defaults:"" type:"fg"`
Outline string `defaults:"" type:"fg"`
Cursor string `defaults:"" type:"fg"`
}
type InputBoxComponent struct { type InputBoxComponent struct {
Data osui.ComponentData Data osui.ComponentData
Style *InputBoxStyle
max_size uint max_size uint
InputData string InputData string
} }
@@ -18,13 +27,16 @@ func (s *InputBoxComponent) GetComponentData() *osui.ComponentData {
} }
func (s InputBoxComponent) Render() string { func (s InputBoxComponent) Render() string {
osui.UseStyle(s.Style)
if s.max_size > uint(len(s.InputData)) { if s.max_size > uint(len(s.InputData)) {
return fmt.Sprintf( return fmt.Sprintf(
" %s\n|%s%s|\n %s", " %s\n%s|%s%s|%s\n %s",
strings.Repeat("_", int(s.max_size)), colors.Reset+s.Style.Outline+strings.Repeat("_", int(s.max_size))+colors.Reset,
s.InputData+osui.LogicValue(s.Data.IsActive, "█", ""), colors.Reset+s.Style.Outline,
strings.Repeat(" ", int(s.max_size)-len(s.InputData)-osui.LogicValueInt(s.Data.IsActive, 1, 0)), colors.Combine(s.Style.Foreground, s.Style.Background)+s.InputData+osui.LogicValue(s.Data.IsActive, s.Style.Cursor+"█"+colors.Combine(s.Style.Foreground, s.Style.Background), ""),
strings.Repeat("‾", int(s.max_size)), strings.Repeat(" ", int(s.max_size)-len(s.InputData)-osui.LogicValueInt(s.Data.IsActive, 1, 0))+colors.Reset+s.Style.Outline,
colors.Reset+s.Data.DefaultColor,
s.Style.Outline+strings.Repeat("‾", int(s.max_size))+colors.Reset+s.Data.DefaultColor,
) )
} }
return fmt.Sprintf( return fmt.Sprintf(
@@ -53,7 +65,6 @@ func (s *InputBoxComponent) Read() error {
func (s *InputBoxComponent) Update(key string) bool { func (s *InputBoxComponent) Update(key string) bool {
switch key { switch key {
case "":
case osui.Key.Enter: case osui.Key.Enter:
return true return true
case osui.Key.Backspace: case osui.Key.Backspace:
@@ -72,5 +83,5 @@ func (s *InputBoxComponent) Update(key string) bool {
} }
func InputBox(max_size uint) *InputBoxComponent { func InputBox(max_size uint) *InputBoxComponent {
return &InputBoxComponent{max_size: max_size} return &InputBoxComponent{max_size: max_size, Style: osui.SetDefaults(&InputBoxStyle{}).(*InputBoxStyle)}
} }
+11 -14
View File
@@ -5,7 +5,7 @@ import (
"strings" "strings"
"github.com/orus-dev/osui" "github.com/orus-dev/osui"
"github.com/orus-dev/osui/styles" "github.com/orus-dev/osui/colors"
) )
type PaginatorStyle struct { type PaginatorStyle struct {
@@ -26,27 +26,27 @@ func (p *PaginatorComponent) GetComponentData() *osui.ComponentData {
func (p PaginatorComponent) Render() string { func (p PaginatorComponent) Render() string {
pgs := strings.Repeat(" ", p.Data.Width/2) pgs := strings.Repeat(" ", p.Data.Width/2)
for page, c := range p.Components { for page, c := range p.Components {
data := c.GetComponentData()
if page == p.ActiveComponent { if page == p.ActiveComponent {
pgs += p.Style.Active + "•" + styles.Reset data.IsActive = true
pgs += p.Style.Active + "•" + colors.Reset
} else { } else {
pgs += styles.Reset + "•" pgs += colors.Reset + "•"
data.IsActive = false
} }
d := c.GetComponentData() if data.Screen == nil {
if d.Screen == nil { data.Screen = p.Data.Screen
d.Screen = p.Data.Screen
} }
d.Height = p.Data.Height - 1 data.Height = p.Data.Height - 1
d.Width = p.Data.Width data.Width = p.Data.Width
} }
frame := osui.NewFrame(p.Data.Width, p.Data.Height-3) frame := osui.NewFrame(p.Data.Width, p.Data.Height-3)
osui.RenderOnFrame(p.Components[p.ActiveComponent], &frame) osui.RenderOnFrame(p.Components[p.ActiveComponent], &frame)
return fmt.Sprintf("%s\n%s", pgs, styles.Reset+strings.Join(frame, "\n")) return fmt.Sprintf("%s\n%s", pgs, colors.Reset+strings.Join(frame, "\n"))
} }
func (p *PaginatorComponent) Update(key string) bool { func (p *PaginatorComponent) Update(key string) bool {
switch key { switch key {
case "":
p.updateActive(p.ActiveComponent)
case "\x1b[Z": case "\x1b[Z":
if p.ActiveComponent > 0 { if p.ActiveComponent > 0 {
p.updateActive(p.ActiveComponent - 1) p.updateActive(p.ActiveComponent - 1)
@@ -79,10 +79,7 @@ func (p *PaginatorComponent) Update(key string) bool {
func (p *PaginatorComponent) updateActive(newIndex int) { func (p *PaginatorComponent) updateActive(newIndex int) {
if newIndex >= 0 && newIndex < len(p.Components) && len(p.Components) > 0 { if newIndex >= 0 && newIndex < len(p.Components) && len(p.Components) > 0 {
p.Components[p.ActiveComponent].GetComponentData().IsActive = false
p.ActiveComponent = newIndex p.ActiveComponent = newIndex
p.Components[p.ActiveComponent].GetComponentData().IsActive = p.Data.IsActive
p.Components[p.ActiveComponent].Update("")
} }
} }
+63 -13
View File
@@ -9,24 +9,35 @@ import (
"runtime" "runtime"
"strings" "strings"
"github.com/orus-dev/osui/colors"
"golang.org/x/term" "golang.org/x/term"
) )
func renderLine(frameLine, line string, x int) string { func renderLine(frameLine, line string, x int) string {
result := "" result := []rune(frameLine) // Convert frameLine to a slice of runes for mutability
if x >= len(frameLine) { visibleCount := 0 // Track visible characters in frameLine
return result
for _, c := range line {
// Skip over non-visible characters when counting the frameLine length
for visibleCount < len(result) {
if result[visibleCount] == '\t' || result[visibleCount] == '\b' {
visibleCount++ // Skip these characters without counting them
} else {
break
}
} }
lChars := []rune(line) // If visibleCount exceeds the length of frameLine, stop
for i, c := range frameLine { if x+visibleCount >= len(result) {
if i >= x && len(lChars) > i-x { break
result += string(lChars[i-x])
} else {
result += string(c)
} }
// Overlay the character from the line onto frameLine at the correct position
result[x+visibleCount] = c
visibleCount++ // Increment visible character count
} }
return result
return string(result) // Convert the result back to a string
} }
func RenderOnFrame(c Component, frame *[]string) { func RenderOnFrame(c Component, frame *[]string) {
@@ -43,7 +54,7 @@ func RenderOnFrame(c Component, frame *[]string) {
} }
func CompressString(input, repl string) (string, []string) { func CompressString(input, repl string) (string, []string) {
pattern := `\x1b\[([0-9;]*)m` pattern := `\x1b\[([0-9;]*)[a-zA-Z]`
re := regexp.MustCompile(pattern) re := regexp.MustCompile(pattern)
matches := re.FindAllString(input, -1) matches := re.FindAllString(input, -1)
return re.ReplaceAllString(input, repl), matches return re.ReplaceAllString(input, repl), matches
@@ -99,11 +110,11 @@ func Clear() {
func SetDefaults(p interface{}) interface{} { func SetDefaults(p interface{}) interface{} {
v := reflect.ValueOf(p) v := reflect.ValueOf(p)
if v.Kind() != reflect.Ptr { if v.Kind() != reflect.Ptr {
panic("setDefaults: expected a pointer to a struct") panic("SetDefaults: expected a pointer to a struct")
} }
val := v.Elem() val := v.Elem()
if val.Kind() != reflect.Struct { if val.Kind() != reflect.Struct {
panic("setDefaults: expected a pointer to a struct") panic("SetDefaults: expected a pointer to a struct")
} }
typ := val.Type() typ := val.Type()
for i := 0; i < val.NumField(); i++ { for i := 0; i < val.NumField(); i++ {
@@ -118,6 +129,32 @@ func SetDefaults(p interface{}) interface{} {
return p return p
} }
func UseStyle(p interface{}) {
v := reflect.ValueOf(p)
if v.Kind() != reflect.Ptr {
panic("SetStyle: expected a pointer to a struct")
}
val := v.Elem()
if val.Kind() != reflect.Struct {
panic("SetStyle: 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 styleType, ok := structField.Tag.Lookup("type"); ok {
if field.Kind() == reflect.String {
if styleType == "bg" || styleType == "background" {
field.SetString(colors.AsBg(field.String()))
} else {
field.SetString(colors.AsFg(field.String()))
}
}
}
}
}
func ShowCursor() { func ShowCursor() {
fmt.Print("\033[?25h") fmt.Print("\033[?25h")
} }
@@ -140,6 +177,19 @@ func LogicValueInt(b bool, _if, _else int) int {
return _else return _else
} }
func DebugRender(c Component) {
s := NewScreen(c)
s.CustomRender = func() {
}
// data := c.GetComponentData()
fmt.Println(colors.Bold + "---OBJ RENDER---" + colors.Reset)
for _, line := range strings.Split(c.Render(), "\n") {
fmt.Printf("%#v\n", line)
}
s.Run()
}
type Key_ = string type Key_ = string
type key struct { type key struct {