paginator and styles

This commit is contained in:
2024-09-24 20:42:44 +02:00
parent 3529f3dcc5
commit 905a77f8b0
8 changed files with 263 additions and 45 deletions
+29 -28
View File
@@ -7,21 +7,9 @@ import (
"github.com/orus-dev/osui"
)
type TextComponent struct {
Data osui.ComponentData
Text string
}
func (t *TextComponent) GetComponentData() *osui.ComponentData {
return &t.Data
}
func (t TextComponent) Render() string {
return t.Text
}
type InputBoxComponent struct {
Data osui.ComponentData
cursor string
max_size uint
InputData string
}
@@ -32,39 +20,52 @@ func (s *InputBoxComponent) GetComponentData() *osui.ComponentData {
func (s InputBoxComponent) Render() string {
return fmt.Sprintf(
" %s\n|%s%s|\n %s",
" %s\n|%s%s|\n %s%s",
strings.Repeat("_", int(s.max_size)),
s.InputData,
strings.Repeat(" ", int(s.max_size)-len(s.InputData)),
strings.Repeat("‾", int(s.max_size)),
s.cursor,
)
}
func (s *InputBoxComponent) Read() error {
for {
fmt.Printf("\x1B[%d;%dH", s.Data.Y+2, int(s.Data.X+2)+len(s.InputData))
key, err := osui.ReadKey()
if err != nil {
fmt.Print("\n\n")
fmt.Println(err)
break
}
switch key {
case osui.Key.Enter:
if s.Update(key) {
fmt.Print("\n\n")
return nil
case osui.Key.Backspace:
if len(s.InputData) > 0 {
s.InputData = s.InputData[:len(s.InputData)-1]
}
default:
if int(s.max_size) > len(s.InputData) {
s.InputData += key
}
}
s.Data.Screen.Render()
}
return nil
}
func (s *InputBoxComponent) Update(key string) bool {
// s.cursor = fmt.Sprintf("\x1B[%d;%dH", s.Data.Y+2, int(s.Data.X+2)+len(s.InputData))
// s.Render()
switch key {
case osui.Key.Enter:
return true
case osui.Key.Backspace:
if len(s.InputData) > 0 {
s.InputData = s.InputData[:len(s.InputData)-1]
}
default:
if int(s.max_size) > len(s.InputData) {
s.InputData += key
}
}
s.Data.Screen.Render()
return false
}
func InputBox(max_size uint) *InputBoxComponent {
return &InputBoxComponent{max_size: max_size}
}
+83
View File
@@ -0,0 +1,83 @@
package ui
import (
"fmt"
"strings"
"github.com/orus-dev/osui"
"github.com/orus-dev/osui/styles"
)
type PaginatorStyle struct {
Active string `default:"\033[32m"`
}
type PaginatorComponent struct {
Data osui.ComponentData
Style *PaginatorStyle
Pages []osui.Component
UpdatePages map[int]func(string) bool
ActivePage int
}
func (p *PaginatorComponent) GetComponentData() *osui.ComponentData {
return &p.Data
}
func (p PaginatorComponent) Render() string {
pgs := strings.Repeat(" ", p.Data.Screen.Width/2)
for page := range p.Pages {
if page == p.ActivePage {
pgs += p.Style.Active + "•" + styles.Reset
} else {
pgs += styles.Reset + "•"
}
}
frame := osui.NewFrame(p.Data.Screen.Width, p.Data.Screen.Height-3)
osui.RenderOnFrame(p.Pages[p.ActivePage], &frame)
return fmt.Sprintf("%s\n%s", pgs, styles.Reset+strings.Join(frame, "\n"))
}
func (p *PaginatorComponent) Run() {
for _, c := range p.Pages {
d := c.GetComponentData()
if d.Screen == nil {
d.Screen = p.Data.Screen
}
}
for {
key, _ := osui.ReadKey()
switch key {
case "\x1b[Z":
if p.ActivePage > 0 {
p.ActivePage--
} else {
p.ActivePage = len(p.Pages) - 1
}
case osui.Key.Tab:
if p.ActivePage < len(p.Pages)-1 {
p.ActivePage++
} else {
p.ActivePage = 0
}
case osui.Key.Escape:
fmt.Print("\n\n")
return
default:
for i, c := range p.UpdatePages {
if i == p.ActivePage {
c(key)
}
}
}
p.Data.Screen.Render()
}
}
func Paginator(pages ...osui.Component) *PaginatorComponent {
return &PaginatorComponent{
Pages: pages,
Style: osui.SetDefaults(&PaginatorStyle{}).(*PaginatorStyle),
UpdatePages: make(map[int]func(string) bool),
}
}
+22
View File
@@ -0,0 +1,22 @@
package ui
import (
"github.com/orus-dev/osui"
)
type TextComponent struct {
Data osui.ComponentData
Text string
}
func (t *TextComponent) GetComponentData() *osui.ComponentData {
return &t.Data
}
func (t TextComponent) Render() string {
return t.Text
}
func Text(text string) *TextComponent {
return &TextComponent{Text: text}
}
-9
View File
@@ -1,9 +0,0 @@
package ui
func Text(text string) *TextComponent {
return &TextComponent{Text: text}
}
func InputBox(max_size uint) *InputBoxComponent {
return &InputBoxComponent{max_size: max_size}
}