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
-1
View File
@@ -39,7 +39,6 @@ func AsBg(s string) string {
}) })
} }
func AsFg(s string) string { func AsFg(s string) string {
re, _ := regexp.Compile(`\x1b\[4([0-9]+)`) re, _ := regexp.Compile(`\x1b\[4([0-9]+)`)
return re.ReplaceAllStringFunc(s, func(match string) string { return re.ReplaceAllStringFunc(s, func(match string) string {
+126
View File
@@ -0,0 +1,126 @@
package osui
import (
"github.com/orus-dev/osui/colors"
)
type Style struct {
Background string `json:"background"`
Foreground string `json:"foreground"`
Outline string `json:"outline"`
ActiveBackground string `json:"activeBackground"`
ActiveForeground string `json:"activeForeground"`
ActiveOutline string `json:"activeOutline"`
ClickedBackground string `json:"clickedBackground"`
ClickedForeground string `json:"clickedForeground"`
ClickedOutline string `json:"clickedOutline"`
SelectedBackground string `json:"selectedBackground"`
SelectedForeground string `json:"selectedForeground"`
SelectedOutline string `json:"selectedOutline"`
Cursor string `json:"cursor"`
CursorColor string `json:"cursorColor"`
InactiveCursor string `json:"inactiveCursor"`
}
func (s *Style) SetDefaults() {
defaultValue(&s.Cursor, "> ")
defaultValue(&s.SelectedForeground, colors.Black)
defaultValue(&s.SelectedBackground, colors.Magenta)
defaultValue(&s.ActiveOutline, colors.Blue)
defaultValue(&s.ClickedForeground, colors.Blue)
defaultValue(&s.ClickedOutline, colors.Blue)
s.UseStyle()
}
func (s *Style) UseStyle() {
s.Foreground = colors.AsFg(s.Foreground)
s.Background = colors.AsBg(s.Background)
s.Outline = colors.AsFg(s.Outline)
s.ActiveForeground = colors.AsFg(s.ActiveForeground)
s.ActiveBackground = colors.AsBg(s.ActiveBackground)
s.ActiveOutline = colors.AsFg(s.ActiveOutline)
s.ClickedForeground = colors.AsFg(s.ClickedForeground)
s.ClickedBackground = colors.AsBg(s.ClickedBackground)
s.ClickedOutline = colors.AsFg(s.ClickedOutline)
s.SelectedForeground = colors.AsFg(s.SelectedForeground)
s.SelectedBackground = colors.AsBg(s.SelectedBackground)
s.SelectedOutline = colors.AsFg(s.SelectedOutline)
}
func defaultValue(sd *string, s string) {
if *sd == "" {
*sd = s
}
}
type Param struct {
Style Style `json:"style"`
X int `json:"y"`
Y int `json:"x"`
Width int `json:"width"`
Height int `json:"height"`
OnClick func()
Keys map[string]string
Toggle bool `json:"useToggle"`
}
func (p *Param) SetDefaultBindings(keys map[string]string) {
p.Style.SetDefaults()
if p.Keys == nil {
p.Keys = keys
return
}
for k, f := range keys {
if _, ok := p.Keys[k]; !ok {
p.Keys[k] = f
}
}
}
func (p *Param) UseParam(c Component) Component {
data := c.GetComponentData()
p.Style.SetDefaults()
data.Style = p.Style
data.X = p.X
data.Y = p.Y
if p.Height != 0 {
data.Height = p.Height
}
if p.Width != 0 {
data.Width = p.Width
}
if p.OnClick != nil {
data.OnClick = p.OnClick
} else {
data.OnClick = func() {}
}
data.Toggle = p.Toggle
data.Keys = p.Keys
return c
}
type ComponentData struct {
X int
Y int
Width int
Height int
DefaultColor string
IsActive bool
Style Style
OnClick func()
Keys map[string]string
Toggle bool
Screen *Screen
}
+34 -2
View File
@@ -6,9 +6,41 @@ import (
) )
func main() { func main() {
// screen := osui.NewScreen()
// my_btn := ui.Button(osui.Param{},
// "Button",
// )
// screen.Run(my_btn)
// var menu ui.Id[*ui.MenuComponent]
// screen := osui.NewScreen()
// screen.Run(ui.Div(osui.Param{},
// menu.Id(ui.Menu(osui.Param{OnClick: func() {
// screen.SetComponent(ui.Text(osui.Param{}, menu.Component.Items[menu.Component.SelectedItem]))
// screen.Exit()
// }},
// "Item 1", "Item 24324322", "Item 366",
// )),
// ))
// screen := osui.NewScreen()
// my_btn := ui.InputBox(osui.Param{},
// 24,
// )
// screen.Run(my_btn)
screen := osui.NewScreen() screen := osui.NewScreen()
my_btn := ui.Menu(osui.Param{Style: osui.Style{Cursor: "> "}},
"item 1", "item 2", "item 3", "item 4", my_btn := ui.Paginator(osui.Param{OnClick: func() {screen.Exit()}},
ui.Div(osui.Param{},
ui.Text(osui.Param{}, "Page 1"),
),
ui.Div(osui.Param{},
ui.Text(osui.Param{}, "Page 2"),
),
) )
screen.Run(my_btn) screen.Run(my_btn)
} }
+50 -52
View File
@@ -1,57 +1,55 @@
package keys package keys
func Enter(s string) bool { const (
return s == "\r" || s == "\n" Enter = "Enter"
Tab = "Tab"
ShiftTab = "ShiftTab"
Backspace = "Backspace"
Escape = "Escape"
Up = "Up"
Down = "Down"
Left = "Left"
Right = "Right"
CtrlW = "CtrlW"
CtrlS = "CtrlS"
CtrlA = "CtrlA"
CtrlD = "CtrlD"
Error = "Error"
)
func Char(key string) string {
return "Char(" + key + ")"
} }
func Tab(s string) bool { func GetKeyName(key string) string {
return s == "\t" switch key {
case "\r", "\n":
return Enter
case "\x7f", "\b":
return Backspace
case "\t":
return Tab
case "\x1b[Z":
return ShiftTab
case "\x1b":
return Escape
case "\x1b[A":
return Up
case "\x1b[B":
return Down
case "\x1b[C":
return Right
case "\x1b[D":
return Left
case "\x17":
return CtrlW
case "\x13":
return CtrlS
case "\x01":
return CtrlA
case "\x04":
return CtrlD
default:
return Char(key)
}
} }
func ShiftTab(s string) bool {
return s == "\x1b[Z"
}
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"
}
func Char(s string, s1 string) bool {
return s == s1
}
func CtrlW(s string) bool {
return s == "\x17"
}
func CtrlS(s string) bool {
return s == "\x13"
}
func CtrlA(s string) bool {
return s == "\x01"
}
func CtrlD(s string) bool {
return s == "\x04"
}
+38 -107
View File
@@ -4,127 +4,39 @@ import (
"fmt" "fmt"
"os" "os"
"strings" "strings"
"time"
"github.com/orus-dev/osui/colors" "github.com/orus-dev/osui/colors"
"golang.org/x/term" "golang.org/x/term"
) )
type Style struct { type Key struct {
Background string `json:"background"` Chars [3]rune
Foreground string `json:"foreground"` Name string
Outline string `json:"outline"`
ActiveBackground string `json:"activeBackground"`
ActiveForeground string `json:"activeForeground"`
ActiveOutline string `json:"activeOutline"`
ClickedBackground string `json:"clickedBackground"`
ClickedForeground string `json:"clickedForeground"`
ClickedOutline string `json:"clickedOutline"`
SelectedBackground string `json:"selectedBackground"`
SelectedForeground string `json:"selectedForeground"`
SelectedOutline string `json:"selectedOutline"`
Cursor string `json:"cursor"`
InactiveCursor string `json:"inactiveCursor"`
} }
func (s *Style) SetDefaults() { type UpdateContext struct {
if s.Cursor != "" { UpdateKind uint8
s.Cursor = "> " Tick uint8
} Key Key
}
func (s *Style) UseStyle() {
s.Background = colors.AsBg(s.Background)
s.Foreground = colors.AsBg(s.Foreground)
s.Outline = colors.AsBg(s.Outline)
s.ActiveBackground = colors.AsBg(s.ActiveBackground)
s.ActiveForeground = colors.AsBg(s.ActiveForeground)
s.ActiveOutline = colors.AsBg(s.ActiveOutline)
s.ClickedBackground = colors.AsBg(s.ClickedBackground)
s.ClickedForeground = colors.AsFg(s.ClickedForeground)
s.ClickedOutline = colors.AsFg(s.ClickedOutline)
}
type Param struct {
Style Style `json:"style"`
X int `json:"y"`
Y int `json:"x"`
Width int `json:"width"`
Height int `json:"height"`
OnClick func()
Keys map[string]func(string) bool
Toggle bool `json:"useToggle"`
}
func (p *Param) SetDefaultBindings(keys map[string]func(string) bool) {
if p.Keys == nil {
p.Keys = keys
return
}
for k, f := range keys {
if _, ok := p.Keys[k]; !ok {
p.Keys[k] = f
}
}
}
func (p *Param) UseParam(c Component) Component {
data := c.GetComponentData()
p.Style.SetDefaults()
data.Style = p.Style
data.X = p.X
data.Y = p.Y
if p.Height != 0 {
data.Height = p.Height
}
if p.Width != 0 {
data.Width = p.Width
}
if p.OnClick != nil {
data.OnClick = p.OnClick
} else {
data.OnClick = func() {}
}
data.Toggle = p.Toggle
data.Keys = p.Keys
return c
}
type ComponentData struct {
X int
Y int
Width int
Height int
DefaultColor string
IsActive bool
Style Style
OnClick func()
Keys map[string]func(string) bool
Toggle bool
Screen *Screen
} }
type Component interface { type Component interface {
Render() string Render() string
GetComponentData() *ComponentData GetComponentData() *ComponentData
Update(string) bool Update(UpdateContext) bool
} }
type Screen struct { type Screen struct {
Component Component Component Component
tickRate uint8
running bool
} }
func NewScreen() *Screen { func NewScreen() *Screen {
return &Screen{} s := Screen{}
s.tickRate = 1
return &s
} }
func (s *Screen) Render() { func (s *Screen) Render() {
@@ -150,13 +62,22 @@ func (s *Screen) Run(c Component) {
panic(err) panic(err)
} }
defer term.Restore(int(os.Stdin.Fd()), oldState) defer term.Restore(int(os.Stdin.Fd()), oldState)
for { s.running = true
for s.running {
s.Render() s.Render()
k, _ := ReadKey() s.Component.Update(UpdateContext{UpdateKind: UpdateKindKey, Key: ReadKey()})
if s.Component.Update(k) { }
ShowCursor() defer term.Restore(int(os.Stdin.Fd()), oldState)
return }
func (s *Screen) _() {
var tick uint8 = 0
for s.running {
s.Component.Update(UpdateContext{UpdateKind: UpdateKindTick, Tick: tick})
if tick == 255 {
tick = 0
} }
time.Sleep(time.Duration(1000/int(s.tickRate)) * time.Millisecond)
} }
} }
@@ -174,3 +95,13 @@ func (s *Screen) SetComponent(c Component) {
data.IsActive = true data.IsActive = true
data.DefaultColor = colors.Reset data.DefaultColor = colors.Reset
} }
func (s *Screen) Exit() {
s.running = false
ShowCursor()
}
const (
UpdateKindKey = uint8(iota)
UpdateKindTick
)
+16 -12
View File
@@ -48,17 +48,21 @@ func (b *ButtonComponent) Render() string {
) )
} }
func (b *ButtonComponent) Update(key string) bool { func (b *ButtonComponent) Update(ctx osui.UpdateContext) bool {
if f, ok := b.Data.Keys["click"]; ok && f(key) { if ctx.UpdateKind == osui.UpdateKindKey {
b.Data.OnClick() switch b.Data.Keys[ctx.Key.Name] {
if b.Data.Toggle { case "click":
b.clicked = !b.clicked b.Data.OnClick()
return false 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 } else if ctx.UpdateKind == osui.UpdateKindTick {
b.Data.Screen.Render()
time.Sleep(time.Millisecond * 120)
b.clicked = false
} }
return false return false
@@ -69,8 +73,8 @@ func (b *ButtonComponent) GetComponentData() *osui.ComponentData {
} }
func Button(param osui.Param, text string) *ButtonComponent { func Button(param osui.Param, text string) *ButtonComponent {
param.SetDefaultBindings(map[string]func(string) bool{ param.SetDefaultBindings(map[string]string{
"click": keys.Enter, keys.Enter: "click",
}) })
return param.UseParam(&ButtonComponent{Text: text, return param.UseParam(&ButtonComponent{Text: text,
Data: osui.ComponentData{ Data: osui.ComponentData{
+29 -34
View File
@@ -8,21 +8,8 @@ import (
"github.com/orus-dev/osui/keys" "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 { type DivComponent struct {
Data osui.ComponentData Data osui.ComponentData
Style *DivStyle
Components []osui.Component Components []osui.Component
ActiveComponent int ActiveComponent int
} }
@@ -47,35 +34,43 @@ func (d *DivComponent) Render() string {
if data.Height == 0 { if data.Height == 0 {
data.Height = d.Data.Height - 2 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 data.Screen = d.Data.Screen
osui.RenderOnFrame(c, &frame) osui.RenderOnFrame(c, &frame)
} }
if d.Style.Outline == "" { if d.Data.Style.Outline == "" {
for i, f := range frame { 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") return strings.Join(frame, "\n")
} else { } else {
for i, f := range frame { 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 { func (d *DivComponent) Update(ctx osui.UpdateContext) bool {
if f, ok := d.Data.Keys["up"]; ok && f(key) { if ctx.UpdateKind == osui.UpdateKindKey {
d.updateActive(findClosestComponent(d.Components, d.ActiveComponent, "up")) switch d.Data.Keys[ctx.Key.Name] {
} else if f, ok := d.Data.Keys["down"]; ok && f(key) { case "up":
d.updateActive(findClosestComponent(d.Components, d.ActiveComponent, "down")) d.updateActive(findClosestComponent(d.Components, d.ActiveComponent, "up"))
} else if f, ok := d.Data.Keys["left"]; ok && f(key) { case "down":
d.updateActive(findClosestComponent(d.Components, d.ActiveComponent, "left")) d.updateActive(findClosestComponent(d.Components, d.ActiveComponent, "down"))
} else if f, ok := d.Data.Keys["right"]; ok && f(key) { case "left":
d.updateActive(findClosestComponent(d.Components, d.ActiveComponent, "right")) d.updateActive(findClosestComponent(d.Components, d.ActiveComponent, "left"))
} else if len(d.Components) > 0 { 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 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")) 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 { func Div(param osui.Param, components ...osui.Component) *DivComponent {
param.SetDefaultBindings(map[string]func(string) bool{ param.SetDefaultBindings(map[string]string{
"up": keys.CtrlW, keys.CtrlW: "up",
"down": keys.CtrlS, keys.CtrlS: "down",
"left": keys.CtrlA, keys.CtrlA: "left",
"right": keys.CtrlD, keys.CtrlD: "right",
}) })
return param.UseParam(&DivComponent{ return param.UseParam(&DivComponent{
Components: components, Components: components,
+32 -33
View File
@@ -9,12 +9,6 @@ import (
"github.com/orus-dev/osui/keys" "github.com/orus-dev/osui/keys"
) )
type InputBoxParams struct {
Style DivStyle
Width int
Height int
}
type InputBoxComponent struct { type InputBoxComponent struct {
Data osui.ComponentData Data osui.ComponentData
max_size uint max_size uint
@@ -33,7 +27,7 @@ func (s InputBoxComponent) Render() string {
" %s\n%s│%s%s│%s\n %s", " %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+strings.Repeat("_", int(s.max_size))+colors.Reset,
colors.Reset+s.Data.Style.Outline, 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, 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, colors.Reset+s.Data.DefaultColor,
s.Data.Style.Outline+strings.Repeat("‾", int(s.max_size))+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", " %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+strings.Repeat("_", int(s.max_size))+colors.Reset,
colors.Reset+s.Data.Style.Outline, 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.DefaultColor,
colors.Reset+s.Data.Style.Outline+strings.Repeat("‾", int(s.max_size))+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 { func (s *InputBoxComponent) Update(ctx osui.UpdateContext) bool {
if f, ok := s.Data.Keys["done"]; ok && f(key) { if ctx.UpdateKind == osui.UpdateKindKey {
return true switch s.Data.Keys[ctx.Key.Name] {
} else if f, ok := s.Data.Keys["remove"]; ok && f(key) { case "done":
if len(s.InputData) > 0 { s.Data.OnClick()
s.InputData = s.InputData[:len(s.InputData)-1] return true
} case "remove":
} else if f, ok := s.Data.Keys["left"]; ok && f(key) { if len(s.InputData) > 0 {
if s.cursor > 0 { s.InputData = s.InputData[:len(s.InputData)-1]
s.cursor-- }
} case "left":
} else if f, ok := s.Data.Keys["right"]; ok && f(key) { if s.cursor > 0 {
if s.cursor < s.max_size { s.cursor--
s.cursor++ }
} case "right":
} else { if s.cursor < s.max_size {
if len(key) == 1 { s.cursor++
if int(s.max_size) > len(s.InputData) { }
s.InputData += key default:
if ctx.Key.Chars[1] == '\x00' {
if int(s.max_size) > len(s.InputData) {
s.InputData += string(ctx.Key.Chars[0])
}
} }
} }
} }
return false return false
} }
func InputBox(param osui.Param, max_size uint) *InputBoxComponent { func InputBox(param osui.Param, max_size uint) *InputBoxComponent {
param.SetDefaultBindings(map[string]func(string) bool{ param.SetDefaultBindings(map[string]string{
"done": keys.Enter, keys.Enter: "done",
"remove": keys.Backspace, keys.Backspace: "remove",
"left": keys.Left, keys.Left: "left",
"right": keys.Right, 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 { for i, item := range m.Items {
if i == m.SelectedItem { 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 { } 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") return strings.Join(res, "\n")
} }
func (m *MenuComponent) Update(key string) bool { func (m *MenuComponent) Update(ctx osui.UpdateContext) bool {
if f, ok := m.Data.Keys["up"]; ok && f(key) { if ctx.UpdateKind == osui.UpdateKindKey {
if m.SelectedItem > 0 { switch m.Data.Keys[ctx.Key.Name] {
m.SelectedItem-- case "up":
} else { if m.SelectedItem > 0 {
m.SelectedItem = len(m.Items) - 1 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 return false
} }
func Menu(param osui.Param, items ...string) *MenuComponent { func Menu(param osui.Param, items ...string) *MenuComponent {
param.SetDefaultBindings(map[string]func(string) bool{ param.SetDefaultBindings(map[string]string{
"up": keys.Up, keys.Up: "up",
"down": keys.Down, keys.Down: "down",
"select": keys.Enter, keys.Enter: "select",
}) })
return param.UseParam(&MenuComponent{ return param.UseParam(&MenuComponent{
Items: items, Items: items,
+35 -43
View File
@@ -9,20 +9,8 @@ import (
"github.com/orus-dev/osui/keys" "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 { type PaginatorComponent struct {
Data osui.ComponentData Data osui.ComponentData
Style *PaginatorStyle
Components []osui.Component Components []osui.Component
ActiveComponent int ActiveComponent int
} }
@@ -39,10 +27,10 @@ func (p *PaginatorComponent) Render() string {
for i, c := range p.Components { for i, c := range p.Components {
data := c.GetComponentData() data := c.GetComponentData()
if i == p.ActiveComponent { if i == p.ActiveComponent {
pgs += p.Style.Active + "•" + colors.Reset pgs += p.Data.Style.SelectedForeground + data.Style.SelectedBackground + "•" + colors.Reset
data.IsActive = true data.IsActive = true
} else { } else {
pgs += colors.Reset + p.Style.Inactive + "•" pgs += colors.Reset + p.Data.Style.Foreground + data.Style.Background + "•"
data.IsActive = false data.IsActive = false
} }
if data.Width == 0 { if data.Width == 0 {
@@ -58,33 +46,37 @@ func (p *PaginatorComponent) Render() string {
for i, f := range frame { for i, f := range frame {
frame[i] = colors.Reset + p.Data.DefaultColor + f + colors.Reset 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 { func (p *PaginatorComponent) Update(ctx osui.UpdateContext) bool {
if f, ok := p.Data.Keys["previous"]; ok && f(key) { if ctx.UpdateKind == osui.UpdateKindKey {
if p.ActiveComponent > 0 { switch p.Data.Keys[ctx.Key.Name] {
p.updateActive(p.ActiveComponent - 1) case "previous":
} else { if p.ActiveComponent > 0 {
p.updateActive(len(p.Components) - 1) p.updateActive(p.ActiveComponent - 1)
} } else {
} else if f, ok := p.Data.Keys["next"]; ok && f(key) { p.updateActive(len(p.Components) - 1)
if p.ActiveComponent < len(p.Components)-1 { }
p.updateActive(p.ActiveComponent + 1) case "next":
} else { if p.ActiveComponent < len(p.Components)-1 {
p.updateActive(0) p.updateActive(p.ActiveComponent + 1)
} } else {
} else if f, ok := p.Data.Keys["exit"]; ok && f(key) { p.updateActive(0)
fmt.Print("\n\n") }
return true case "exit":
} else { p.Data.OnClick()
if len(p.Components) > 0 { fmt.Print("\n\n")
p.Components[p.ActiveComponent].GetComponentData().IsActive = p.Data.IsActive return true
if p.Components[p.ActiveComponent].Update(key) { default:
if p.ActiveComponent < len(p.Components)-1 { if len(p.Components) > 0 {
p.updateActive(p.ActiveComponent + 1) p.Components[p.ActiveComponent].GetComponentData().IsActive = p.Data.IsActive
} else { if p.Components[p.ActiveComponent].Update(ctx) {
p.updateActive(0) 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 { func Paginator(param osui.Param, pages ...osui.Component) *PaginatorComponent {
param.SetDefaultBindings(map[string]func(string) bool{ param.SetDefaultBindings(map[string]string{
"previous": keys.ShiftTab, keys.ShiftTab: "previous",
"next": keys.Tab, keys.Tab: "next",
"exit": keys.Escape, keys.Escape: "exit",
}) })
return param.UseParam(&PaginatorComponent{ return param.UseParam(&PaginatorComponent{
Components: pages, Components: pages,
+2 -2
View File
@@ -9,7 +9,7 @@ type TextComponent struct {
Text string Text string
} }
func (t *TextComponent) Update(key string) bool { func (t *TextComponent) Update(ctx osui.UpdateContext) bool {
return false return false
} }
@@ -22,6 +22,6 @@ func (t TextComponent) Render() string {
} }
func Text(param osui.Param, text string) *TextComponent { 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) return param.UseParam(&TextComponent{Text: text}).(*TextComponent)
} }
+4 -3
View File
@@ -9,6 +9,7 @@ import (
"strings" "strings"
"github.com/nathan-fiscaletti/consolesize-go" "github.com/nathan-fiscaletti/consolesize-go"
"github.com/orus-dev/osui/keys"
) )
var re = regexp.MustCompile(`(\x1b\[([0-9;]*)[a-zA-Z])+`) var re = regexp.MustCompile(`(\x1b\[([0-9;]*)[a-zA-Z])+`)
@@ -67,13 +68,13 @@ func RenderOnFrame(c Component, frame *[]string) {
} }
} }
func ReadKey() (string, error) { func ReadKey() Key {
var b [3]byte var b [3]byte
n, err := os.Stdin.Read(b[:]) n, err := os.Stdin.Read(b[:])
if err != nil { if err != nil {
return "", err return Key{Chars: [3]rune{'E', 'r', 'r'}, Name: err.Error()}
} }
return string(b[:n]), nil return Key{Chars: [3]rune{rune(b[0]), rune(b[1]), rune(b[2])}, Name: keys.GetKeyName(string(b[:n]))}
} }
func NewFrame(width, height int) []string { func NewFrame(width, height int) []string {