updated structure
This commit is contained in:
+4
-53
@@ -2,61 +2,12 @@ package main
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/orus-dev/osui"
|
"github.com/orus-dev/osui"
|
||||||
"github.com/orus-dev/osui/colors"
|
|
||||||
"github.com/orus-dev/osui/ui"
|
"github.com/orus-dev/osui/ui"
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
screen := osui.NewScreen(App())
|
screen := osui.NewScreen()
|
||||||
screen.Run()
|
my_btn := ui.Menu(osui.Param{Style: osui.Style{Cursor: "> "}, OnClick: func() {
|
||||||
}
|
}}, "item 1", "item 2", "item 3", "item 4")
|
||||||
|
screen.Run(my_btn)
|
||||||
func App() *ui.PaginatorComponent {
|
|
||||||
w, h := osui.GetTerminalSize()
|
|
||||||
|
|
||||||
var keybindings ui.Class[*ui.TextComponent]
|
|
||||||
keybindings.SetProperties(func(tc *ui.TextComponent) *ui.TextComponent {
|
|
||||||
tc.Text = colors.Blue + tc.Text
|
|
||||||
tc.Data.Y = h - 3
|
|
||||||
return tc
|
|
||||||
})
|
|
||||||
|
|
||||||
return ui.Paginator(
|
|
||||||
// Page 1
|
|
||||||
ui.Div(
|
|
||||||
ui.Text("Welcome to the example! The blue text on the bottom show the keybindings"),
|
|
||||||
keybindings.Class(ui.Text("Next Page: Tab. Previous Page: shift + tab")),
|
|
||||||
),
|
|
||||||
|
|
||||||
// Page 2
|
|
||||||
ui.Div(
|
|
||||||
ui.WithPosition(0, 0, ui.Text("This blue square is a div. A div stores multiple components into one")),
|
|
||||||
ui.WithPosition((w-30)/2, (h-4)/2,
|
|
||||||
ui.WithSize(32, 7,
|
|
||||||
ui.Div(ui.WithPosition(2, 2, ui.Text("This text is inside a div!"))).Params(ui.DivParams{Style: ui.DivStyle{Outline: colors.Blue, Foreground: colors.Red}}),
|
|
||||||
)),
|
|
||||||
keybindings.Class(ui.Text("Navigate: Ctrl+(W/A/S/D)")),
|
|
||||||
),
|
|
||||||
|
|
||||||
// Page 3
|
|
||||||
ui.Div(
|
|
||||||
ui.WithPosition(0, 0, ui.Text("This is a InputBox. It takes user input just like a GUI. "+colors.Red+colors.Bold+"NOTE: This InputBox is inside a div")),
|
|
||||||
ui.WithPosition((w-32)/2, (h-3)/2, ui.InputBox(30)),
|
|
||||||
keybindings.Class(ui.Text("Navigate: Ctrl+(W/A/S/D). Finish: Enter.")),
|
|
||||||
),
|
|
||||||
|
|
||||||
// Page 4
|
|
||||||
ui.Div(
|
|
||||||
ui.WithPosition(0, 0, ui.Text("This is a Button. To click it press Enter. "+colors.Red+colors.Bold+"NOTE: This Button is inside a div")),
|
|
||||||
ui.WithPosition((w-20)/2, (h-3)/2, ui.Button("This is a button")),
|
|
||||||
keybindings.Class(ui.Text("Navigate: Ctrl+(W/A/S/D). Click Button: Enter")),
|
|
||||||
),
|
|
||||||
|
|
||||||
// Page 5
|
|
||||||
ui.Div(
|
|
||||||
ui.WithPosition(0, 0, ui.Text("This is a Menu. It prompts you to select an item. "+colors.Red+colors.Bold+"NOTE: This Menu is inside a div")),
|
|
||||||
ui.WithPosition(0, 2, ui.Menu("Item 1", "Item 2", "Item 3", "Item 4", "Item 5").Params(ui.MenuParams{OnSelected: func(mc *ui.MenuComponent, b bool) {}})),
|
|
||||||
keybindings.Class(ui.Text("Navigate: Ctrl+(W/A/S/D). Navigate Menu: W/S. Select: Enter. Exit Menu: Q. ")),
|
|
||||||
),
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -9,6 +9,93 @@ import (
|
|||||||
"golang.org/x/term"
|
"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"`
|
||||||
|
}
|
||||||
|
|
||||||
|
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()
|
||||||
|
OnUpdate func(ctx *Context)
|
||||||
|
Toggle bool `json:"useToggle"`
|
||||||
|
}
|
||||||
|
|
||||||
|
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() {}
|
||||||
|
}
|
||||||
|
|
||||||
|
if p.OnUpdate != nil {
|
||||||
|
data.OnUpdate = p.OnUpdate
|
||||||
|
} else {
|
||||||
|
data.OnUpdate = func(ctx *Context) { ctx.Response = 0 }
|
||||||
|
}
|
||||||
|
|
||||||
|
data.Toggle = p.Toggle
|
||||||
|
|
||||||
|
return c
|
||||||
|
}
|
||||||
|
|
||||||
|
type Context struct {
|
||||||
|
Key string
|
||||||
|
Response int
|
||||||
|
}
|
||||||
|
|
||||||
type ComponentData struct {
|
type ComponentData struct {
|
||||||
X int
|
X int
|
||||||
Y int
|
Y int
|
||||||
@@ -16,6 +103,10 @@ type ComponentData struct {
|
|||||||
Height int
|
Height int
|
||||||
DefaultColor string
|
DefaultColor string
|
||||||
IsActive bool
|
IsActive bool
|
||||||
|
Style Style
|
||||||
|
OnClick func()
|
||||||
|
OnUpdate func(ctx *Context)
|
||||||
|
Toggle bool
|
||||||
Screen *Screen
|
Screen *Screen
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -26,19 +117,50 @@ type Component interface {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type Screen struct {
|
type Screen struct {
|
||||||
component Component
|
Component Component
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewScreen(c Component) *Screen {
|
func NewScreen() *Screen {
|
||||||
HideCursor()
|
return &Screen{}
|
||||||
s := &Screen{component: c}
|
|
||||||
return s
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Screen) Render() {
|
func (s *Screen) Render() {
|
||||||
width, height := GetTerminalSize()
|
width, height := GetTerminalSize()
|
||||||
|
data := s.Component.GetComponentData()
|
||||||
|
if data.Height == 0 {
|
||||||
|
data.Height = height
|
||||||
|
}
|
||||||
|
if data.Width == 0 {
|
||||||
|
data.Width = width
|
||||||
|
}
|
||||||
frame := NewFrame(width, height)
|
frame := NewFrame(width, height)
|
||||||
data := s.component.GetComponentData()
|
RenderOnFrame(s.Component, &frame)
|
||||||
|
Clear()
|
||||||
|
fmt.Print(strings.Join(frame, ""))
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *Screen) Run(c Component) {
|
||||||
|
s.SetComponent(c)
|
||||||
|
HideCursor()
|
||||||
|
oldState, err := term.MakeRaw(int(os.Stdin.Fd()))
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
defer term.Restore(int(os.Stdin.Fd()), oldState)
|
||||||
|
for {
|
||||||
|
s.Render()
|
||||||
|
k, _ := ReadKey()
|
||||||
|
if s.Component.Update(k) {
|
||||||
|
ShowCursor()
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *Screen) SetComponent(c Component) {
|
||||||
|
s.Component = c
|
||||||
|
width, height := GetTerminalSize()
|
||||||
|
data := s.Component.GetComponentData()
|
||||||
if data.Height == 0 {
|
if data.Height == 0 {
|
||||||
data.Height = height
|
data.Height = height
|
||||||
}
|
}
|
||||||
@@ -48,25 +170,4 @@ func (s *Screen) Render() {
|
|||||||
data.Screen = s
|
data.Screen = s
|
||||||
data.IsActive = true
|
data.IsActive = true
|
||||||
data.DefaultColor = colors.Reset
|
data.DefaultColor = colors.Reset
|
||||||
RenderOnFrame(s.component, &frame)
|
|
||||||
Clear()
|
|
||||||
fmt.Print(strings.Join(frame, ""))
|
|
||||||
}
|
|
||||||
|
|
||||||
func (s *Screen) Run() {
|
|
||||||
oldState, err := term.MakeRaw(int(os.Stdin.Fd()))
|
|
||||||
if err != nil {
|
|
||||||
panic(err)
|
|
||||||
}
|
|
||||||
defer term.Restore(int(os.Stdin.Fd()), oldState)
|
|
||||||
data := s.component.GetComponentData()
|
|
||||||
data.Screen = s
|
|
||||||
for {
|
|
||||||
s.Render()
|
|
||||||
k, _ := ReadKey()
|
|
||||||
if s.component.Update(k) {
|
|
||||||
ShowCursor()
|
|
||||||
return
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
+24
-59
@@ -10,80 +10,55 @@ import (
|
|||||||
"github.com/orus-dev/osui/isKey"
|
"github.com/orus-dev/osui/isKey"
|
||||||
)
|
)
|
||||||
|
|
||||||
type ButtonParams struct {
|
|
||||||
Style ButtonStyle
|
|
||||||
Toggle bool
|
|
||||||
OnClick func(*ButtonComponent) bool
|
|
||||||
Width int
|
|
||||||
}
|
|
||||||
|
|
||||||
type ButtonStyle struct {
|
|
||||||
Background string `default:"" type:"bg"`
|
|
||||||
Foreground string `default:"" type:"fg"`
|
|
||||||
Outline string `default:"" type:"fg"`
|
|
||||||
|
|
||||||
ActiveBackground string `default:"" type:"bg"`
|
|
||||||
ActiveForeground string `default:"\x1b[34m" type:"fg"`
|
|
||||||
ActiveOutline string `default:"" type:"fg"`
|
|
||||||
|
|
||||||
ClickedBackground string `default:"" type:"bg"`
|
|
||||||
ClickedForeground string `default:"\x1b[32m" type:"fg"`
|
|
||||||
ClickedOutline string `default:"" type:"fg"`
|
|
||||||
}
|
|
||||||
|
|
||||||
type ButtonComponent struct {
|
type ButtonComponent struct {
|
||||||
Data osui.ComponentData
|
Data osui.ComponentData
|
||||||
Style *ButtonStyle
|
|
||||||
Text string
|
Text string
|
||||||
Toggle bool
|
clicked bool
|
||||||
Clicked bool
|
|
||||||
on_click func(*ButtonComponent) bool
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (b *ButtonComponent) Render() string {
|
func (b *ButtonComponent) Render() string {
|
||||||
osui.UseStyle(b.Style)
|
b.Data.Style.UseStyle()
|
||||||
|
|
||||||
if b.Clicked {
|
if b.clicked {
|
||||||
return fmt.Sprintf(" %s\n%s│%s│%s\n %s",
|
return fmt.Sprintf(" %s\n%s│%s│%s\n %s",
|
||||||
colors.Reset+b.Style.ClickedOutline+strings.Repeat("_", b.Data.Width-2)+colors.Reset+b.Data.DefaultColor,
|
colors.Reset+b.Data.Style.ClickedOutline+strings.Repeat("_", b.Data.Width-2)+colors.Reset+b.Data.DefaultColor,
|
||||||
colors.Reset+b.Style.ClickedOutline,
|
colors.Reset+b.Data.Style.ClickedOutline,
|
||||||
colors.Reset+colors.Combine(b.Style.ClickedBackground, b.Style.ClickedForeground)+centerText(b.Text, b.Data.Width-2)+colors.Reset+b.Style.ClickedOutline,
|
colors.Reset+colors.Combine(b.Data.Style.ClickedBackground, b.Data.Style.ClickedForeground)+centerText(b.Text, b.Data.Width-2)+colors.Reset+b.Data.Style.ClickedOutline,
|
||||||
colors.Reset+b.Data.DefaultColor,
|
colors.Reset+b.Data.DefaultColor,
|
||||||
colors.Reset+b.Style.ClickedOutline+strings.Repeat("‾", b.Data.Width-2)+colors.Reset+b.Data.DefaultColor,
|
colors.Reset+b.Data.Style.ClickedOutline+strings.Repeat("‾", b.Data.Width-2)+colors.Reset+b.Data.DefaultColor,
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
if b.Data.IsActive {
|
if b.Data.IsActive {
|
||||||
return fmt.Sprintf(" %s\n%s│%s│%s\n %s",
|
return fmt.Sprintf(" %s\n%s│%s│%s\n %s",
|
||||||
colors.Reset+b.Style.ActiveOutline+strings.Repeat("_", b.Data.Width-2)+colors.Reset+b.Data.DefaultColor,
|
colors.Reset+b.Data.Style.ActiveOutline+strings.Repeat("_", b.Data.Width-2)+colors.Reset+b.Data.DefaultColor,
|
||||||
colors.Reset+b.Style.ActiveOutline,
|
colors.Reset+b.Data.Style.ActiveOutline,
|
||||||
colors.Reset+colors.Combine(b.Style.ActiveBackground, b.Style.ActiveForeground)+centerText(b.Text, b.Data.Width-2)+colors.Reset+b.Style.ActiveOutline,
|
colors.Reset+colors.Combine(b.Data.Style.ActiveBackground, b.Data.Style.ActiveForeground)+centerText(b.Text, b.Data.Width-2)+colors.Reset+b.Data.Style.ActiveOutline,
|
||||||
colors.Reset+b.Data.DefaultColor,
|
colors.Reset+b.Data.DefaultColor,
|
||||||
colors.Reset+b.Style.ActiveOutline+strings.Repeat("‾", b.Data.Width-2)+colors.Reset+b.Data.DefaultColor,
|
colors.Reset+b.Data.Style.ActiveOutline+strings.Repeat("‾", b.Data.Width-2)+colors.Reset+b.Data.DefaultColor,
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
return fmt.Sprintf(" %s\n%s│%s│%s\n %s",
|
return fmt.Sprintf(" %s\n%s│%s│%s\n %s",
|
||||||
colors.Reset+b.Style.Outline+strings.Repeat("_", b.Data.Width-2)+colors.Reset+b.Data.DefaultColor,
|
colors.Reset+b.Data.Style.Outline+strings.Repeat("_", b.Data.Width-2)+colors.Reset+b.Data.DefaultColor,
|
||||||
colors.Reset+b.Style.Outline,
|
colors.Reset+b.Data.Style.Outline,
|
||||||
colors.Reset+colors.Combine(b.Style.Background, b.Style.Foreground)+centerText(b.Text, b.Data.Width-2)+colors.Reset+b.Style.Outline,
|
colors.Reset+colors.Combine(b.Data.Style.Background, b.Data.Style.Foreground)+centerText(b.Text, b.Data.Width-2)+colors.Reset+b.Data.Style.Outline,
|
||||||
colors.Reset+b.Data.DefaultColor,
|
colors.Reset+b.Data.DefaultColor,
|
||||||
colors.Reset+b.Style.Outline+strings.Repeat("‾", b.Data.Width-2)+colors.Reset+b.Data.DefaultColor,
|
colors.Reset+b.Data.Style.Outline+strings.Repeat("‾", b.Data.Width-2)+colors.Reset+b.Data.DefaultColor,
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (b *ButtonComponent) Update(key string) bool {
|
func (b *ButtonComponent) Update(key string) bool {
|
||||||
if isKey.Enter(key) {
|
if isKey.Enter(key) {
|
||||||
if !b.on_click(b) {
|
b.Data.OnClick()
|
||||||
if b.Toggle {
|
if b.Data.Toggle {
|
||||||
b.Clicked = !b.Clicked
|
b.clicked = !b.clicked
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
b.Clicked = true
|
b.clicked = true
|
||||||
b.Data.Screen.Render()
|
b.Data.Screen.Render()
|
||||||
time.Sleep(time.Millisecond * 120)
|
time.Sleep(time.Millisecond * 120)
|
||||||
b.Clicked = false
|
b.clicked = false
|
||||||
}
|
|
||||||
}
|
}
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
@@ -92,22 +67,12 @@ func (b *ButtonComponent) GetComponentData() *osui.ComponentData {
|
|||||||
return &b.Data
|
return &b.Data
|
||||||
}
|
}
|
||||||
|
|
||||||
func (b *ButtonComponent) Params(param ButtonParams) *ButtonComponent {
|
func Button(param osui.Param, text string) *ButtonComponent {
|
||||||
b.Style = osui.SetDefaults(¶m.Style).(*ButtonStyle)
|
return param.UseParam(&ButtonComponent{Text: text,
|
||||||
if param.OnClick != nil {
|
|
||||||
b.on_click = param.OnClick
|
|
||||||
}
|
|
||||||
b.Data.Width = osui.LogicValueInt(param.Width == 0, 20, param.Width)
|
|
||||||
return b
|
|
||||||
}
|
|
||||||
|
|
||||||
func Button(text string) *ButtonComponent {
|
|
||||||
return &ButtonComponent{Text: text,
|
|
||||||
Style: osui.SetDefaults(&ButtonStyle{}).(*ButtonStyle),
|
|
||||||
Data: osui.ComponentData{
|
Data: osui.ComponentData{
|
||||||
Width: 20,
|
Width: 20,
|
||||||
Height: 1,
|
Height: 1,
|
||||||
},
|
},
|
||||||
on_click: func(bc *ButtonComponent) bool { return false },
|
clicked: false,
|
||||||
}
|
}).(*ButtonComponent)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -32,7 +32,7 @@ func (d *DivComponent) GetComponentData() *osui.ComponentData {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (d *DivComponent) Render() string {
|
func (d *DivComponent) Render() string {
|
||||||
osui.UseStyle(d.Style)
|
d.Data.Style.UseStyle()
|
||||||
frame := osui.NewFrame(d.Data.Width-2, d.Data.Height-2)
|
frame := osui.NewFrame(d.Data.Width-2, d.Data.Height-2)
|
||||||
for i, c := range d.Components {
|
for i, c := range d.Components {
|
||||||
data := c.GetComponentData()
|
data := c.GetComponentData()
|
||||||
@@ -88,21 +88,14 @@ func (d *DivComponent) Update(key string) bool {
|
|||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
func (b *DivComponent) Params(param DivParams) *DivComponent {
|
|
||||||
b.Style = osui.SetDefaults(¶m.Style).(*DivStyle)
|
|
||||||
b.Data.Width = osui.LogicValueInt(param.Width == 0, 20, param.Width)
|
|
||||||
return b
|
|
||||||
}
|
|
||||||
|
|
||||||
func (d *DivComponent) updateActive(newIndex int) {
|
func (d *DivComponent) updateActive(newIndex int) {
|
||||||
if newIndex >= 0 && newIndex < len(d.Components) && len(d.Components) > 0 {
|
if newIndex >= 0 && newIndex < len(d.Components) && len(d.Components) > 0 {
|
||||||
d.ActiveComponent = newIndex
|
d.ActiveComponent = newIndex
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func Div(components ...osui.Component) *DivComponent {
|
func Div(param osui.Param, components ...osui.Component) *DivComponent {
|
||||||
return &DivComponent{
|
return param.UseParam(&DivComponent{
|
||||||
Components: components,
|
Components: components,
|
||||||
Style: osui.SetDefaults(&DivStyle{}).(*DivStyle),
|
}).(*DivComponent)
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
+12
-27
@@ -15,16 +15,8 @@ type InputBoxParams struct {
|
|||||||
Height int
|
Height int
|
||||||
}
|
}
|
||||||
|
|
||||||
type InputBoxStyle struct {
|
|
||||||
Background string `default:"" type:"bg"`
|
|
||||||
Foreground string `default:"" type:"fg"`
|
|
||||||
Outline string `default:"" type:"fg"`
|
|
||||||
Cursor string `default:"" type:"fg"`
|
|
||||||
}
|
|
||||||
|
|
||||||
type InputBoxComponent struct {
|
type InputBoxComponent struct {
|
||||||
Data osui.ComponentData
|
Data osui.ComponentData
|
||||||
Style *InputBoxStyle
|
|
||||||
max_size uint
|
max_size uint
|
||||||
cursor uint
|
cursor uint
|
||||||
InputData string
|
InputData string
|
||||||
@@ -35,26 +27,26 @@ func (s *InputBoxComponent) GetComponentData() *osui.ComponentData {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (s InputBoxComponent) Render() string {
|
func (s InputBoxComponent) Render() string {
|
||||||
osui.UseStyle(s.Style)
|
s.Data.Style.UseStyle()
|
||||||
if s.max_size > uint(len(s.InputData)) {
|
if s.max_size > uint(len(s.InputData)) {
|
||||||
return fmt.Sprintf(
|
return fmt.Sprintf(
|
||||||
" %s\n%s│%s%s│%s\n %s",
|
" %s\n%s│%s%s│%s\n %s",
|
||||||
colors.Reset+s.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.Style.Outline,
|
colors.Reset+s.Data.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), ""),
|
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), ""),
|
||||||
strings.Repeat(" ", int(s.max_size)-len(s.InputData)-osui.LogicValueInt(s.Data.IsActive, 1, 0))+colors.Reset+s.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.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,
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
return fmt.Sprintf(
|
return fmt.Sprintf(
|
||||||
" %s\n%s│%s%s\n %s",
|
" %s\n%s│%s%s\n %s",
|
||||||
colors.Reset+s.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.Style.Outline,
|
colors.Reset+s.Data.Style.Outline,
|
||||||
colors.Combine(s.Style.Foreground, s.Style.Background)+s.InputData+osui.LogicValue(s.Data.IsActive, s.Style.Cursor+"█"+colors.Reset, s.Style.Outline+"|"+colors.Reset),
|
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.Reset+s.Data.DefaultColor,
|
colors.Reset+s.Data.DefaultColor,
|
||||||
colors.Reset+s.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,
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -83,13 +75,6 @@ func (s *InputBoxComponent) Update(key string) bool {
|
|||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
func (b *InputBoxComponent) Params(p interface{}) *InputBoxComponent {
|
func InputBox(param osui.Param, max_size uint) *InputBoxComponent {
|
||||||
param := p.(InputBoxParams)
|
return param.UseParam(&InputBoxComponent{max_size: max_size}).(*InputBoxComponent)
|
||||||
b.Style = osui.SetDefaults(¶m.Style).(*InputBoxStyle)
|
|
||||||
b.Data.Width = osui.LogicValueInt(param.Width == 0, 20, param.Width)
|
|
||||||
return b
|
|
||||||
}
|
|
||||||
|
|
||||||
func InputBox(max_size uint) *InputBoxComponent {
|
|
||||||
return &InputBoxComponent{max_size: max_size, Style: osui.SetDefaults(&InputBoxStyle{}).(*InputBoxStyle)}
|
|
||||||
}
|
}
|
||||||
|
|||||||
+18
-38
@@ -8,26 +8,10 @@ import (
|
|||||||
"github.com/orus-dev/osui/isKey"
|
"github.com/orus-dev/osui/isKey"
|
||||||
)
|
)
|
||||||
|
|
||||||
type MenuParams struct {
|
|
||||||
Style MenuStyle
|
|
||||||
OnSelected func(*MenuComponent, bool)
|
|
||||||
}
|
|
||||||
|
|
||||||
type MenuStyle struct {
|
|
||||||
Fg string `default:"" type:"fg"`
|
|
||||||
Bg string `default:"" type:"bg"`
|
|
||||||
SelectedFg string `default:"\x1b[34m" type:"fg"`
|
|
||||||
SelectedBg string `default:"" type:"bg"`
|
|
||||||
Cursor string `default:"> " type:"fg"`
|
|
||||||
CursorInactive string `default:" " type:"fg"`
|
|
||||||
}
|
|
||||||
|
|
||||||
type MenuComponent struct {
|
type MenuComponent struct {
|
||||||
Data osui.ComponentData
|
Data osui.ComponentData
|
||||||
Style *MenuStyle
|
|
||||||
Items []string
|
Items []string
|
||||||
SelectedItem int
|
SelectedItem int
|
||||||
OnSelected func(*MenuComponent, bool)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *MenuComponent) GetComponentData() *osui.ComponentData {
|
func (m *MenuComponent) GetComponentData() *osui.ComponentData {
|
||||||
@@ -35,21 +19,21 @@ func (m *MenuComponent) GetComponentData() *osui.ComponentData {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (m *MenuComponent) Render() string {
|
func (m *MenuComponent) Render() string {
|
||||||
osui.UseStyle(m.Style)
|
m.Data.Style.UseStyle()
|
||||||
res := []string{}
|
res := []string{}
|
||||||
|
|
||||||
cursor := m.Style.Cursor
|
cursor := m.Data.Style.Cursor
|
||||||
if !m.Data.IsActive {
|
if !m.Data.IsActive {
|
||||||
cursor = m.Style.CursorInactive
|
cursor = m.Data.Style.InactiveCursor
|
||||||
}
|
}
|
||||||
d, _ := osui.CompressString(m.Style.Cursor)
|
d, _ := osui.CompressString(m.Data.Style.Cursor)
|
||||||
empty := strings.Repeat(" ", len(d))
|
empty := strings.Repeat(" ", len(d))
|
||||||
|
|
||||||
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.Style.SelectedFg, m.Style.SelectedBg)+item+colors.Reset+m.Data.DefaultColor)
|
res = append(res, cursor+colors.Combine(m.Data.Style.SelectedBackground, m.Data.Style.SelectedBackground)+item+colors.Reset+m.Data.DefaultColor)
|
||||||
} else {
|
} else {
|
||||||
res = append(res, empty+colors.Combine(m.Style.Fg, m.Style.Bg)+item+colors.Reset+m.Data.DefaultColor)
|
res = append(res, empty+colors.Combine(m.Data.Style.Foreground, m.Data.Style.Background)+item+colors.Reset+m.Data.DefaultColor)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -57,37 +41,33 @@ func (m *MenuComponent) Render() string {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (m *MenuComponent) Update(key string) bool {
|
func (m *MenuComponent) Update(key string) bool {
|
||||||
if isKey.Char(key, "s") {
|
ctx := osui.Context{Key: key}
|
||||||
|
m.Data.OnUpdate(&ctx)
|
||||||
|
if ctx.Response != 0 {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
if isKey.Down(key) {
|
||||||
if m.SelectedItem+1 < len(m.Items) {
|
if m.SelectedItem+1 < len(m.Items) {
|
||||||
m.SelectedItem++
|
m.SelectedItem++
|
||||||
} else {
|
} else {
|
||||||
m.SelectedItem = 0
|
m.SelectedItem = 0
|
||||||
}
|
}
|
||||||
} else if isKey.Char(key, "w") {
|
} else if isKey.Up(key) {
|
||||||
if m.SelectedItem > 0 {
|
if m.SelectedItem > 0 {
|
||||||
m.SelectedItem--
|
m.SelectedItem--
|
||||||
} else {
|
} else {
|
||||||
m.SelectedItem = len(m.Items) - 1
|
m.SelectedItem = len(m.Items) - 1
|
||||||
}
|
}
|
||||||
} else if isKey.Enter(key) {
|
} else if isKey.Enter(key) {
|
||||||
m.OnSelected(m, true)
|
m.Data.OnClick()
|
||||||
return true
|
|
||||||
} else if isKey.Char(key, "q") {
|
|
||||||
m.OnSelected(m, false)
|
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
func (b *MenuComponent) Params(param MenuParams) *MenuComponent {
|
func Menu(param osui.Param, items ...string) *MenuComponent {
|
||||||
b.Style = osui.SetDefaults(¶m.Style).(*MenuStyle)
|
return param.UseParam(&MenuComponent{
|
||||||
b.OnSelected = param.OnSelected
|
|
||||||
return b
|
|
||||||
}
|
|
||||||
|
|
||||||
func Menu(items ...string) *MenuComponent {
|
|
||||||
return &MenuComponent{
|
|
||||||
Items: items,
|
Items: items,
|
||||||
Style: osui.SetDefaults(&MenuStyle{}).(*MenuStyle),
|
}).(*MenuComponent)
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
+4
-11
@@ -34,7 +34,7 @@ func (p *PaginatorComponent) GetComponentData() *osui.ComponentData {
|
|||||||
func (p *PaginatorComponent) Render() string {
|
func (p *PaginatorComponent) Render() string {
|
||||||
width, _ := osui.GetTerminalSize()
|
width, _ := osui.GetTerminalSize()
|
||||||
pgs := strings.Repeat(" ", (width-len(p.Components))/2)
|
pgs := strings.Repeat(" ", (width-len(p.Components))/2)
|
||||||
osui.UseStyle(p.Style)
|
p.Data.Style.UseStyle()
|
||||||
frame := osui.NewFrame(p.Data.Width, p.Data.Height)
|
frame := osui.NewFrame(p.Data.Width, p.Data.Height)
|
||||||
for i, c := range p.Components {
|
for i, c := range p.Components {
|
||||||
data := c.GetComponentData()
|
data := c.GetComponentData()
|
||||||
@@ -98,15 +98,8 @@ func (p *PaginatorComponent) updateActive(newIndex int) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (b *PaginatorComponent) Params(p interface{}) *PaginatorComponent {
|
func Paginator(param osui.Param, pages ...osui.Component) *PaginatorComponent {
|
||||||
param := p.(PaginatorParams)
|
return param.UseParam(&PaginatorComponent{
|
||||||
b.Style = osui.SetDefaults(¶m.Style).(*PaginatorStyle)
|
|
||||||
return b
|
|
||||||
}
|
|
||||||
|
|
||||||
func Paginator(pages ...osui.Component) *PaginatorComponent {
|
|
||||||
return &PaginatorComponent{
|
|
||||||
Components: pages,
|
Components: pages,
|
||||||
Style: osui.SetDefaults(&PaginatorStyle{}).(*PaginatorStyle),
|
}).(*PaginatorComponent)
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
+2
-2
@@ -21,6 +21,6 @@ func (t TextComponent) Render() string {
|
|||||||
return t.Text
|
return t.Text
|
||||||
}
|
}
|
||||||
|
|
||||||
func Text(text string) *TextComponent {
|
func Text(param osui.Param, text string) *TextComponent {
|
||||||
return &TextComponent{Text: text}
|
return param.UseParam(&TextComponent{Text: text}).(*TextComponent)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,13 +4,11 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
"os/exec"
|
"os/exec"
|
||||||
"reflect"
|
|
||||||
"regexp"
|
"regexp"
|
||||||
"runtime"
|
"runtime"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/nathan-fiscaletti/consolesize-go"
|
"github.com/nathan-fiscaletti/consolesize-go"
|
||||||
"github.com/orus-dev/osui/colors"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
var re = regexp.MustCompile(`(\x1b\[([0-9;]*)[a-zA-Z])+`)
|
var re = regexp.MustCompile(`(\x1b\[([0-9;]*)[a-zA-Z])+`)
|
||||||
@@ -99,55 +97,6 @@ func Clear() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func SetDefaults(p interface{}) interface{} {
|
|
||||||
v := reflect.ValueOf(p)
|
|
||||||
if v.Kind() != reflect.Ptr {
|
|
||||||
panic("SetDefaults: expected a pointer to a struct")
|
|
||||||
}
|
|
||||||
val := v.Elem()
|
|
||||||
if val.Kind() != reflect.Struct {
|
|
||||||
panic("SetDefaults: expected a pointer to a struct")
|
|
||||||
}
|
|
||||||
typ := val.Type()
|
|
||||||
for i := 0; i < val.NumField(); i++ {
|
|
||||||
field := val.Field(i)
|
|
||||||
structField := typ.Field(i)
|
|
||||||
if defaultValue, ok := structField.Tag.Lookup("default"); ok {
|
|
||||||
if field.Kind() == reflect.String && field.String() == "" {
|
|
||||||
field.SetString(defaultValue)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return p
|
|
||||||
}
|
|
||||||
|
|
||||||
func UseStyle(p interface{}) {
|
|
||||||
SetDefaults(p)
|
|
||||||
v := reflect.ValueOf(p)
|
|
||||||
if v.Kind() != reflect.Ptr {
|
|
||||||
panic("SetStyle: expected a pointer to a struct")
|
|
||||||
}
|
|
||||||
val := v.Elem()
|
|
||||||
if val.Kind() != reflect.Struct {
|
|
||||||
panic("SetStyle: expected a pointer to a struct")
|
|
||||||
}
|
|
||||||
typ := val.Type()
|
|
||||||
for i := 0; i < val.NumField(); i++ {
|
|
||||||
field := val.Field(i)
|
|
||||||
structField := typ.Field(i)
|
|
||||||
if styleType, ok := structField.Tag.Lookup("type"); ok {
|
|
||||||
if field.Kind() == reflect.String {
|
|
||||||
if styleType == "bg" || styleType == "background" {
|
|
||||||
field.SetString(colors.AsBg(field.String()))
|
|
||||||
} else {
|
|
||||||
field.SetString(colors.AsFg(field.String()))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
func ShowCursor() {
|
func ShowCursor() {
|
||||||
fmt.Print("\033[?25h")
|
fmt.Print("\033[?25h")
|
||||||
}
|
}
|
||||||
@@ -170,7 +119,6 @@ func LogicValueInt(b bool, _if, _else int) int {
|
|||||||
return _else
|
return _else
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get the terminal size
|
|
||||||
func GetTerminalSize() (int, int) {
|
func GetTerminalSize() (int, int) {
|
||||||
return consolesize.GetConsoleSize()
|
return consolesize.GetConsoleSize()
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user