styling
This commit is contained in:
@@ -4,58 +4,77 @@ import (
|
||||
"strings"
|
||||
|
||||
"github.com/orus-dev/osui"
|
||||
"github.com/orus-dev/osui/colors"
|
||||
)
|
||||
|
||||
type DivStyle struct {
|
||||
Background string `defaults:"" type:"bg"`
|
||||
Foreground string `defaults:"" type:"fg"`
|
||||
}
|
||||
|
||||
type DivComponent struct {
|
||||
Data osui.ComponentData
|
||||
Style *DivStyle
|
||||
Components []osui.Component
|
||||
ActiveComponent int
|
||||
}
|
||||
|
||||
func (p *DivComponent) GetComponentData() *osui.ComponentData {
|
||||
return &p.Data
|
||||
func (d *DivComponent) GetComponentData() *osui.ComponentData {
|
||||
return &d.Data
|
||||
}
|
||||
|
||||
func (p DivComponent) Render() string {
|
||||
frame := osui.NewFrame(p.Data.Width, p.Data.Height)
|
||||
for _, c := range p.Components {
|
||||
func (d *DivComponent) Render() string {
|
||||
osui.UseStyle(d.Style)
|
||||
frame := osui.NewFrame(d.Data.Width, d.Data.Height)
|
||||
for i, c := range d.Components {
|
||||
data := c.GetComponentData()
|
||||
data.Width = p.Data.Width
|
||||
data.Height = p.Data.Height
|
||||
data.Screen = p.Data.Screen
|
||||
if i == d.ActiveComponent {
|
||||
data.IsActive = true
|
||||
} else {
|
||||
data.IsActive = false
|
||||
}
|
||||
if data.Width == 0 {
|
||||
data.Width = d.Data.Width
|
||||
}
|
||||
if data.Height == 0 {
|
||||
data.Height = d.Data.Height
|
||||
}
|
||||
data.DefaultColor = colors.Combine(d.Style.Foreground, d.Style.Background)
|
||||
data.Screen = d.Data.Screen
|
||||
osui.RenderOnFrame(c, &frame)
|
||||
}
|
||||
for i, f := range frame {
|
||||
frame[i] = colors.Combine(d.Style.Foreground, d.Style.Background) + f + d.Data.DefaultColor
|
||||
}
|
||||
return strings.Join(frame, "\n")
|
||||
}
|
||||
|
||||
func (p *DivComponent) Run() {
|
||||
for _, c := range p.Components {
|
||||
d := c.GetComponentData()
|
||||
d.Screen = p.Data.Screen
|
||||
func (d *DivComponent) Run() {
|
||||
for _, c := range d.Components {
|
||||
data := c.GetComponentData()
|
||||
data.Screen = d.Data.Screen
|
||||
}
|
||||
for {
|
||||
key, _ := osui.ReadKey()
|
||||
if p.Update(key) {
|
||||
if d.Update(key) {
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (p *DivComponent) Update(key string) bool {
|
||||
func (d *DivComponent) Update(key string) bool {
|
||||
switch key {
|
||||
|
||||
case "":
|
||||
p.updateActive(p.ActiveComponent)
|
||||
case osui.Key.Up:
|
||||
p.updateActive(p.ActiveComponent - 1)
|
||||
d.updateActive(d.ActiveComponent - 1)
|
||||
case osui.Key.Down:
|
||||
p.updateActive(p.ActiveComponent + 1)
|
||||
d.updateActive(d.ActiveComponent + 1)
|
||||
default:
|
||||
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)
|
||||
if len(d.Components) > 0 {
|
||||
d.Components[d.ActiveComponent].GetComponentData().IsActive = d.Data.IsActive
|
||||
if d.Components[d.ActiveComponent].Update(key) {
|
||||
if d.ActiveComponent < len(d.Components)-1 {
|
||||
d.updateActive(d.ActiveComponent + 1)
|
||||
} else {
|
||||
return true
|
||||
}
|
||||
@@ -65,17 +84,15 @@ func (p *DivComponent) Update(key string) bool {
|
||||
return false
|
||||
}
|
||||
|
||||
func (p *DivComponent) updateActive(newIndex int) {
|
||||
if newIndex >= 0 && newIndex < len(p.Components) && len(p.Components) > 0 {
|
||||
p.Components[p.ActiveComponent].GetComponentData().IsActive = false
|
||||
p.ActiveComponent = newIndex
|
||||
p.Components[p.ActiveComponent].GetComponentData().IsActive = p.Data.IsActive
|
||||
p.Components[p.ActiveComponent].Update("")
|
||||
func (d *DivComponent) updateActive(newIndex int) {
|
||||
if newIndex >= 0 && newIndex < len(d.Components) && len(d.Components) > 0 {
|
||||
d.ActiveComponent = newIndex
|
||||
}
|
||||
}
|
||||
|
||||
func Div(components ...osui.Component) *DivComponent {
|
||||
return &DivComponent{
|
||||
Components: components,
|
||||
Style: osui.SetDefaults(&DivStyle{}).(*DivStyle),
|
||||
}
|
||||
}
|
||||
|
||||
+18
-7
@@ -5,10 +5,19 @@ import (
|
||||
"strings"
|
||||
|
||||
"github.com/orus-dev/osui"
|
||||
"github.com/orus-dev/osui/colors"
|
||||
)
|
||||
|
||||
type InputBoxStyle struct {
|
||||
Background string `defaults:"" type:"bg"`
|
||||
Foreground string `defaults:"" type:"fg"`
|
||||
Outline string `defaults:"" type:"fg"`
|
||||
Cursor string `defaults:"" type:"fg"`
|
||||
}
|
||||
|
||||
type InputBoxComponent struct {
|
||||
Data osui.ComponentData
|
||||
Style *InputBoxStyle
|
||||
max_size uint
|
||||
InputData string
|
||||
}
|
||||
@@ -18,13 +27,16 @@ func (s *InputBoxComponent) GetComponentData() *osui.ComponentData {
|
||||
}
|
||||
|
||||
func (s InputBoxComponent) Render() string {
|
||||
osui.UseStyle(s.Style)
|
||||
if s.max_size > uint(len(s.InputData)) {
|
||||
return fmt.Sprintf(
|
||||
" %s\n|%s%s|\n %s",
|
||||
strings.Repeat("_", int(s.max_size)),
|
||||
s.InputData+osui.LogicValue(s.Data.IsActive, "█", ""),
|
||||
strings.Repeat(" ", int(s.max_size)-len(s.InputData)-osui.LogicValueInt(s.Data.IsActive, 1, 0)),
|
||||
strings.Repeat("‾", int(s.max_size)),
|
||||
" %s\n%s|%s%s|%s\n %s",
|
||||
colors.Reset+s.Style.Outline+strings.Repeat("_", int(s.max_size))+colors.Reset,
|
||||
colors.Reset+s.Style.Outline,
|
||||
colors.Combine(s.Style.Foreground, s.Style.Background)+s.InputData+osui.LogicValue(s.Data.IsActive, s.Style.Cursor+"█"+colors.Combine(s.Style.Foreground, s.Style.Background), ""),
|
||||
strings.Repeat(" ", int(s.max_size)-len(s.InputData)-osui.LogicValueInt(s.Data.IsActive, 1, 0))+colors.Reset+s.Style.Outline,
|
||||
colors.Reset+s.Data.DefaultColor,
|
||||
s.Style.Outline+strings.Repeat("‾", int(s.max_size))+colors.Reset+s.Data.DefaultColor,
|
||||
)
|
||||
}
|
||||
return fmt.Sprintf(
|
||||
@@ -53,7 +65,6 @@ func (s *InputBoxComponent) Read() error {
|
||||
|
||||
func (s *InputBoxComponent) Update(key string) bool {
|
||||
switch key {
|
||||
case "":
|
||||
case osui.Key.Enter:
|
||||
return true
|
||||
case osui.Key.Backspace:
|
||||
@@ -72,5 +83,5 @@ func (s *InputBoxComponent) Update(key string) bool {
|
||||
}
|
||||
|
||||
func InputBox(max_size uint) *InputBoxComponent {
|
||||
return &InputBoxComponent{max_size: max_size}
|
||||
return &InputBoxComponent{max_size: max_size, Style: osui.SetDefaults(&InputBoxStyle{}).(*InputBoxStyle)}
|
||||
}
|
||||
|
||||
+11
-14
@@ -5,7 +5,7 @@ import (
|
||||
"strings"
|
||||
|
||||
"github.com/orus-dev/osui"
|
||||
"github.com/orus-dev/osui/styles"
|
||||
"github.com/orus-dev/osui/colors"
|
||||
)
|
||||
|
||||
type PaginatorStyle struct {
|
||||
@@ -26,27 +26,27 @@ func (p *PaginatorComponent) GetComponentData() *osui.ComponentData {
|
||||
func (p PaginatorComponent) Render() string {
|
||||
pgs := strings.Repeat(" ", p.Data.Width/2)
|
||||
for page, c := range p.Components {
|
||||
data := c.GetComponentData()
|
||||
if page == p.ActiveComponent {
|
||||
pgs += p.Style.Active + "•" + styles.Reset
|
||||
data.IsActive = true
|
||||
pgs += p.Style.Active + "•" + colors.Reset
|
||||
} else {
|
||||
pgs += styles.Reset + "•"
|
||||
pgs += colors.Reset + "•"
|
||||
data.IsActive = false
|
||||
}
|
||||
d := c.GetComponentData()
|
||||
if d.Screen == nil {
|
||||
d.Screen = p.Data.Screen
|
||||
if data.Screen == nil {
|
||||
data.Screen = p.Data.Screen
|
||||
}
|
||||
d.Height = p.Data.Height - 1
|
||||
d.Width = p.Data.Width
|
||||
data.Height = p.Data.Height - 1
|
||||
data.Width = p.Data.Width
|
||||
}
|
||||
frame := osui.NewFrame(p.Data.Width, p.Data.Height-3)
|
||||
osui.RenderOnFrame(p.Components[p.ActiveComponent], &frame)
|
||||
return fmt.Sprintf("%s\n%s", pgs, styles.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 {
|
||||
switch key {
|
||||
case "":
|
||||
p.updateActive(p.ActiveComponent)
|
||||
case "\x1b[Z":
|
||||
if p.ActiveComponent > 0 {
|
||||
p.updateActive(p.ActiveComponent - 1)
|
||||
@@ -79,10 +79,7 @@ func (p *PaginatorComponent) Update(key string) bool {
|
||||
|
||||
func (p *PaginatorComponent) updateActive(newIndex int) {
|
||||
if newIndex >= 0 && newIndex < len(p.Components) && len(p.Components) > 0 {
|
||||
p.Components[p.ActiveComponent].GetComponentData().IsActive = false
|
||||
p.ActiveComponent = newIndex
|
||||
p.Components[p.ActiveComponent].GetComponentData().IsActive = p.Data.IsActive
|
||||
p.Components[p.ActiveComponent].Update("")
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user