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