divs and paginators
This commit is contained in:
+3
-6
@@ -8,12 +8,9 @@ import (
|
|||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
colors := ui.Text(
|
colors := ui.Text(
|
||||||
styles.Red + "Red!\n" + styles.Green + "Green!\n" + styles.Blue + "Blue!\n",
|
styles.Red + "Red!\n" + styles.Green + "Green!\n" + styles.Blue + "Blue!" + styles.Reset,
|
||||||
)
|
)
|
||||||
input := ui.InputBox(30)
|
paginator := ui.Paginator(ui.Div(colors), ui.Div(ui.InputBox(30)))
|
||||||
paginator := ui.Paginator(colors, input)
|
|
||||||
paginator.UpdatePages[1] = input.Update
|
|
||||||
screen := osui.NewScreen(paginator)
|
screen := osui.NewScreen(paginator)
|
||||||
screen.Render()
|
screen.Run()
|
||||||
paginator.Run()
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -8,47 +8,56 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
type ComponentData struct {
|
type ComponentData struct {
|
||||||
X uint16
|
X uint16
|
||||||
Y uint16
|
Y uint16
|
||||||
Screen *Screen
|
Width int
|
||||||
|
Height int
|
||||||
|
IsActive bool
|
||||||
|
Screen *Screen
|
||||||
}
|
}
|
||||||
|
|
||||||
type Component interface {
|
type Component interface {
|
||||||
Render() string
|
Render() string
|
||||||
GetComponentData() *ComponentData
|
GetComponentData() *ComponentData
|
||||||
|
Update(string) bool
|
||||||
}
|
}
|
||||||
|
|
||||||
type Screen struct {
|
type Screen struct {
|
||||||
components []Component
|
component Component
|
||||||
Width int
|
|
||||||
Height int
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewScreen(c ...Component) *Screen {
|
func NewScreen(c Component) *Screen {
|
||||||
s := &Screen{components: c}
|
HideCursor()
|
||||||
for _, component := range s.components {
|
s := &Screen{component: c}
|
||||||
componentData := component.GetComponentData()
|
|
||||||
if componentData.Screen == nil {
|
|
||||||
componentData.Screen = s
|
|
||||||
} else {
|
|
||||||
componentData.Screen.components = s.components
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return s
|
return s
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Screen) Render() error {
|
func (s *Screen) Render() error {
|
||||||
Clear()
|
Clear()
|
||||||
width, height, err := term.GetSize(0)
|
width, height, err := term.GetSize(0)
|
||||||
s.Width = width
|
|
||||||
s.Height = height
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("error getting the terminal size: %s", err)
|
return fmt.Errorf("error getting the terminal size: %s", err)
|
||||||
}
|
}
|
||||||
frame := NewFrame(width, height)
|
frame := NewFrame(width, height)
|
||||||
for _, component := range s.components {
|
data := s.component.GetComponentData()
|
||||||
RenderOnFrame(component, &frame)
|
data.Width = width
|
||||||
}
|
data.Height = height
|
||||||
|
data.Screen = s
|
||||||
|
data.IsActive = true
|
||||||
|
RenderOnFrame(s.component, &frame)
|
||||||
fmt.Print(strings.Join(frame, "\n"))
|
fmt.Print(strings.Join(frame, "\n"))
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (s *Screen) Run() {
|
||||||
|
s.component.GetComponentData().Screen = s
|
||||||
|
s.component.Update("OSUI:RENDER")
|
||||||
|
for {
|
||||||
|
s.Render()
|
||||||
|
k, _ := ReadKey()
|
||||||
|
if s.component.Update(k) {
|
||||||
|
ShowCursor()
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -0,0 +1,81 @@
|
|||||||
|
package ui
|
||||||
|
|
||||||
|
import (
|
||||||
|
"strings"
|
||||||
|
|
||||||
|
"github.com/orus-dev/osui"
|
||||||
|
)
|
||||||
|
|
||||||
|
type DivComponent struct {
|
||||||
|
Data osui.ComponentData
|
||||||
|
Components []osui.Component
|
||||||
|
ActiveComponent int
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p *DivComponent) GetComponentData() *osui.ComponentData {
|
||||||
|
return &p.Data
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p DivComponent) Render() string {
|
||||||
|
frame := osui.NewFrame(p.Data.Width, p.Data.Height)
|
||||||
|
for _, c := range p.Components {
|
||||||
|
data := c.GetComponentData()
|
||||||
|
data.Width = p.Data.Width
|
||||||
|
data.Height = p.Data.Height
|
||||||
|
data.Screen = p.Data.Screen
|
||||||
|
osui.RenderOnFrame(c, &frame)
|
||||||
|
}
|
||||||
|
return strings.Join(frame, "\n")
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p *DivComponent) Run() {
|
||||||
|
for _, c := range p.Components {
|
||||||
|
d := c.GetComponentData()
|
||||||
|
d.Screen = p.Data.Screen
|
||||||
|
}
|
||||||
|
for {
|
||||||
|
key, _ := osui.ReadKey()
|
||||||
|
if p.Update(key) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p *DivComponent) Update(key string) bool {
|
||||||
|
switch key {
|
||||||
|
|
||||||
|
case "":
|
||||||
|
p.updateActive(p.ActiveComponent)
|
||||||
|
case osui.Key.Up:
|
||||||
|
p.updateActive(p.ActiveComponent - 1)
|
||||||
|
case osui.Key.Down:
|
||||||
|
p.updateActive(p.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)
|
||||||
|
} else {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
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 Div(components ...osui.Component) *DivComponent {
|
||||||
|
return &DivComponent{
|
||||||
|
Components: components,
|
||||||
|
}
|
||||||
|
}
|
||||||
+16
-11
@@ -9,7 +9,6 @@ import (
|
|||||||
|
|
||||||
type InputBoxComponent struct {
|
type InputBoxComponent struct {
|
||||||
Data osui.ComponentData
|
Data osui.ComponentData
|
||||||
cursor string
|
|
||||||
max_size uint
|
max_size uint
|
||||||
InputData string
|
InputData string
|
||||||
}
|
}
|
||||||
@@ -19,13 +18,20 @@ func (s *InputBoxComponent) GetComponentData() *osui.ComponentData {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (s InputBoxComponent) Render() string {
|
func (s InputBoxComponent) Render() string {
|
||||||
|
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)),
|
||||||
|
)
|
||||||
|
}
|
||||||
return fmt.Sprintf(
|
return fmt.Sprintf(
|
||||||
" %s\n|%s%s|\n %s%s",
|
" %s\n|%s\n %s",
|
||||||
strings.Repeat("_", int(s.max_size)),
|
strings.Repeat("_", int(s.max_size)),
|
||||||
s.InputData,
|
s.InputData+osui.LogicValue(s.Data.IsActive, "█", "|"),
|
||||||
strings.Repeat(" ", int(s.max_size)-len(s.InputData)),
|
|
||||||
strings.Repeat("‾", int(s.max_size)),
|
strings.Repeat("‾", int(s.max_size)),
|
||||||
s.cursor,
|
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -46,23 +52,22 @@ func (s *InputBoxComponent) Read() error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (s *InputBoxComponent) Update(key string) bool {
|
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 {
|
switch key {
|
||||||
|
case "":
|
||||||
case osui.Key.Enter:
|
case osui.Key.Enter:
|
||||||
return true
|
return true
|
||||||
|
|
||||||
case osui.Key.Backspace:
|
case osui.Key.Backspace:
|
||||||
if len(s.InputData) > 0 {
|
if len(s.InputData) > 0 {
|
||||||
s.InputData = s.InputData[:len(s.InputData)-1]
|
s.InputData = s.InputData[:len(s.InputData)-1]
|
||||||
}
|
}
|
||||||
|
|
||||||
default:
|
default:
|
||||||
if int(s.max_size) > len(s.InputData) {
|
if len(key) == 1 {
|
||||||
s.InputData += key
|
if int(s.max_size) > len(s.InputData) {
|
||||||
|
s.InputData += key
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
s.Data.Screen.Render()
|
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+53
-42
@@ -13,11 +13,10 @@ type PaginatorStyle struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type PaginatorComponent struct {
|
type PaginatorComponent struct {
|
||||||
Data osui.ComponentData
|
Data osui.ComponentData
|
||||||
Style *PaginatorStyle
|
Style *PaginatorStyle
|
||||||
Pages []osui.Component
|
Components []osui.Component
|
||||||
UpdatePages map[int]func(string) bool
|
ActiveComponent int
|
||||||
ActivePage int
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *PaginatorComponent) GetComponentData() *osui.ComponentData {
|
func (p *PaginatorComponent) GetComponentData() *osui.ComponentData {
|
||||||
@@ -25,59 +24,71 @@ func (p *PaginatorComponent) GetComponentData() *osui.ComponentData {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (p PaginatorComponent) Render() string {
|
func (p PaginatorComponent) Render() string {
|
||||||
pgs := strings.Repeat(" ", p.Data.Screen.Width/2)
|
pgs := strings.Repeat(" ", p.Data.Width/2)
|
||||||
for page := range p.Pages {
|
for page, c := range p.Components {
|
||||||
if page == p.ActivePage {
|
if page == p.ActiveComponent {
|
||||||
pgs += p.Style.Active + "•" + styles.Reset
|
pgs += p.Style.Active + "•" + styles.Reset
|
||||||
} else {
|
} else {
|
||||||
pgs += styles.Reset + "•"
|
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()
|
d := c.GetComponentData()
|
||||||
if d.Screen == nil {
|
if d.Screen == nil {
|
||||||
d.Screen = p.Data.Screen
|
d.Screen = p.Data.Screen
|
||||||
}
|
}
|
||||||
|
d.Height = p.Data.Height - 1
|
||||||
|
d.Width = p.Data.Width
|
||||||
}
|
}
|
||||||
for {
|
frame := osui.NewFrame(p.Data.Width, p.Data.Height-3)
|
||||||
key, _ := osui.ReadKey()
|
osui.RenderOnFrame(p.Components[p.ActiveComponent], &frame)
|
||||||
switch key {
|
return fmt.Sprintf("%s\n%s", pgs, styles.Reset+strings.Join(frame, "\n"))
|
||||||
case "\x1b[Z":
|
}
|
||||||
if p.ActivePage > 0 {
|
|
||||||
p.ActivePage--
|
func (p *PaginatorComponent) Update(key string) bool {
|
||||||
} else {
|
switch key {
|
||||||
p.ActivePage = len(p.Pages) - 1
|
case "":
|
||||||
}
|
p.updateActive(p.ActiveComponent)
|
||||||
case osui.Key.Tab:
|
case "\x1b[Z":
|
||||||
if p.ActivePage < len(p.Pages)-1 {
|
if p.ActiveComponent > 0 {
|
||||||
p.ActivePage++
|
p.updateActive(p.ActiveComponent - 1)
|
||||||
} else {
|
} else {
|
||||||
p.ActivePage = 0
|
p.updateActive(len(p.Components) - 1)
|
||||||
}
|
}
|
||||||
case osui.Key.Escape:
|
case osui.Key.Tab:
|
||||||
fmt.Print("\n\n")
|
if p.ActiveComponent < len(p.Components)-1 {
|
||||||
return
|
p.updateActive(p.ActiveComponent + 1)
|
||||||
default:
|
} else {
|
||||||
for i, c := range p.UpdatePages {
|
p.updateActive(0)
|
||||||
if i == p.ActivePage {
|
}
|
||||||
c(key)
|
case osui.Key.Escape:
|
||||||
|
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(key) {
|
||||||
|
if p.ActiveComponent < len(p.Components)-1 {
|
||||||
|
p.updateActive(p.ActiveComponent + 1)
|
||||||
|
} else {
|
||||||
|
p.updateActive(0)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
p.Data.Screen.Render()
|
}
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
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("")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func Paginator(pages ...osui.Component) *PaginatorComponent {
|
func Paginator(pages ...osui.Component) *PaginatorComponent {
|
||||||
return &PaginatorComponent{
|
return &PaginatorComponent{
|
||||||
Pages: pages,
|
Components: pages,
|
||||||
Style: osui.SetDefaults(&PaginatorStyle{}).(*PaginatorStyle),
|
Style: osui.SetDefaults(&PaginatorStyle{}).(*PaginatorStyle),
|
||||||
UpdatePages: make(map[int]func(string) bool),
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -9,6 +9,10 @@ type TextComponent struct {
|
|||||||
Text string
|
Text string
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (t *TextComponent) Update(string) bool {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
func (t *TextComponent) GetComponentData() *osui.ComponentData {
|
func (t *TextComponent) GetComponentData() *osui.ComponentData {
|
||||||
return &t.Data
|
return &t.Data
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
package osui
|
package osui
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
"os/exec"
|
"os/exec"
|
||||||
"reflect"
|
"reflect"
|
||||||
@@ -117,6 +118,28 @@ func SetDefaults(p interface{}) interface{} {
|
|||||||
return p
|
return p
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func ShowCursor() {
|
||||||
|
fmt.Print("\033[?25h")
|
||||||
|
}
|
||||||
|
|
||||||
|
func HideCursor() {
|
||||||
|
fmt.Print("\033[?25l")
|
||||||
|
}
|
||||||
|
|
||||||
|
func LogicValue(b bool, _if, _else string) string {
|
||||||
|
if b {
|
||||||
|
return _if
|
||||||
|
}
|
||||||
|
return _else
|
||||||
|
}
|
||||||
|
|
||||||
|
func LogicValueInt(b bool, _if, _else int) int {
|
||||||
|
if b {
|
||||||
|
return _if
|
||||||
|
}
|
||||||
|
return _else
|
||||||
|
}
|
||||||
|
|
||||||
type Key_ = string
|
type Key_ = string
|
||||||
|
|
||||||
type key struct {
|
type key struct {
|
||||||
|
|||||||
Reference in New Issue
Block a user