bug fix but still a new one :(

This commit is contained in:
2024-09-26 23:03:17 +02:00
parent 3b3e92fae5
commit 4214f96c61
8 changed files with 108 additions and 66 deletions
+14 -14
View File
@@ -7,21 +7,21 @@ import (
)
const (
Reset = "\033[0m"
Bold = "\033[1m"
Underline = "\033[4m"
Italic = "\033[3m"
Reverse = "\033[7m"
Strike = "\033[9m"
Reset = "\x1b[0m"
Bold = "\x1b[1m"
Underline = "\x1b[4m"
Italic = "\x1b[3m"
Reverse = "\x1b[7m"
Strike = "\x1b[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"
Black = "\x1b[30m"
Red = "\x1b[31m"
Green = "\x1b[32m"
Yellow = "\x1b[33m"
Blue = "\x1b[34m"
Magenta = "\x1b[35m"
Cyan = "\x1b[36m"
White = "\x1b[37m"
)
func Rgb(r, g, b uint8) string {
+14 -21
View File
@@ -1,32 +1,25 @@
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/cutils"
"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)
text := colors.Red + "This is a div!"
width, h := osui.GetTerminalSize()
paginator := ui.Paginator(
ui.Div(
ui.Text("Welcome to the example! Press tab to go to the next page or press tab to go to a previous page"),
),
ui.Div(cutils.WithPosition((width-30)/2, (h-4)/2, cutils.WithSize(30, 4, cutils.WithStyle(&ui.DivStyle{Background: colors.Blue}, ui.Div(ui.Text(text)))))),
// ui.Div(
// ui.Text("This is a InputBox. It takes user input and"),
// cutils.WithPosition(0, 2, ui.InputBox(20)),
// ),
)
screen := osui.NewScreen(paginator)
screen.Run()
}
+4 -7
View File
@@ -5,12 +5,11 @@ import (
"strings"
"github.com/orus-dev/osui/colors"
"golang.org/x/term"
)
type ComponentData struct {
X uint16
Y uint16
X int
Y int
Width int
Height int
DefaultColor string
@@ -22,6 +21,7 @@ type Component interface {
Render() string
GetComponentData() *ComponentData
Update(string) bool
SetStyle(interface{})
}
type Screen struct {
@@ -41,10 +41,7 @@ func (s *Screen) Render() error {
return nil
}
Clear()
width, height, err := term.GetSize(0)
if err != nil {
return fmt.Errorf("error getting the terminal size: %s", err)
}
width, height := GetTerminalSize()
frame := NewFrame(width, height)
data := s.component.GetComponentData()
if data.Height == 0 {
+11 -6
View File
@@ -44,7 +44,7 @@ func (d *DivComponent) Render() string {
osui.RenderOnFrame(c, &frame)
}
for i, f := range frame {
frame[i] = colors.Combine(d.Style.Foreground, d.Style.Background) + f + d.Data.DefaultColor
frame[i] = colors.Reset + d.Data.DefaultColor + colors.Combine(d.Style.Foreground, d.Style.Background) + f + colors.Reset + d.Data.DefaultColor
}
return strings.Join(frame, "\n")
}
@@ -73,17 +73,22 @@ func (d *DivComponent) Update(key string) bool {
if len(d.Components) > 0 {
d.Components[d.ActiveComponent].GetComponentData().IsActive = d.Data.IsActive
if d.Components[d.ActiveComponent].Update(key) {
if d.ActiveComponent < len(d.Components)-1 {
d.updateActive(d.ActiveComponent + 1)
} else {
return true
}
return true
// if d.ActiveComponent < len(d.Components)-1 {
// d.updateActive(d.ActiveComponent + 1)
// } else {
// return true
// }
}
}
}
return false
}
func (d *DivComponent) SetStyle(c interface{}) {
d.Style = osui.SetDefaults(c.(*DivStyle)).(*DivStyle)
}
func (d *DivComponent) updateActive(newIndex int) {
if newIndex >= 0 && newIndex < len(d.Components) && len(d.Components) > 0 {
d.ActiveComponent = newIndex
+11 -4
View File
@@ -39,11 +39,14 @@ func (s InputBoxComponent) Render() string {
s.Style.Outline+strings.Repeat("‾", int(s.max_size))+colors.Reset+s.Data.DefaultColor,
)
}
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)),
" %s\n%s|%s%s\n %s",
colors.Reset+s.Style.Outline+strings.Repeat("_", int(s.max_size))+colors.Reset,
colors.Reset+s.Style.Outline,
colors.Combine(s.Style.Foreground, s.Style.Background)+s.InputData+osui.LogicValue(s.Data.IsActive, s.Style.Cursor+"█", "")+colors.Reset+colors.Reset,
colors.Reset+s.Data.DefaultColor,
colors.Reset+s.Style.Outline+strings.Repeat("‾", int(s.max_size))+colors.Reset+s.Data.DefaultColor,
)
}
@@ -82,6 +85,10 @@ func (s *InputBoxComponent) Update(key string) bool {
return false
}
func (d *InputBoxComponent) SetStyle(c interface{}) {
d.Style = osui.SetDefaults(c.(*InputBoxStyle)).(*InputBoxStyle)
}
func InputBox(max_size uint) *InputBoxComponent {
return &InputBoxComponent{max_size: max_size, Style: osui.SetDefaults(&InputBoxStyle{}).(*InputBoxStyle)}
}
+25 -12
View File
@@ -9,7 +9,8 @@ import (
)
type PaginatorStyle struct {
Active string `default:"\033[32m"`
Active string `default:"\033[32m"`
Inactive string `default:""`
}
type PaginatorComponent struct {
@@ -23,25 +24,33 @@ 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 {
func (p *PaginatorComponent) Render() string {
width, _ := osui.GetTerminalSize()
pgs := strings.Repeat(" ", (width-len(p.Components))/2)
osui.UseStyle(p.Style)
frame := osui.NewFrame(p.Data.Width, p.Data.Height)
for i, c := range p.Components {
data := c.GetComponentData()
if page == p.ActiveComponent {
data.IsActive = true
if i == p.ActiveComponent {
pgs += p.Style.Active + "•" + colors.Reset
data.IsActive = true
} else {
pgs += colors.Reset + "•"
pgs += colors.Reset + p.Style.Inactive + "•"
data.IsActive = false
}
if data.Screen == nil {
data.Screen = p.Data.Screen
if data.Width == 0 {
data.Width = p.Data.Width
}
data.Height = p.Data.Height - 1
data.Width = p.Data.Width
if data.Height == 0 {
data.Height = p.Data.Height
}
data.DefaultColor = p.Data.DefaultColor
data.Screen = p.Data.Screen
}
frame := osui.NewFrame(p.Data.Width, p.Data.Height-3)
osui.RenderOnFrame(p.Components[p.ActiveComponent], &frame)
for i, f := range frame {
frame[i] = colors.Reset + p.Data.DefaultColor + f + colors.Reset
}
return fmt.Sprintf("%s\n%s", pgs, colors.Reset+strings.Join(frame, "\n"))
}
@@ -83,6 +92,10 @@ func (p *PaginatorComponent) updateActive(newIndex int) {
}
}
func (p *PaginatorComponent) SetStyle(c interface{}) {
p.Style = osui.SetDefaults(c.(*PaginatorStyle)).(*PaginatorStyle)
}
func Paginator(pages ...osui.Component) *PaginatorComponent {
return &PaginatorComponent{
Components: pages,
+4 -1
View File
@@ -2,6 +2,7 @@ package ui
import (
"github.com/orus-dev/osui"
"github.com/orus-dev/osui/colors"
)
type TextComponent struct {
@@ -18,9 +19,11 @@ func (t *TextComponent) GetComponentData() *osui.ComponentData {
}
func (t TextComponent) Render() string {
return t.Text
return colors.Reset + t.Data.DefaultColor + t.Text
}
func (t *TextComponent) SetStyle(interface{}) {}
func Text(text string) *TextComponent {
return &TextComponent{Text: text}
}
+25 -1
View File
@@ -19,7 +19,22 @@ func renderLine(frameLine, line string, x int) string {
frameLen := len(result)
lineLen := len(lineRunes)
for i := 0; i < lineLen && x+i < frameLen; i++ {
cl := 0
for i, r := range result {
if r == '\b' || r == '\t' {
cl++
}
if i > x+1 {
break
}
}
x += cl
for i := 0; i < lineLen && (x+i)-cl < frameLen; i++ {
if x+i > frameLen {
result = append(result, lineRunes[i])
continue
}
result[x+i] = lineRunes[i]
}
@@ -34,6 +49,7 @@ func RenderOnFrame(c Component, frame *[]string) {
f, fa := CompressString(fo, "\b")
line, la := CompressString(lo, "\t")
r := renderLine(f, line, int(componentData.X))
// (*frame)[int(componentData.Y)+i] = fmt.Sprintf("%#v\n", DecompressString(DecompressString(r, la, "\t"), fa, "\b"))
(*frame)[int(componentData.Y)+i] = DecompressString(DecompressString(r, la, "\t"), fa, "\b")
}
}
@@ -199,3 +215,11 @@ var Key = key{
Right: "\x1b[C",
Left: "\x1b[D",
}
func GetTerminalSize() (int, int) {
w, h, err := term.GetSize(0)
if err != nil {
panic(err)
}
return w, h
}