diff --git a/colors/colors.go b/colors/colors.go index 008dd67..c79b1f5 100644 --- a/colors/colors.go +++ b/colors/colors.go @@ -39,7 +39,6 @@ func AsBg(s string) string { }) } - func AsFg(s string) string { re, _ := regexp.Compile(`\x1b\[4([0-9]+)`) return re.ReplaceAllStringFunc(s, func(match string) string { diff --git a/componentUtils.go b/componentUtils.go new file mode 100644 index 0000000..4360498 --- /dev/null +++ b/componentUtils.go @@ -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 +} diff --git a/example/main.go b/example/main.go index 6827a17..afabf26 100644 --- a/example/main.go +++ b/example/main.go @@ -6,9 +6,41 @@ import ( ) 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() - 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) } diff --git a/keys/key.go b/keys/key.go index 1663804..9ee383c 100644 --- a/keys/key.go +++ b/keys/key.go @@ -1,57 +1,55 @@ package keys -func Enter(s string) bool { - return s == "\r" || s == "\n" +const ( + 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 { - return s == "\t" +func GetKeyName(key string) string { + 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" -} \ No newline at end of file diff --git a/osui.go b/osui.go index 6b048be..88d1c43 100644 --- a/osui.go +++ b/osui.go @@ -4,127 +4,39 @@ import ( "fmt" "os" "strings" + "time" "github.com/orus-dev/osui/colors" "golang.org/x/term" ) -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"` - InactiveCursor string `json:"inactiveCursor"` +type Key struct { + Chars [3]rune + Name string } -func (s *Style) SetDefaults() { - if s.Cursor != "" { - s.Cursor = "> " - } -} -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 UpdateContext struct { + UpdateKind uint8 + Tick uint8 + Key Key } type Component interface { Render() string GetComponentData() *ComponentData - Update(string) bool + Update(UpdateContext) bool } type Screen struct { Component Component + tickRate uint8 + running bool } func NewScreen() *Screen { - return &Screen{} + s := Screen{} + s.tickRate = 1 + return &s } func (s *Screen) Render() { @@ -150,13 +62,22 @@ func (s *Screen) Run(c Component) { panic(err) } defer term.Restore(int(os.Stdin.Fd()), oldState) - for { + s.running = true + for s.running { s.Render() - k, _ := ReadKey() - if s.Component.Update(k) { - ShowCursor() - return + s.Component.Update(UpdateContext{UpdateKind: UpdateKindKey, Key: ReadKey()}) + } + defer term.Restore(int(os.Stdin.Fd()), oldState) +} + +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.DefaultColor = colors.Reset } + +func (s *Screen) Exit() { + s.running = false + ShowCursor() +} + +const ( + UpdateKindKey = uint8(iota) + UpdateKindTick +) diff --git a/ui/button.go b/ui/button.go index 24bcdd4..213d816 100644 --- a/ui/button.go +++ b/ui/button.go @@ -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{ diff --git a/ui/div.go b/ui/div.go index 77c1a49..0861c10 100644 --- a/ui/div.go +++ b/ui/div.go @@ -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, diff --git a/ui/inputBox.go b/ui/inputBox.go index 7d0cd5f..8e5ce23 100644 --- a/ui/inputBox.go +++ b/ui/inputBox.go @@ -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) } diff --git a/ui/menu.go b/ui/menu.go index 30b181b..b975edd 100644 --- a/ui/menu.go +++ b/ui/menu.go @@ -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, diff --git a/ui/paginator.go b/ui/paginator.go index 8d1365c..853a625 100644 --- a/ui/paginator.go +++ b/ui/paginator.go @@ -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, diff --git a/ui/text.go b/ui/text.go index 5e0be55..8dc257c 100644 --- a/ui/text.go +++ b/ui/text.go @@ -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) } diff --git a/utils.go b/utils.go index b637e87..4c1ce5c 100644 --- a/utils.go +++ b/utils.go @@ -9,6 +9,7 @@ import ( "strings" "github.com/nathan-fiscaletti/consolesize-go" + "github.com/orus-dev/osui/keys" ) 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 n, err := os.Stdin.Read(b[:]) 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 {