bug fix (There is a small bug on inputBox again)

This commit is contained in:
2024-09-27 14:38:39 +02:00
parent 3b2ff24c1f
commit cf93b3383f
7 changed files with 83 additions and 89 deletions
+11 -7
View File
@@ -8,17 +8,21 @@ import (
) )
func main() { func main() {
text := colors.Red + "This is a div!"
width, h := osui.GetTerminalSize() width, h := osui.GetTerminalSize()
paginator := ui.Paginator( paginator := ui.Paginator(
ui.Div( 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.Text("Welcome to the example! Press tab to go to the next page or press shift + 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, Foreground: colors.Red},
ui.Div(cutils.WithPosition(0, 0, ui.Text("This is a test!"))),
)))),
ui.Div(
cutils.WithPosition(26, 2, ui.Text("This is a InputBox. It takes user input just like a GUI")),
cutils.WithPosition(0, 1, ui.InputBox(20)),
), ),
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 := osui.NewScreen(paginator)
screen.Run() screen.Run()
+33
View File
@@ -0,0 +1,33 @@
package isKey
func Enter(s string) bool {
return s == "\r"
}
func Tab(s string) bool {
return s == "\t"
}
func Backspace(s string) bool {
return s == "\x7f" || s == "\b"
}
func Escape(s string) bool {
return s == "\x1b"
}
func Up(s string) bool {
return s == "\x1b[A"
}
func Down(s string) bool {
return s == "\x1b[B"
}
func Right(s string) bool {
return s == "\x1b[C"
}
func Left(s string) bool {
return s == "\x1b[D"
}
+10 -12
View File
@@ -5,6 +5,7 @@ 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/isKey"
) )
type DivStyle struct { type DivStyle struct {
@@ -39,12 +40,12 @@ func (d *DivComponent) Render() string {
if data.Height == 0 { if data.Height == 0 {
data.Height = d.Data.Height data.Height = d.Data.Height
} }
data.DefaultColor = colors.Combine(d.Style.Foreground, d.Style.Background) data.DefaultColor = colors.Combine(d.Style.Background, d.Style.Foreground)
data.Screen = d.Data.Screen data.Screen = d.Data.Screen
osui.RenderOnFrame(c, &frame) osui.RenderOnFrame(c, &frame)
} }
for i, f := range frame { for i, f := range frame {
frame[i] = colors.Reset + d.Data.DefaultColor + colors.Combine(d.Style.Foreground, d.Style.Background) + f + colors.Reset + d.Data.DefaultColor frame[i] = colors.Combine(d.Style.Foreground, d.Style.Background) + f + colors.Reset
} }
return strings.Join(frame, "\n") return strings.Join(frame, "\n")
} }
@@ -63,22 +64,19 @@ func (d *DivComponent) Run() {
} }
func (d *DivComponent) Update(key string) bool { func (d *DivComponent) Update(key string) bool {
switch key { if isKey.Up(key) {
case osui.Key.Up:
d.updateActive(d.ActiveComponent - 1) d.updateActive(d.ActiveComponent - 1)
case osui.Key.Down: } else if isKey.Down(key) {
d.updateActive(d.ActiveComponent + 1) d.updateActive(d.ActiveComponent + 1)
default: } else {
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
// }
} }
} }
} }
+16 -8
View File
@@ -6,6 +6,7 @@ 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/isKey"
) )
type InputBoxStyle struct { type InputBoxStyle struct {
@@ -19,6 +20,7 @@ type InputBoxComponent struct {
Data osui.ComponentData Data osui.ComponentData
Style *InputBoxStyle Style *InputBoxStyle
max_size uint max_size uint
cursor uint
InputData string InputData string
} }
@@ -30,7 +32,7 @@ func (s InputBoxComponent) Render() string {
osui.UseStyle(s.Style) 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%s|%s\n %s", " %s\n%s│%s%s│%s\n %s",
colors.Reset+s.Style.Outline+strings.Repeat("_", int(s.max_size))+colors.Reset, colors.Reset+s.Style.Outline+strings.Repeat("_", int(s.max_size))+colors.Reset,
colors.Reset+s.Style.Outline, colors.Reset+s.Style.Outline,
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), ""), 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), ""),
@@ -41,10 +43,10 @@ func (s InputBoxComponent) Render() string {
} }
return fmt.Sprintf( return fmt.Sprintf(
" %s\n%s|%s%s\n %s", " %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+strings.Repeat("_", int(s.max_size))+colors.Reset,
colors.Reset+s.Style.Outline, 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.Combine(s.Style.Foreground, s.Style.Background)+s.InputData+osui.LogicValue(s.Data.IsActive, s.Style.Cursor+"█"+colors.Reset+colors.Reset, colors.Reset+s.Style.Outline+"|"+colors.Reset),
colors.Reset+s.Data.DefaultColor, colors.Reset+s.Data.DefaultColor,
colors.Reset+s.Style.Outline+strings.Repeat("‾", int(s.max_size))+colors.Reset+s.Data.DefaultColor, colors.Reset+s.Style.Outline+strings.Repeat("‾", int(s.max_size))+colors.Reset+s.Data.DefaultColor,
) )
@@ -67,15 +69,21 @@ func (s *InputBoxComponent) Read() error {
} }
func (s *InputBoxComponent) Update(key string) bool { func (s *InputBoxComponent) Update(key string) bool {
switch key { if isKey.Enter(key) {
case osui.Key.Enter:
return true return true
case osui.Key.Backspace: } else if isKey.Backspace(key) {
if len(s.InputData) > 0 { if len(s.InputData) > 0 {
s.InputData = s.InputData[:len(s.InputData)-1] s.InputData = s.InputData[:len(s.InputData)-1]
} }
} else if isKey.Left(key) {
default: if s.cursor > 0 {
s.cursor--
}
} else if isKey.Right(key) {
if s.cursor < s.max_size {
s.cursor++
}
} else {
if len(key) == 1 { if len(key) == 1 {
if int(s.max_size) > len(s.InputData) { if int(s.max_size) > len(s.InputData) {
s.InputData += key s.InputData += key
+5 -5
View File
@@ -6,6 +6,7 @@ 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/isKey"
) )
type PaginatorStyle struct { type PaginatorStyle struct {
@@ -55,23 +56,22 @@ func (p *PaginatorComponent) Render() string {
} }
func (p *PaginatorComponent) Update(key string) bool { func (p *PaginatorComponent) Update(key string) bool {
switch key { if key == "\x1b[Z" {
case "\x1b[Z":
if p.ActiveComponent > 0 { if p.ActiveComponent > 0 {
p.updateActive(p.ActiveComponent - 1) p.updateActive(p.ActiveComponent - 1)
} else { } else {
p.updateActive(len(p.Components) - 1) p.updateActive(len(p.Components) - 1)
} }
case osui.Key.Tab: } else if isKey.Tab(key) {
if p.ActiveComponent < len(p.Components)-1 { if p.ActiveComponent < len(p.Components)-1 {
p.updateActive(p.ActiveComponent + 1) p.updateActive(p.ActiveComponent + 1)
} else { } else {
p.updateActive(0) p.updateActive(0)
} }
case osui.Key.Escape: } else if isKey.Escape(key) {
fmt.Print("\n\n") fmt.Print("\n\n")
return true return true
default: } else {
if len(p.Components) > 0 { if len(p.Components) > 0 {
p.Components[p.ActiveComponent].GetComponentData().IsActive = p.Data.IsActive p.Components[p.ActiveComponent].GetComponentData().IsActive = p.Data.IsActive
if p.Components[p.ActiveComponent].Update(key) { if p.Components[p.ActiveComponent].Update(key) {
+1 -2
View File
@@ -2,7 +2,6 @@ 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 {
@@ -19,7 +18,7 @@ func (t *TextComponent) GetComponentData() *osui.ComponentData {
} }
func (t TextComponent) Render() string { func (t TextComponent) Render() string {
return colors.Reset + t.Data.DefaultColor + t.Text return t.Text
} }
func (t *TextComponent) SetStyle(interface{}) {} func (t *TextComponent) SetStyle(interface{}) {}
+7 -55
View File
@@ -16,25 +16,15 @@ import (
func renderLine(frameLine, line string, x int) string { func renderLine(frameLine, line string, x int) string {
result := []rune(frameLine) result := []rune(frameLine)
lineRunes := []rune(line) lineRunes := []rune(line)
x += strings.Count(string(result[:(x+1)]), "\t") + strings.Count(string(result[:(x+1)]), "\b")
for range strings.Count(line, "\t") + strings.Count(line, "\b") {
result = append(result, ' ')
}
frameLen := len(result) frameLen := len(result)
lineLen := len(lineRunes)
cl := 0 for i := 0; i < len(lineRunes) && x+i < frameLen; i++ {
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]
} }
@@ -49,7 +39,6 @@ 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")
} }
} }
@@ -179,43 +168,6 @@ 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 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",
}
func GetTerminalSize() (int, int) { func GetTerminalSize() (int, int) {
w, h, err := term.GetSize(0) w, h, err := term.GetSize(0)
if err != nil { if err != nil {