a few more changes

This commit is contained in:
2024-10-13 16:32:05 +02:00
parent 905bb6711e
commit 553804ec51
12 changed files with 392 additions and 310 deletions
+16 -12
View File
@@ -48,17 +48,21 @@ func (b *ButtonComponent) Render() string {
)
}
func (b *ButtonComponent) Update(key string) bool {
if f, ok := b.Data.Keys["click"]; ok && f(key) {
b.Data.OnClick()
if b.Data.Toggle {
b.clicked = !b.clicked
return false
func (b *ButtonComponent) Update(ctx osui.UpdateContext) bool {
if ctx.UpdateKind == osui.UpdateKindKey {
switch b.Data.Keys[ctx.Key.Name] {
case "click":
b.Data.OnClick()
if b.Data.Toggle {
b.clicked = !b.clicked
return false
}
b.clicked = true
b.Data.Screen.Render()
time.Sleep(time.Millisecond * 120)
b.clicked = false
}
b.clicked = true
b.Data.Screen.Render()
time.Sleep(time.Millisecond * 120)
b.clicked = false
} else if ctx.UpdateKind == osui.UpdateKindTick {
}
return false
@@ -69,8 +73,8 @@ func (b *ButtonComponent) GetComponentData() *osui.ComponentData {
}
func Button(param osui.Param, text string) *ButtonComponent {
param.SetDefaultBindings(map[string]func(string) bool{
"click": keys.Enter,
param.SetDefaultBindings(map[string]string{
keys.Enter: "click",
})
return param.UseParam(&ButtonComponent{Text: text,
Data: osui.ComponentData{
+29 -34
View File
@@ -8,21 +8,8 @@ import (
"github.com/orus-dev/osui/keys"
)
type DivParams struct {
Style DivStyle
Width int
Height int
}
type DivStyle struct {
Background string `default:"" type:"bg"`
Foreground string `default:"" type:"fg"`
Outline string `default:"" type:"fg"`
}
type DivComponent struct {
Data osui.ComponentData
Style *DivStyle
Components []osui.Component
ActiveComponent int
}
@@ -47,35 +34,43 @@ func (d *DivComponent) Render() string {
if data.Height == 0 {
data.Height = d.Data.Height - 2
}
data.DefaultColor = colors.Combine(d.Style.Background, d.Style.Foreground)
data.DefaultColor = colors.Combine(d.Data.Style.Background, d.Data.Style.Foreground)
data.Screen = d.Data.Screen
osui.RenderOnFrame(c, &frame)
}
if d.Style.Outline == "" {
if d.Data.Style.Outline == "" {
for i, f := range frame {
frame[i] = colors.Combine(d.Style.Foreground, d.Style.Background) + f + colors.Reset
frame[i] = colors.Combine(d.Data.Style.Foreground, d.Data.Style.Background) + f + colors.Reset
}
return strings.Join(frame, "\n")
} else {
for i, f := range frame {
frame[i] = d.Style.Outline + "│" + colors.Reset + colors.Combine(d.Style.Foreground, d.Style.Background) + f + colors.Reset + d.Style.Outline + "│" + colors.Reset
frame[i] = d.Data.Style.Outline + "│" + colors.Reset + colors.Combine(d.Data.Style.Foreground, d.Data.Style.Background) + f + colors.Reset + d.Data.Style.Outline + "│" + colors.Reset
}
}
return " " + d.Style.Outline + strings.Repeat("_", d.Data.Width-2) + colors.Reset + "\n" + strings.Join(frame, "\n") + "\n " + d.Style.Outline + strings.Repeat("‾", d.Data.Width-2) + colors.Reset
return " " + d.Data.Style.Outline + strings.Repeat("_", d.Data.Width-2) + colors.Reset + "\n" + strings.Join(frame, "\n") + "\n " + d.Data.Style.Outline + strings.Repeat("‾", d.Data.Width-2) + colors.Reset
}
func (d *DivComponent) Update(key string) bool {
if f, ok := d.Data.Keys["up"]; ok && f(key) {
d.updateActive(findClosestComponent(d.Components, d.ActiveComponent, "up"))
} else if f, ok := d.Data.Keys["down"]; ok && f(key) {
d.updateActive(findClosestComponent(d.Components, d.ActiveComponent, "down"))
} else if f, ok := d.Data.Keys["left"]; ok && f(key) {
d.updateActive(findClosestComponent(d.Components, d.ActiveComponent, "left"))
} else if f, ok := d.Data.Keys["right"]; ok && f(key) {
d.updateActive(findClosestComponent(d.Components, d.ActiveComponent, "right"))
} else if len(d.Components) > 0 {
func (d *DivComponent) Update(ctx osui.UpdateContext) bool {
if ctx.UpdateKind == osui.UpdateKindKey {
switch d.Data.Keys[ctx.Key.Name] {
case "up":
d.updateActive(findClosestComponent(d.Components, d.ActiveComponent, "up"))
case "down":
d.updateActive(findClosestComponent(d.Components, d.ActiveComponent, "down"))
case "left":
d.updateActive(findClosestComponent(d.Components, d.ActiveComponent, "left"))
case "right":
d.updateActive(findClosestComponent(d.Components, d.ActiveComponent, "right"))
default:
d.Components[d.ActiveComponent].GetComponentData().IsActive = d.Data.IsActive
if d.Components[d.ActiveComponent].Update(ctx) {
d.updateActive(findClosestComponent(d.Components, d.ActiveComponent, "down"))
}
}
} else if ctx.UpdateKind == osui.UpdateKindTick {
d.Components[d.ActiveComponent].GetComponentData().IsActive = d.Data.IsActive
if d.Components[d.ActiveComponent].Update(key) {
if d.Components[d.ActiveComponent].Update(ctx) {
d.updateActive(findClosestComponent(d.Components, d.ActiveComponent, "down"))
}
}
@@ -90,11 +85,11 @@ func (d *DivComponent) updateActive(newIndex int) {
}
func Div(param osui.Param, components ...osui.Component) *DivComponent {
param.SetDefaultBindings(map[string]func(string) bool{
"up": keys.CtrlW,
"down": keys.CtrlS,
"left": keys.CtrlA,
"right": keys.CtrlD,
param.SetDefaultBindings(map[string]string{
keys.CtrlW: "up",
keys.CtrlS: "down",
keys.CtrlA: "left",
keys.CtrlD: "right",
})
return param.UseParam(&DivComponent{
Components: components,
+32 -33
View File
@@ -9,12 +9,6 @@ import (
"github.com/orus-dev/osui/keys"
)
type InputBoxParams struct {
Style DivStyle
Width int
Height int
}
type InputBoxComponent struct {
Data osui.ComponentData
max_size uint
@@ -33,7 +27,7 @@ func (s InputBoxComponent) Render() string {
" %s\n%s│%s%s│%s\n %s",
colors.Reset+s.Data.Style.Outline+strings.Repeat("_", int(s.max_size))+colors.Reset,
colors.Reset+s.Data.Style.Outline,
colors.Combine(s.Data.Style.Foreground, s.Data.Style.Background)+s.InputData+osui.LogicValue(s.Data.IsActive, s.Data.Style.Cursor+"█"+colors.Combine(s.Data.Style.Foreground, s.Data.Style.Background), ""),
colors.Combine(s.Data.Style.Foreground, s.Data.Style.Background)+s.InputData+osui.LogicValue(s.Data.IsActive, s.Data.Style.CursorColor+"█"+colors.Combine(s.Data.Style.Foreground, s.Data.Style.Background), ""),
strings.Repeat(" ", int(s.max_size)-len(s.InputData)-osui.LogicValueInt(s.Data.IsActive, 1, 0))+colors.Reset+s.Data.Style.Outline,
colors.Reset+s.Data.DefaultColor,
s.Data.Style.Outline+strings.Repeat("‾", int(s.max_size))+colors.Reset+s.Data.DefaultColor,
@@ -44,43 +38,48 @@ func (s InputBoxComponent) Render() string {
" %s\n%s│%s%s\n %s",
colors.Reset+s.Data.Style.Outline+strings.Repeat("_", int(s.max_size))+colors.Reset,
colors.Reset+s.Data.Style.Outline,
colors.Combine(s.Data.Style.Foreground, s.Data.Style.Background)+s.InputData+osui.LogicValue(s.Data.IsActive, s.Data.Style.Cursor+"█"+colors.Reset, s.Data.Style.Outline+"|"+colors.Reset),
colors.Combine(s.Data.Style.Foreground, s.Data.Style.Background)+s.InputData+osui.LogicValue(s.Data.IsActive, s.Data.Style.CursorColor+"█"+colors.Reset, s.Data.Style.Outline+"|"+colors.Reset),
colors.Reset+s.Data.DefaultColor,
colors.Reset+s.Data.Style.Outline+strings.Repeat("‾", int(s.max_size))+colors.Reset+s.Data.DefaultColor,
)
}
func (s *InputBoxComponent) Update(key string) bool {
if f, ok := s.Data.Keys["done"]; ok && f(key) {
return true
} else if f, ok := s.Data.Keys["remove"]; ok && f(key) {
if len(s.InputData) > 0 {
s.InputData = s.InputData[:len(s.InputData)-1]
}
} else if f, ok := s.Data.Keys["left"]; ok && f(key) {
if s.cursor > 0 {
s.cursor--
}
} else if f, ok := s.Data.Keys["right"]; ok && f(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
func (s *InputBoxComponent) Update(ctx osui.UpdateContext) bool {
if ctx.UpdateKind == osui.UpdateKindKey {
switch s.Data.Keys[ctx.Key.Name] {
case "done":
s.Data.OnClick()
return true
case "remove":
if len(s.InputData) > 0 {
s.InputData = s.InputData[:len(s.InputData)-1]
}
case "left":
if s.cursor > 0 {
s.cursor--
}
case "right":
if s.cursor < s.max_size {
s.cursor++
}
default:
if ctx.Key.Chars[1] == '\x00' {
if int(s.max_size) > len(s.InputData) {
s.InputData += string(ctx.Key.Chars[0])
}
}
}
}
return false
}
func InputBox(param osui.Param, max_size uint) *InputBoxComponent {
param.SetDefaultBindings(map[string]func(string) bool{
"done": keys.Enter,
"remove": keys.Backspace,
"left": keys.Left,
"right": keys.Right,
param.SetDefaultBindings(map[string]string{
keys.Enter: "done",
keys.Backspace: "remove",
keys.Left: "left",
keys.Right: "right",
})
return param.UseParam(&InputBoxComponent{max_size: max_size}).(*InputBoxComponent)
return param.UseParam(&InputBoxComponent{max_size: max_size, Data: osui.ComponentData{}}).(*InputBoxComponent)
}
+26 -21
View File
@@ -31,41 +31,46 @@ func (m *MenuComponent) Render() string {
for i, item := range m.Items {
if i == m.SelectedItem {
res = append(res, cursor+colors.Combine(m.Data.Style.SelectedBackground, m.Data.Style.SelectedBackground)+item+colors.Reset+m.Data.DefaultColor)
res = append(res, colors.Reset+cursor+m.Data.Style.SelectedBackground+m.Data.Style.SelectedForeground+item+"\x1b[0m ")
} else {
res = append(res, empty+colors.Combine(m.Data.Style.Foreground, m.Data.Style.Background)+item+colors.Reset+m.Data.DefaultColor)
res = append(res, colors.Reset+empty+m.Data.Style.Foreground+m.Data.Style.Background+item)
}
}
return strings.Join(res, "\n")
}
func (m *MenuComponent) Update(key string) bool {
if f, ok := m.Data.Keys["up"]; ok && f(key) {
if m.SelectedItem > 0 {
m.SelectedItem--
} else {
m.SelectedItem = len(m.Items) - 1
func (m *MenuComponent) Update(ctx osui.UpdateContext) bool {
if ctx.UpdateKind == osui.UpdateKindKey {
switch m.Data.Keys[ctx.Key.Name] {
case "up":
if m.SelectedItem > 0 {
m.SelectedItem--
} else {
m.SelectedItem = len(m.Items) - 1
}
case "down":
if m.SelectedItem+1 < len(m.Items) {
m.SelectedItem++
} else {
m.SelectedItem = 0
}
case "select":
m.Data.OnClick()
return true
case "exit":
return true
}
} else if f, ok := m.Data.Keys["down"]; ok && f(key) {
if m.SelectedItem+1 < len(m.Items) {
m.SelectedItem++
} else {
m.SelectedItem = 0
}
} else if f, ok := m.Data.Keys["select"]; ok && f(key) {
m.Data.OnClick()
return true
}
return false
}
func Menu(param osui.Param, items ...string) *MenuComponent {
param.SetDefaultBindings(map[string]func(string) bool{
"up": keys.Up,
"down": keys.Down,
"select": keys.Enter,
param.SetDefaultBindings(map[string]string{
keys.Up: "up",
keys.Down: "down",
keys.Enter: "select",
})
return param.UseParam(&MenuComponent{
Items: items,
+35 -43
View File
@@ -9,20 +9,8 @@ import (
"github.com/orus-dev/osui/keys"
)
type PaginatorParams struct {
Style DivStyle
Width int
Height int
}
type PaginatorStyle struct {
Active string `default:"\033[32m"`
Inactive string `default:""`
}
type PaginatorComponent struct {
Data osui.ComponentData
Style *PaginatorStyle
Components []osui.Component
ActiveComponent int
}
@@ -39,10 +27,10 @@ func (p *PaginatorComponent) Render() string {
for i, c := range p.Components {
data := c.GetComponentData()
if i == p.ActiveComponent {
pgs += p.Style.Active + "•" + colors.Reset
pgs += p.Data.Style.SelectedForeground + data.Style.SelectedBackground + "•" + colors.Reset
data.IsActive = true
} else {
pgs += colors.Reset + p.Style.Inactive + "•"
pgs += colors.Reset + p.Data.Style.Foreground + data.Style.Background + "•"
data.IsActive = false
}
if data.Width == 0 {
@@ -58,33 +46,37 @@ func (p *PaginatorComponent) Render() string {
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"))
}
func (p *PaginatorComponent) Update(key string) bool {
if f, ok := p.Data.Keys["previous"]; ok && f(key) {
if p.ActiveComponent > 0 {
p.updateActive(p.ActiveComponent - 1)
} else {
p.updateActive(len(p.Components) - 1)
}
} else if f, ok := p.Data.Keys["next"]; ok && f(key) {
if p.ActiveComponent < len(p.Components)-1 {
p.updateActive(p.ActiveComponent + 1)
} else {
p.updateActive(0)
}
} else if f, ok := p.Data.Keys["exit"]; ok && f(key) {
fmt.Print("\n\n")
return true
} else {
if len(p.Components) > 0 {
p.Components[p.ActiveComponent].GetComponentData().IsActive = p.Data.IsActive
if p.Components[p.ActiveComponent].Update(key) {
if p.ActiveComponent < len(p.Components)-1 {
p.updateActive(p.ActiveComponent + 1)
} else {
p.updateActive(0)
func (p *PaginatorComponent) Update(ctx osui.UpdateContext) bool {
if ctx.UpdateKind == osui.UpdateKindKey {
switch p.Data.Keys[ctx.Key.Name] {
case "previous":
if p.ActiveComponent > 0 {
p.updateActive(p.ActiveComponent - 1)
} else {
p.updateActive(len(p.Components) - 1)
}
case "next":
if p.ActiveComponent < len(p.Components)-1 {
p.updateActive(p.ActiveComponent + 1)
} else {
p.updateActive(0)
}
case "exit":
p.Data.OnClick()
fmt.Print("\n\n")
return true
default:
if len(p.Components) > 0 {
p.Components[p.ActiveComponent].GetComponentData().IsActive = p.Data.IsActive
if p.Components[p.ActiveComponent].Update(ctx) {
if p.ActiveComponent < len(p.Components)-1 {
p.updateActive(p.ActiveComponent + 1)
} else {
p.updateActive(0)
}
}
}
}
@@ -99,10 +91,10 @@ func (p *PaginatorComponent) updateActive(newIndex int) {
}
func Paginator(param osui.Param, pages ...osui.Component) *PaginatorComponent {
param.SetDefaultBindings(map[string]func(string) bool{
"previous": keys.ShiftTab,
"next": keys.Tab,
"exit": keys.Escape,
param.SetDefaultBindings(map[string]string{
keys.ShiftTab: "previous",
keys.Tab: "next",
keys.Escape: "exit",
})
return param.UseParam(&PaginatorComponent{
Components: pages,
+2 -2
View File
@@ -9,7 +9,7 @@ type TextComponent struct {
Text string
}
func (t *TextComponent) Update(key string) bool {
func (t *TextComponent) Update(ctx osui.UpdateContext) bool {
return false
}
@@ -22,6 +22,6 @@ func (t TextComponent) Render() string {
}
func Text(param osui.Param, text string) *TextComponent {
param.SetDefaultBindings(map[string]func(string) bool{})
param.SetDefaultBindings(map[string]string{})
return param.UseParam(&TextComponent{Text: text}).(*TextComponent)
}