Compare commits
5
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
b2b5c9c00d | ||
|
|
dd8f1fbc1f | ||
|
|
e873c01e50 | ||
|
|
3bab6d3cd6 | ||
|
|
06f9e4577c |
@@ -1,22 +1,25 @@
|
||||
# osui
|
||||
A Component-based TUI library written in go!
|
||||
<h1 style="text-align:center;"><img src="assets/osui.png" width="170px"></img></h1>
|
||||
<h1 style="text-align:center;"></img>A Component-based TUI library written in go</h1>
|
||||
<p style="text-align:center;"><img src="assets/osui.gif"></img></p>
|
||||
|
||||
|
||||
# Hello World
|
||||
```go
|
||||
package main
|
||||
|
||||
import (
|
||||
"github.com/orus-dev/osui/osui"
|
||||
"github.com/orus-dev/osui/osui/ui"
|
||||
"github.com/orus-dev/osui"
|
||||
"github.com/orus-dev/osui/ui"
|
||||
)
|
||||
|
||||
func main() {
|
||||
app := ui.Div(
|
||||
ui.Text("Hello, World!")
|
||||
)
|
||||
|
||||
app := ui.Div(
|
||||
ui.Text("Hello, World!")
|
||||
)
|
||||
|
||||
screen := osui.NewScreen(app)
|
||||
screen.Run()
|
||||
screen := osui.NewScreen(app)
|
||||
screen.Run()
|
||||
}
|
||||
```
|
||||
|
||||
# [Documentation](https://github.com/orus-dev/osui/wiki)
|
||||
|
||||
Binary file not shown.
|
After Width: | Height: | Size: 270 KiB |
Binary file not shown.
|
After Width: | Height: | Size: 12 KiB |
+4
-5
@@ -25,9 +25,8 @@ func App() *PaginatorComponent {
|
||||
WithPosition(0, 0, Text("This blue square is a div. A div stores multiple components into one")),
|
||||
WithPosition((w-30)/2, (h-4)/2,
|
||||
WithSize(30, 5,
|
||||
WithStyle(&DivStyle{Outline: colors.Blue},
|
||||
Div(WithPosition(4, 1, Button("Hello, World!", BtnParams{Width: 19}))),
|
||||
))),
|
||||
Div(WithPosition(4, 1, Button("Hello, World!").Params(BtnParams{Width: 19}))).Params(DivParams{Style: DivStyle{Outline: colors.Blue}}),
|
||||
)),
|
||||
WithPosition(0, h-2, Text(colors.Blue+"Navigate: Ctrl+(W/A/S/D)")),
|
||||
),
|
||||
|
||||
@@ -41,14 +40,14 @@ func App() *PaginatorComponent {
|
||||
// Page 4
|
||||
Div(
|
||||
WithPosition(0, 0, Text("This is a Button. To click it press Enter. "+colors.Red+colors.Bold+"NOTE: This Button is inside a div")),
|
||||
WithPosition((w-20)/2, (h-3)/2, Button("This is a button", BtnParams{OnClick: func(bc *ButtonComponent) bool { return false }})),
|
||||
WithPosition((w-20)/2, (h-3)/2, Button("This is a button")),
|
||||
WithPosition(0, h-2, Text(colors.Blue+"Navigate: Ctrl+(W/A/S/D). Click Button: Enter")),
|
||||
),
|
||||
|
||||
// Page 5
|
||||
Div(
|
||||
WithPosition(0, 0, Text("This is a Menu. It prompts you to select a item. "+colors.Red+colors.Bold+"NOTE: This Menu is inside a div")),
|
||||
WithPosition(0, 2, Menu("Item 1", "Item 2", "Item 3", "Item 4", "Item 5").OnSelected(func(m *MenuComponent, b bool) {})),
|
||||
WithPosition(0, 2, Menu("Item 1", "Item 2", "Item 3", "Item 4", "Item 5").Params(MenuParams{})),
|
||||
WithPosition(0, h-2, Text(colors.Blue+"Navigate: Ctrl+(W/A/S/D). Navigate Menu: W/S. Select: Enter. Exit Menu: Q. ")),
|
||||
),
|
||||
)
|
||||
|
||||
@@ -2,7 +2,9 @@ module github.com/orus-dev/osui
|
||||
|
||||
go 1.23.1
|
||||
|
||||
require golang.org/x/term v0.24.0 // direct
|
||||
|
||||
require (
|
||||
github.com/nathan-fiscaletti/consolesize-go v0.0.0-20220204101620-317176b6684d // indirect
|
||||
golang.org/x/sys v0.25.0 // indirect
|
||||
golang.org/x/term v0.24.0 // direct
|
||||
)
|
||||
|
||||
@@ -1,3 +1,5 @@
|
||||
github.com/nathan-fiscaletti/consolesize-go v0.0.0-20220204101620-317176b6684d h1:NqRhLdNVlozULwM1B3VaHhcXYSgrOAv8V5BE65om+1Q=
|
||||
github.com/nathan-fiscaletti/consolesize-go v0.0.0-20220204101620-317176b6684d/go.mod h1:cxIIfNMTwff8f/ZvRouvWYF6wOoO7nj99neWSx2q/Es=
|
||||
golang.org/x/sys v0.25.0 h1:r+8e+loiHxRqhXVl6ML1nO3l1+oFoWbnlu2Ehimmi34=
|
||||
golang.org/x/sys v0.25.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
|
||||
golang.org/x/term v0.24.0 h1:Mh5cbb+Zk2hqqXNO7S1iTjEphVL+jb8ZWaqh/g+JWkM=
|
||||
|
||||
@@ -21,7 +21,6 @@ type Component interface {
|
||||
Render() string
|
||||
GetComponentData() *ComponentData
|
||||
Update(string) bool
|
||||
SetStyle(interface{})
|
||||
}
|
||||
|
||||
type Screen struct {
|
||||
|
||||
+3
-2
@@ -24,8 +24,9 @@ func (h *HelloComponent) Update(key string) bool {
|
||||
return false
|
||||
}
|
||||
|
||||
func (h *HelloComponent) SetStyle(c interface{}) {
|
||||
h.Style = osui.SetDefaults(c.(*HelloStyle)).(*HelloStyle)
|
||||
func (b *MenuComponent) Params(param MenuParams) *MenuComponent {
|
||||
b.Style = osui.SetDefaults(¶m.Style).(*MenuStyle)
|
||||
return b
|
||||
}
|
||||
|
||||
func Hello(name string) *HelloComponent {
|
||||
|
||||
+24
-30
@@ -10,6 +10,13 @@ import (
|
||||
"github.com/orus-dev/osui/isKey"
|
||||
)
|
||||
|
||||
type BtnParams struct {
|
||||
Style ButtonStyle
|
||||
Toggle bool
|
||||
OnClick func(*ButtonComponent) bool
|
||||
Width int
|
||||
}
|
||||
|
||||
type ButtonStyle struct {
|
||||
Background string `default:"" type:"bg"`
|
||||
Foreground string `default:"" type:"fg"`
|
||||
@@ -25,12 +32,12 @@ type ButtonStyle struct {
|
||||
}
|
||||
|
||||
type ButtonComponent struct {
|
||||
Data osui.ComponentData
|
||||
Style *ButtonStyle
|
||||
Text string
|
||||
Toggle bool
|
||||
Clicked bool
|
||||
OnClick func(*ButtonComponent) bool
|
||||
Data osui.ComponentData
|
||||
Style *ButtonStyle
|
||||
Text string
|
||||
Toggle bool
|
||||
Clicked bool
|
||||
on_click func(*ButtonComponent) bool
|
||||
}
|
||||
|
||||
func (b *ButtonComponent) Render() string {
|
||||
@@ -67,7 +74,7 @@ func (b *ButtonComponent) Render() string {
|
||||
|
||||
func (b *ButtonComponent) Update(key string) bool {
|
||||
if isKey.Enter(key) {
|
||||
if !b.OnClick(b) {
|
||||
if !b.on_click(b) {
|
||||
if b.Toggle {
|
||||
b.Clicked = !b.Clicked
|
||||
return false
|
||||
@@ -85,35 +92,22 @@ func (b *ButtonComponent) GetComponentData() *osui.ComponentData {
|
||||
return &b.Data
|
||||
}
|
||||
|
||||
func (b *ButtonComponent) SetStyle(style interface{}) {
|
||||
b.Style = osui.SetDefaults(style).(*ButtonStyle)
|
||||
}
|
||||
|
||||
type BtnParams struct {
|
||||
Style ButtonStyle
|
||||
Toggle bool
|
||||
OnClick func(*ButtonComponent) bool
|
||||
Width int
|
||||
}
|
||||
|
||||
func Button(text string, p ...BtnParams) *ButtonComponent {
|
||||
if len(p) > 0 {
|
||||
param := p[0]
|
||||
return &ButtonComponent{Text: text, Style: osui.SetDefaults(¶m.Style).(*ButtonStyle),
|
||||
Data: osui.ComponentData{
|
||||
Width: osui.LogicValueInt(param.Width == 0, 20, param.Width),
|
||||
Height: 1,
|
||||
},
|
||||
OnClick: fnLogicValue(param.OnClick),
|
||||
}
|
||||
|
||||
func (b *ButtonComponent) Params(param BtnParams) *ButtonComponent {
|
||||
b.Style = osui.SetDefaults(¶m.Style).(*ButtonStyle)
|
||||
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{
|
||||
Width: 20,
|
||||
Height: 1,
|
||||
},
|
||||
OnClick: func(bc *ButtonComponent) bool { return false },
|
||||
on_click: func(bc *ButtonComponent) bool { return false },
|
||||
}
|
||||
}
|
||||
|
||||
@@ -8,6 +8,12 @@ import (
|
||||
"github.com/orus-dev/osui/isKey"
|
||||
)
|
||||
|
||||
type DivParams struct {
|
||||
Style DivStyle
|
||||
Width int
|
||||
Height int
|
||||
}
|
||||
|
||||
type DivStyle struct {
|
||||
Background string `default:"" type:"bg"`
|
||||
Foreground string `default:"" type:"fg"`
|
||||
@@ -82,8 +88,10 @@ func (d *DivComponent) Update(key string) bool {
|
||||
return false
|
||||
}
|
||||
|
||||
func (d *DivComponent) SetStyle(c interface{}) {
|
||||
d.Style = osui.SetDefaults(c.(*DivStyle)).(*DivStyle)
|
||||
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) {
|
||||
@@ -92,11 +100,6 @@ func (d *DivComponent) updateActive(newIndex int) {
|
||||
}
|
||||
}
|
||||
|
||||
func (d *DivComponent) AddTo(c *DivComponent) *DivComponent {
|
||||
*c = *d
|
||||
return d
|
||||
}
|
||||
|
||||
func Div(components ...osui.Component) *DivComponent {
|
||||
return &DivComponent{
|
||||
Components: components,
|
||||
|
||||
+11
-2
@@ -9,6 +9,12 @@ import (
|
||||
"github.com/orus-dev/osui/isKey"
|
||||
)
|
||||
|
||||
type InputBoxParams struct {
|
||||
Style DivStyle
|
||||
Width int
|
||||
Height int
|
||||
}
|
||||
|
||||
type InputBoxStyle struct {
|
||||
Background string `default:"" type:"bg"`
|
||||
Foreground string `default:"" type:"fg"`
|
||||
@@ -77,8 +83,11 @@ func (s *InputBoxComponent) Update(key string) bool {
|
||||
return false
|
||||
}
|
||||
|
||||
func (d *InputBoxComponent) SetStyle(c interface{}) {
|
||||
d.Style = osui.SetDefaults(c.(*InputBoxStyle)).(*InputBoxStyle)
|
||||
func (b *InputBoxComponent) Params(p interface{}) *InputBoxComponent {
|
||||
param := p.(InputBoxParams)
|
||||
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 {
|
||||
|
||||
+8
-7
@@ -8,6 +8,11 @@ import (
|
||||
"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"`
|
||||
@@ -74,13 +79,9 @@ func (m *MenuComponent) Update(key string) bool {
|
||||
return false
|
||||
}
|
||||
|
||||
func (m *MenuComponent) SetStyle(c interface{}) {
|
||||
m.Style = osui.SetDefaults(c.(*MenuStyle)).(*MenuStyle)
|
||||
}
|
||||
|
||||
func (m *MenuComponent) OnSelected(f func(*MenuComponent, bool)) *MenuComponent {
|
||||
m.onSelected = f
|
||||
return m
|
||||
func (b *MenuComponent) Params(param MenuParams) *MenuComponent {
|
||||
b.Style = osui.SetDefaults(¶m.Style).(*MenuStyle)
|
||||
return b
|
||||
}
|
||||
|
||||
func Menu(items ...string) *MenuComponent {
|
||||
|
||||
+10
-2
@@ -9,6 +9,12 @@ import (
|
||||
"github.com/orus-dev/osui/isKey"
|
||||
)
|
||||
|
||||
type PaginatorParams struct {
|
||||
Style DivStyle
|
||||
Width int
|
||||
Height int
|
||||
}
|
||||
|
||||
type PaginatorStyle struct {
|
||||
Active string `default:"\033[32m"`
|
||||
Inactive string `default:""`
|
||||
@@ -92,8 +98,10 @@ func (p *PaginatorComponent) updateActive(newIndex int) {
|
||||
}
|
||||
}
|
||||
|
||||
func (p *PaginatorComponent) SetStyle(c interface{}) {
|
||||
p.Style = osui.SetDefaults(c.(*PaginatorStyle)).(*PaginatorStyle)
|
||||
func (b *PaginatorComponent) Params(p interface{}) *PaginatorComponent {
|
||||
param := p.(PaginatorParams)
|
||||
b.Style = osui.SetDefaults(¶m.Style).(*PaginatorStyle)
|
||||
return b
|
||||
}
|
||||
|
||||
func Paginator(pages ...osui.Component) *PaginatorComponent {
|
||||
|
||||
@@ -21,8 +21,6 @@ func (t TextComponent) Render() string {
|
||||
return t.Text
|
||||
}
|
||||
|
||||
func (t *TextComponent) SetStyle(interface{}) {}
|
||||
|
||||
func Text(text string) *TextComponent {
|
||||
return &TextComponent{Text: text}
|
||||
}
|
||||
|
||||
+14
-27
@@ -8,6 +8,20 @@ import (
|
||||
"github.com/orus-dev/osui"
|
||||
)
|
||||
|
||||
func WithPosition(x, y int, c osui.Component) osui.Component {
|
||||
data := c.GetComponentData()
|
||||
data.X = x
|
||||
data.Y = y
|
||||
return c
|
||||
}
|
||||
|
||||
func WithSize(width, height int, c osui.Component) osui.Component {
|
||||
data := c.GetComponentData()
|
||||
data.Width = width
|
||||
data.Height = height
|
||||
return c
|
||||
}
|
||||
|
||||
func centerText(text string, width int) string {
|
||||
if len(text) > width {
|
||||
return text[:width]
|
||||
@@ -19,33 +33,6 @@ func centerText(text string, width int) string {
|
||||
return fmt.Sprintf("%s%s%s", leftPadding, text, rightPadding)
|
||||
}
|
||||
|
||||
func fnLogicValue(f func(*ButtonComponent) bool) func(*ButtonComponent) bool {
|
||||
if f == nil {
|
||||
return func(*ButtonComponent) bool { return false }
|
||||
} else {
|
||||
return f
|
||||
}
|
||||
}
|
||||
|
||||
func WithPosition(x, y int, c osui.Component) osui.Component {
|
||||
data := c.GetComponentData()
|
||||
data.X = x
|
||||
data.Y = y
|
||||
return c
|
||||
}
|
||||
|
||||
func WithStyle(style interface{}, c osui.Component) osui.Component {
|
||||
c.SetStyle(style)
|
||||
return c
|
||||
}
|
||||
|
||||
func WithSize(width, height int, c osui.Component) osui.Component {
|
||||
data := c.GetComponentData()
|
||||
data.Width = width
|
||||
data.Height = height
|
||||
return c
|
||||
}
|
||||
|
||||
func findClosestComponent(a []osui.Component, i int, d string) int {
|
||||
if len(a) == 0 || i < 0 || i >= len(a) {
|
||||
return -1 // Invalid input
|
||||
|
||||
@@ -9,6 +9,7 @@ import (
|
||||
"runtime"
|
||||
"strings"
|
||||
|
||||
"github.com/nathan-fiscaletti/consolesize-go"
|
||||
"github.com/orus-dev/osui/colors"
|
||||
"golang.org/x/term"
|
||||
)
|
||||
@@ -174,9 +175,5 @@ func LogicValueInt(b bool, _if, _else int) int {
|
||||
}
|
||||
|
||||
func GetTerminalSize() (int, int) {
|
||||
w, h, err := term.GetSize(0)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
return w, h
|
||||
return consolesize.GetConsoleSize()
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user