bug fix (There is a small bug on inputBox again)
This commit is contained in:
+11
-7
@@ -8,17 +8,21 @@ import (
|
||||
)
|
||||
|
||||
func main() {
|
||||
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.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.Run()
|
||||
|
||||
@@ -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"
|
||||
}
|
||||
@@ -5,6 +5,7 @@ import (
|
||||
|
||||
"github.com/orus-dev/osui"
|
||||
"github.com/orus-dev/osui/colors"
|
||||
"github.com/orus-dev/osui/isKey"
|
||||
)
|
||||
|
||||
type DivStyle struct {
|
||||
@@ -39,12 +40,12 @@ func (d *DivComponent) Render() string {
|
||||
if data.Height == 0 {
|
||||
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
|
||||
osui.RenderOnFrame(c, &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")
|
||||
}
|
||||
@@ -63,22 +64,19 @@ func (d *DivComponent) Run() {
|
||||
}
|
||||
|
||||
func (d *DivComponent) Update(key string) bool {
|
||||
switch key {
|
||||
|
||||
case osui.Key.Up:
|
||||
if isKey.Up(key) {
|
||||
d.updateActive(d.ActiveComponent - 1)
|
||||
case osui.Key.Down:
|
||||
} else if isKey.Down(key) {
|
||||
d.updateActive(d.ActiveComponent + 1)
|
||||
default:
|
||||
} else {
|
||||
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
|
||||
// if d.ActiveComponent < len(d.Components)-1 {
|
||||
// d.updateActive(d.ActiveComponent + 1)
|
||||
// } else {
|
||||
// return true
|
||||
// }
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+16
-8
@@ -6,6 +6,7 @@ import (
|
||||
|
||||
"github.com/orus-dev/osui"
|
||||
"github.com/orus-dev/osui/colors"
|
||||
"github.com/orus-dev/osui/isKey"
|
||||
)
|
||||
|
||||
type InputBoxStyle struct {
|
||||
@@ -19,6 +20,7 @@ type InputBoxComponent struct {
|
||||
Data osui.ComponentData
|
||||
Style *InputBoxStyle
|
||||
max_size uint
|
||||
cursor uint
|
||||
InputData string
|
||||
}
|
||||
|
||||
@@ -30,7 +32,7 @@ func (s InputBoxComponent) Render() string {
|
||||
osui.UseStyle(s.Style)
|
||||
if s.max_size > uint(len(s.InputData)) {
|
||||
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,
|
||||
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(
|
||||
" %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,
|
||||
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.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 {
|
||||
switch key {
|
||||
case osui.Key.Enter:
|
||||
if isKey.Enter(key) {
|
||||
return true
|
||||
case osui.Key.Backspace:
|
||||
} else if isKey.Backspace(key) {
|
||||
if len(s.InputData) > 0 {
|
||||
s.InputData = s.InputData[:len(s.InputData)-1]
|
||||
}
|
||||
|
||||
default:
|
||||
} else if isKey.Left(key) {
|
||||
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 int(s.max_size) > len(s.InputData) {
|
||||
s.InputData += key
|
||||
|
||||
+5
-5
@@ -6,6 +6,7 @@ import (
|
||||
|
||||
"github.com/orus-dev/osui"
|
||||
"github.com/orus-dev/osui/colors"
|
||||
"github.com/orus-dev/osui/isKey"
|
||||
)
|
||||
|
||||
type PaginatorStyle struct {
|
||||
@@ -55,23 +56,22 @@ func (p *PaginatorComponent) Render() string {
|
||||
}
|
||||
|
||||
func (p *PaginatorComponent) Update(key string) bool {
|
||||
switch key {
|
||||
case "\x1b[Z":
|
||||
if key == "\x1b[Z" {
|
||||
if p.ActiveComponent > 0 {
|
||||
p.updateActive(p.ActiveComponent - 1)
|
||||
} else {
|
||||
p.updateActive(len(p.Components) - 1)
|
||||
}
|
||||
case osui.Key.Tab:
|
||||
} else if isKey.Tab(key) {
|
||||
if p.ActiveComponent < len(p.Components)-1 {
|
||||
p.updateActive(p.ActiveComponent + 1)
|
||||
} else {
|
||||
p.updateActive(0)
|
||||
}
|
||||
case osui.Key.Escape:
|
||||
} else if isKey.Escape(key) {
|
||||
fmt.Print("\n\n")
|
||||
return true
|
||||
default:
|
||||
} else {
|
||||
if len(p.Components) > 0 {
|
||||
p.Components[p.ActiveComponent].GetComponentData().IsActive = p.Data.IsActive
|
||||
if p.Components[p.ActiveComponent].Update(key) {
|
||||
|
||||
+1
-2
@@ -2,7 +2,6 @@ package ui
|
||||
|
||||
import (
|
||||
"github.com/orus-dev/osui"
|
||||
"github.com/orus-dev/osui/colors"
|
||||
)
|
||||
|
||||
type TextComponent struct {
|
||||
@@ -19,7 +18,7 @@ func (t *TextComponent) GetComponentData() *osui.ComponentData {
|
||||
}
|
||||
|
||||
func (t TextComponent) Render() string {
|
||||
return colors.Reset + t.Data.DefaultColor + t.Text
|
||||
return t.Text
|
||||
}
|
||||
|
||||
func (t *TextComponent) SetStyle(interface{}) {}
|
||||
|
||||
@@ -16,25 +16,15 @@ import (
|
||||
func renderLine(frameLine, line string, x int) string {
|
||||
result := []rune(frameLine)
|
||||
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)
|
||||
lineLen := len(lineRunes)
|
||||
|
||||
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
|
||||
}
|
||||
for i := 0; i < len(lineRunes) && x+i < frameLen; i++ {
|
||||
result[x+i] = lineRunes[i]
|
||||
}
|
||||
|
||||
@@ -49,7 +39,6 @@ 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")
|
||||
}
|
||||
}
|
||||
@@ -179,43 +168,6 @@ func LogicValueInt(b bool, _if, _else int) int {
|
||||
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) {
|
||||
w, h, err := term.GetSize(0)
|
||||
if err != nil {
|
||||
|
||||
Reference in New Issue
Block a user