8 Commits
Author SHA1 Message Date
selimaj-dev 7e714abddf fixed color bug 2024-10-03 13:19:43 +02:00
selimaj-dev 3ceaa13b3f fixed color bug 2024-10-03 13:10:43 +02:00
selimaj-dev 8165e80b05 fixed ui when horizontal but color bug 2024-10-01 19:53:19 +02:00
selimaj-dev f77c04c908 fixed ui when horizontal 2024-10-01 16:37:54 +02:00
selimaj-dev 27530fe8cc fixed ui when horizontal 2024-10-01 16:34:50 +02:00
selimaj-dev 705bd8d61b ids and classes 2024-09-30 14:10:00 +02:00
RudideC bf70ceacf1 this seems to fix an issue i was facing 2024-09-30 10:10:32 +02:00
selimaj-dev 6cdb685eb5 small change 2024-09-29 22:13:54 +02:00
7 changed files with 129 additions and 86 deletions
+49 -41
View File
@@ -1,54 +1,62 @@
package main package main
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/ui" "github.com/orus-dev/osui/ui"
) )
func main() { func main() {
screen := osui.NewScreen(App()) screen := osui.NewScreen(App())
screen.Run() screen.Run()
} }
func App() *PaginatorComponent { func App() *ui.PaginatorComponent {
w, h := osui.GetTerminalSize() w, h := osui.GetTerminalSize()
return Paginator(
// Page 1 var keybindings ui.Class[*ui.TextComponent]
Div( keybindings.SetProperties(func(tc *ui.TextComponent) *ui.TextComponent {
Text("Welcome to the example! The blue text on the bottom show the keybindings"), tc.Text = colors.Blue + tc.Text
WithPosition(0, h-2, Text(colors.Blue+"Next Page: Tab. Previous Page: shift + tab")), tc.Data.Y = h - 2
), return tc
})
// Page 2 return ui.Paginator(
Div( // Page 1
WithPosition(0, 0, Text("This blue square is a div. A div stores multiple components into one")), ui.Div(
WithPosition((w-30)/2, (h-4)/2, ui.Text("Welcome to the example! The blue text on the bottom show the keybindings"),
WithSize(30, 5, keybindings.Class(ui.Text("Next Page: Tab. Previous Page: shift + tab")),
Div(WithPosition(4, 1, Button("Hello, World!").Params(BtnParams{Width: 19}))).Params(DivParams{Style: DivStyle{Outline: colors.Blue}}), ),
)),
WithPosition(0, h-2, Text(colors.Blue+"Navigate: Ctrl+(W/A/S/D)")),
),
// Page 3 // Page 2
Div( ui.Div(
WithPosition(0, 0, Text("This is a InputBox. It takes user input just like a GUI. "+colors.Red+colors.Bold+"NOTE: This InputBox is inside a div")), ui.WithPosition(0, 0, ui.Text("This blue square is a div. A div stores multiple components into one")),
WithPosition((w-32)/2, (h-3)/2, InputBox(30)), ui.WithPosition((w-30)/2, (h-4)/2,
WithPosition(0, h-2, Text(colors.Blue+"Navigate: W/S. Select: Enter. Exit Menu: Q. ")), ui.WithSize(30, 5,
), ui.Div(ui.WithPosition(4, 1, ui.Button("Hello, World!").Params(ui.ButtonParams{Width: 19}))).Params(ui.DivParams{Style: ui.DivStyle{Outline: colors.Blue}}),
)),
keybindings.Class(ui.Text("Navigate: Ctrl+(W/A/S/D)")),
),
// Page 4 // Page 3
Div( ui.Div(
WithPosition(0, 0, Text("This is a Button. To click it press Enter. "+colors.Red+colors.Bold+"NOTE: This Button is inside a div")), ui.WithPosition(0, 0, ui.Text("This is a InputBox. It takes user input just like a GUI. "+colors.Red+colors.Bold+"NOTE: This InputBox is inside a div")),
WithPosition((w-20)/2, (h-3)/2, Button("This is a button")), ui.WithPosition((w-32)/2, (h-3)/2, ui.InputBox(30)),
WithPosition(0, h-2, Text(colors.Blue+"Navigate: Ctrl+(W/A/S/D). Click Button: Enter")), keybindings.Class(ui.Text("Navigate: Ctrl+(W/A/S/D). Finish: Enter.")),
), ),
// Page 5 // Page 4
Div( ui.Div(
WithPosition(0, 0, Text("This is a Menu. It prompts you to select a item. "+colors.Red+colors.Bold+"NOTE: This Menu is inside a div")), ui.WithPosition(0, 0, ui.Text("This is a Button. To click it press Enter. "+colors.Red+colors.Bold+"NOTE: This Button is inside a div")),
WithPosition(0, 2, Menu("Item 1", "Item 2", "Item 3", "Item 4", "Item 5").Params(MenuParams{})), ui.WithPosition((w-20)/2, (h-3)/2, ui.Button("This is a button")),
WithPosition(0, h-2, Text(colors.Blue+"Navigate: Ctrl+(W/A/S/D). Navigate Menu: W/S. Select: Enter. Exit Menu: Q. ")), keybindings.Class(ui.Text("Navigate: Ctrl+(W/A/S/D). Click Button: Enter")),
), ),
)
// Page 5
ui.Div(
ui.WithPosition(0, 0, ui.Text("This is a Menu. It prompts you to select an item. "+colors.Red+colors.Bold+"NOTE: This Menu is inside a div")),
ui.WithPosition(0, 2, ui.Menu("Item 1", "Item 2", "Item 3", "Item 4", "Item 5").Params(ui.MenuParams{OnSelected: func(mc *ui.MenuComponent, b bool) {}})),
keybindings.Class(ui.Text("Navigate: Ctrl+(W/A/S/D). Navigate Menu: W/S. Select: Enter. Exit Menu: Q. ")),
),
)
} }
+1 -1
View File
@@ -5,6 +5,6 @@ go 1.23.1
require golang.org/x/term v0.24.0 // direct require golang.org/x/term v0.24.0 // direct
require ( require (
github.com/nathan-fiscaletti/consolesize-go v0.0.0-20220204101620-317176b6684d // indirect github.com/nathan-fiscaletti/consolesize-go v0.0.0-20220204101620-317176b6684d
golang.org/x/sys v0.25.0 // indirect golang.org/x/sys v0.25.0 // indirect
) )
+1 -1
View File
@@ -34,7 +34,6 @@ func NewScreen(c Component) *Screen {
} }
func (s *Screen) Render() { func (s *Screen) Render() {
Clear()
width, height := GetTerminalSize() width, height := GetTerminalSize()
frame := NewFrame(width, height) frame := NewFrame(width, height)
data := s.component.GetComponentData() data := s.component.GetComponentData()
@@ -48,6 +47,7 @@ func (s *Screen) Render() {
data.IsActive = true data.IsActive = true
data.DefaultColor = colors.Reset data.DefaultColor = colors.Reset
RenderOnFrame(s.component, &frame) RenderOnFrame(s.component, &frame)
Clear()
fmt.Print(strings.Join(frame, "\n")) fmt.Print(strings.Join(frame, "\n"))
} }
+2 -2
View File
@@ -10,7 +10,7 @@ import (
"github.com/orus-dev/osui/isKey" "github.com/orus-dev/osui/isKey"
) )
type BtnParams struct { type ButtonParams struct {
Style ButtonStyle Style ButtonStyle
Toggle bool Toggle bool
OnClick func(*ButtonComponent) bool OnClick func(*ButtonComponent) bool
@@ -92,7 +92,7 @@ func (b *ButtonComponent) GetComponentData() *osui.ComponentData {
return &b.Data return &b.Data
} }
func (b *ButtonComponent) Params(param BtnParams) *ButtonComponent { func (b *ButtonComponent) Params(param ButtonParams) *ButtonComponent {
b.Style = osui.SetDefaults(&param.Style).(*ButtonStyle) b.Style = osui.SetDefaults(&param.Style).(*ButtonStyle)
if param.OnClick != nil { if param.OnClick != nil {
b.on_click = param.OnClick b.on_click = param.OnClick
+5 -4
View File
@@ -27,7 +27,7 @@ type MenuComponent struct {
Style *MenuStyle Style *MenuStyle
Items []string Items []string
SelectedItem int SelectedItem int
onSelected func(*MenuComponent, bool) OnSelected func(*MenuComponent, bool)
} }
func (m *MenuComponent) GetComponentData() *osui.ComponentData { func (m *MenuComponent) GetComponentData() *osui.ComponentData {
@@ -42,7 +42,7 @@ func (m *MenuComponent) Render() string {
if !m.Data.IsActive { if !m.Data.IsActive {
cursor = m.Style.CursorInactive cursor = m.Style.CursorInactive
} }
d, _ := osui.CompressString(m.Style.Cursor, "") d, _ := osui.CompressString(m.Style.Cursor)
empty := strings.Repeat(" ", len(d)) empty := strings.Repeat(" ", len(d))
for i, item := range m.Items { for i, item := range m.Items {
@@ -70,10 +70,10 @@ func (m *MenuComponent) Update(key string) bool {
m.SelectedItem = len(m.Items) - 1 m.SelectedItem = len(m.Items) - 1
} }
} else if isKey.Enter(key) { } else if isKey.Enter(key) {
m.onSelected(m, true) m.OnSelected(m, true)
return true return true
} else if isKey.Char(key, "q") { } else if isKey.Char(key, "q") {
m.onSelected(m, false) m.OnSelected(m, false)
return true return true
} }
return false return false
@@ -81,6 +81,7 @@ func (m *MenuComponent) Update(key string) bool {
func (b *MenuComponent) Params(param MenuParams) *MenuComponent { func (b *MenuComponent) Params(param MenuParams) *MenuComponent {
b.Style = osui.SetDefaults(&param.Style).(*MenuStyle) b.Style = osui.SetDefaults(&param.Style).(*MenuStyle)
b.OnSelected = param.OnSelected
return b return b
} }
+32
View File
@@ -81,3 +81,35 @@ func findClosestComponent(a []osui.Component, i int, d string) int {
return closestIndex return closestIndex
} }
type Id[T osui.Component] struct {
changer func(T) T
Component T
}
func (i *Id[T]) Id(c T) T {
i.Component = c
if i.changer != nil {
return i.changer(i.Component)
}
return i.Component
}
func (i *Id[T]) SetProperties(c func(T) T) {
i.changer = c
}
type Class[T osui.Component] struct {
changer func(T) T
}
func (c *Class[T]) Class(co T) T {
if c.changer != nil {
return c.changer(co)
}
return co
}
func (c *Class[T]) SetProperties(ch func(T) T) {
c.changer = ch
}
+39 -37
View File
@@ -14,60 +14,61 @@ import (
"golang.org/x/term" "golang.org/x/term"
) )
func renderLine(frameLine, line_ string, x int) string { func RenderLine(frame_, line_ string, x int, fm map[int]string, lm map[int]string) string {
frame := []rune(frameLine) var res strings.Builder
frame := []rune(frame_)
line := []rune(line_) line := []rune(line_)
i := 0 for i := range frame {
v := 0 if i >= x && i-x < len(line) {
for j, c := range frame { if v, ok := lm[i-x]; ok {
if i+j >= x && v < len(line) { res.WriteString(v)
if c != '\b' && line[v] == '\t' {
frame = append(frame[:i+j], append([]rune{' '}, frame[i+j:]...)...)
} }
frame[i+j] = line[v] res.WriteRune(line[i-x])
v++ } else {
if v, ok := fm[i]; ok {
res.WriteString(v)
}
res.WriteRune(frame[i])
} }
if c == '\b' { }
return res.String()
}
func CompressString(input string) (string, map[int]string) {
pattern := `(\x1b\[([0-9;]*)[a-zA-Z])+`
re := regexp.MustCompile(pattern)
matchesMap := make(map[int]string)
res := []rune{}
i := 0
for i < len([]rune(input)) {
loc := re.FindStringIndex(string([]rune(input)[i:]))
if loc != nil && loc[0] == 0 {
ansiSeq := re.FindString(string([]rune(input)[i:]))
matchesMap[len(res)] = ansiSeq
i += len([]rune(ansiSeq))
} else {
res = append(res, []rune(input)[i])
i++ i++
} }
} }
return string(frame) return string(res), matchesMap
} }
func RenderOnFrame(c Component, frame *[]string) { func RenderOnFrame(c Component, frame *[]string) {
componentData := c.GetComponentData() componentData := c.GetComponentData()
for i, lo := range strings.Split(c.Render(), "\n") { for i, line := range strings.Split(c.Render(), "\n") {
if int(componentData.Y)+i < len(*frame) { if int(componentData.Y)+i < len(*frame) {
fo := (*frame)[int(componentData.Y)+i] fo, fm := CompressString((*frame)[int(componentData.Y)+i])
f, fa := CompressString(fo, "\b") lo, lm := CompressString(line)
line, la := CompressString(lo, "\t") (*frame)[int(componentData.Y)+i] = RenderLine(fo, lo, componentData.X, fm, lm)
r := renderLine(f, line, int(componentData.X))
(*frame)[int(componentData.Y)+i] = DecompressString(DecompressString(r, la, "\t"), fa, "\b")
} }
} }
} }
func CompressString(input, repl string) (string, []string) {
pattern := `\x1b\[([0-9;]*)[a-zA-Z]`
re := regexp.MustCompile(pattern)
matches := re.FindAllString(input, -1)
return re.ReplaceAllString(input, repl), matches
}
func DecompressString(modified string, outputArray []string, c string) string {
parts := strings.Split(modified, c)
reconstructed := ""
for i, part := range parts {
reconstructed += part
if i < len(outputArray) {
reconstructed += outputArray[i]
}
}
return reconstructed
}
func ReadKey() (string, error) { func ReadKey() (string, error) {
oldState, err := term.MakeRaw(int(os.Stdin.Fd())) oldState, err := term.MakeRaw(int(os.Stdin.Fd()))
if err != nil { if err != nil {
@@ -174,6 +175,7 @@ func LogicValueInt(b bool, _if, _else int) int {
return _else return _else
} }
// Get the terminal size
func GetTerminalSize() (int, int) { func GetTerminalSize() (int, int) {
return consolesize.GetConsoleSize() return consolesize.GetConsoleSize()
} }