7 Commits
Author SHA1 Message Date
selimaj-dev 6cdb685eb5 small change 2024-09-29 22:13:54 +02:00
selimaj-dev cdb8576cc4 readme update 2024-09-29 22:11:05 +02:00
selimaj-dev b2b5c9c00d windows bug fix 02 2024-09-29 21:52:17 +02:00
selimaj-dev dd8f1fbc1f bug fixes and clean code 2024-09-29 21:22:25 +02:00
Rudi e873c01e50 wow 2024-09-29 17:35:30 +02:00
selimaj-dev 3bab6d3cd6 readme update 2024-09-29 14:39:33 +02:00
selimaj-dev 06f9e4577c readme update 2024-09-29 14:39:25 +02:00
16 changed files with 112 additions and 102 deletions
+19 -11
View File
@@ -1,22 +1,30 @@
# osui <div style="text-align: center;">
A Component-based TUI library written in go! <img src="assets/osui.png" width="170px" />
<h1>A Component-based TUI library written in Go</h1>
<img src="assets/osui.gif" />
</div>
# Hello World
```go ```go
package main package main
import ( import (
"github.com/orus-dev/osui/osui" "github.com/orus-dev/osui"
"github.com/orus-dev/osui/osui/ui" "github.com/orus-dev/osui/ui"
) )
func main() { func main() {
app := ui.Div(
ui.Text("Hello, World!")
)
app := ui.Div( screen := osui.NewScreen(app)
ui.Text("Hello, World!") screen.Run()
)
screen := osui.NewScreen(app)
screen.Run()
} }
``` ```
# [Documentation](https://github.com/orus-dev/osui/wiki)
BIN
View File
Binary file not shown.

After

Width:  |  Height:  |  Size: 270 KiB

BIN
View File
Binary file not shown.

After

Width:  |  Height:  |  Size: 12 KiB

+4 -5
View File
@@ -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(0, 0, Text("This blue square is a div. A div stores multiple components into one")),
WithPosition((w-30)/2, (h-4)/2, WithPosition((w-30)/2, (h-4)/2,
WithSize(30, 5, WithSize(30, 5,
WithStyle(&DivStyle{Outline: colors.Blue}, Div(WithPosition(4, 1, Button("Hello, World!").Params(BtnParams{Width: 19}))).Params(DivParams{Style: DivStyle{Outline: colors.Blue}}),
Div(WithPosition(4, 1, Button("Hello, World!", BtnParams{Width: 19}))), )),
))),
WithPosition(0, h-2, Text(colors.Blue+"Navigate: Ctrl+(W/A/S/D)")), WithPosition(0, h-2, Text(colors.Blue+"Navigate: Ctrl+(W/A/S/D)")),
), ),
@@ -41,14 +40,14 @@ func App() *PaginatorComponent {
// Page 4 // Page 4
Div( 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(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")), WithPosition(0, h-2, Text(colors.Blue+"Navigate: Ctrl+(W/A/S/D). Click Button: Enter")),
), ),
// Page 5 // Page 5
Div( 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, 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. ")), WithPosition(0, h-2, Text(colors.Blue+"Navigate: Ctrl+(W/A/S/D). Navigate Menu: W/S. Select: Enter. Exit Menu: Q. ")),
), ),
) )
+3 -1
View File
@@ -2,7 +2,9 @@ module github.com/orus-dev/osui
go 1.23.1 go 1.23.1
require golang.org/x/term v0.24.0 // direct
require ( require (
github.com/nathan-fiscaletti/consolesize-go v0.0.0-20220204101620-317176b6684d // indirect
golang.org/x/sys v0.25.0 // indirect golang.org/x/sys v0.25.0 // indirect
golang.org/x/term v0.24.0 // direct
) )
+2
View File
@@ -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 h1:r+8e+loiHxRqhXVl6ML1nO3l1+oFoWbnlu2Ehimmi34=
golang.org/x/sys v0.25.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= 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= golang.org/x/term v0.24.0 h1:Mh5cbb+Zk2hqqXNO7S1iTjEphVL+jb8ZWaqh/g+JWkM=
-1
View File
@@ -21,7 +21,6 @@ type Component interface {
Render() string Render() string
GetComponentData() *ComponentData GetComponentData() *ComponentData
Update(string) bool Update(string) bool
SetStyle(interface{})
} }
type Screen struct { type Screen struct {
+3 -2
View File
@@ -24,8 +24,9 @@ func (h *HelloComponent) Update(key string) bool {
return false return false
} }
func (h *HelloComponent) SetStyle(c interface{}) { func (b *MenuComponent) Params(param MenuParams) *MenuComponent {
h.Style = osui.SetDefaults(c.(*HelloStyle)).(*HelloStyle) b.Style = osui.SetDefaults(&param.Style).(*MenuStyle)
return b
} }
func Hello(name string) *HelloComponent { func Hello(name string) *HelloComponent {
+24 -30
View File
@@ -10,6 +10,13 @@ import (
"github.com/orus-dev/osui/isKey" "github.com/orus-dev/osui/isKey"
) )
type BtnParams struct {
Style ButtonStyle
Toggle bool
OnClick func(*ButtonComponent) bool
Width int
}
type ButtonStyle struct { type ButtonStyle struct {
Background string `default:"" type:"bg"` Background string `default:"" type:"bg"`
Foreground string `default:"" type:"fg"` Foreground string `default:"" type:"fg"`
@@ -25,12 +32,12 @@ type ButtonStyle struct {
} }
type ButtonComponent struct { type ButtonComponent struct {
Data osui.ComponentData Data osui.ComponentData
Style *ButtonStyle Style *ButtonStyle
Text string Text string
Toggle bool Toggle bool
Clicked bool Clicked bool
OnClick func(*ButtonComponent) bool on_click func(*ButtonComponent) bool
} }
func (b *ButtonComponent) Render() string { func (b *ButtonComponent) Render() string {
@@ -67,7 +74,7 @@ func (b *ButtonComponent) Render() string {
func (b *ButtonComponent) Update(key string) bool { func (b *ButtonComponent) Update(key string) bool {
if isKey.Enter(key) { if isKey.Enter(key) {
if !b.OnClick(b) { if !b.on_click(b) {
if b.Toggle { if b.Toggle {
b.Clicked = !b.Clicked b.Clicked = !b.Clicked
return false return false
@@ -85,35 +92,22 @@ func (b *ButtonComponent) GetComponentData() *osui.ComponentData {
return &b.Data return &b.Data
} }
func (b *ButtonComponent) SetStyle(style interface{}) { func (b *ButtonComponent) Params(param BtnParams) *ButtonComponent {
b.Style = osui.SetDefaults(style).(*ButtonStyle) b.Style = osui.SetDefaults(&param.Style).(*ButtonStyle)
} if param.OnClick != nil {
b.on_click = param.OnClick
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(&param.Style).(*ButtonStyle),
Data: osui.ComponentData{
Width: osui.LogicValueInt(param.Width == 0, 20, param.Width),
Height: 1,
},
OnClick: fnLogicValue(param.OnClick),
}
} }
b.Data.Width = osui.LogicValueInt(param.Width == 0, 20, param.Width)
return b
}
func Button(text string) *ButtonComponent {
return &ButtonComponent{Text: text, return &ButtonComponent{Text: text,
Style: osui.SetDefaults(&ButtonStyle{}).(*ButtonStyle), Style: osui.SetDefaults(&ButtonStyle{}).(*ButtonStyle),
Data: osui.ComponentData{ Data: osui.ComponentData{
Width: 20, Width: 20,
Height: 1, Height: 1,
}, },
OnClick: func(bc *ButtonComponent) bool { return false }, on_click: func(bc *ButtonComponent) bool { return false },
} }
} }
+10 -7
View File
@@ -8,6 +8,12 @@ import (
"github.com/orus-dev/osui/isKey" "github.com/orus-dev/osui/isKey"
) )
type DivParams struct {
Style DivStyle
Width int
Height int
}
type DivStyle struct { type DivStyle struct {
Background string `default:"" type:"bg"` Background string `default:"" type:"bg"`
Foreground string `default:"" type:"fg"` Foreground string `default:"" type:"fg"`
@@ -82,8 +88,10 @@ func (d *DivComponent) Update(key string) bool {
return false return false
} }
func (d *DivComponent) SetStyle(c interface{}) { func (b *DivComponent) Params(param DivParams) *DivComponent {
d.Style = osui.SetDefaults(c.(*DivStyle)).(*DivStyle) b.Style = osui.SetDefaults(&param.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) {
@@ -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 { func Div(components ...osui.Component) *DivComponent {
return &DivComponent{ return &DivComponent{
Components: components, Components: components,
+11 -2
View File
@@ -9,6 +9,12 @@ import (
"github.com/orus-dev/osui/isKey" "github.com/orus-dev/osui/isKey"
) )
type InputBoxParams struct {
Style DivStyle
Width int
Height int
}
type InputBoxStyle struct { type InputBoxStyle struct {
Background string `default:"" type:"bg"` Background string `default:"" type:"bg"`
Foreground string `default:"" type:"fg"` Foreground string `default:"" type:"fg"`
@@ -77,8 +83,11 @@ func (s *InputBoxComponent) Update(key string) bool {
return false return false
} }
func (d *InputBoxComponent) SetStyle(c interface{}) { func (b *InputBoxComponent) Params(p interface{}) *InputBoxComponent {
d.Style = osui.SetDefaults(c.(*InputBoxStyle)).(*InputBoxStyle) param := p.(InputBoxParams)
b.Style = osui.SetDefaults(&param.Style).(*InputBoxStyle)
b.Data.Width = osui.LogicValueInt(param.Width == 0, 20, param.Width)
return b
} }
func InputBox(max_size uint) *InputBoxComponent { func InputBox(max_size uint) *InputBoxComponent {
+8 -7
View File
@@ -8,6 +8,11 @@ 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 { type MenuStyle struct {
Fg string `default:"" type:"fg"` Fg string `default:"" type:"fg"`
Bg string `default:"" type:"bg"` Bg string `default:"" type:"bg"`
@@ -74,13 +79,9 @@ func (m *MenuComponent) Update(key string) bool {
return false return false
} }
func (m *MenuComponent) SetStyle(c interface{}) { func (b *MenuComponent) Params(param MenuParams) *MenuComponent {
m.Style = osui.SetDefaults(c.(*MenuStyle)).(*MenuStyle) b.Style = osui.SetDefaults(&param.Style).(*MenuStyle)
} return b
func (m *MenuComponent) OnSelected(f func(*MenuComponent, bool)) *MenuComponent {
m.onSelected = f
return m
} }
func Menu(items ...string) *MenuComponent { func Menu(items ...string) *MenuComponent {
+10 -2
View File
@@ -9,6 +9,12 @@ import (
"github.com/orus-dev/osui/isKey" "github.com/orus-dev/osui/isKey"
) )
type PaginatorParams struct {
Style DivStyle
Width int
Height int
}
type PaginatorStyle struct { type PaginatorStyle struct {
Active string `default:"\033[32m"` Active string `default:"\033[32m"`
Inactive string `default:""` Inactive string `default:""`
@@ -92,8 +98,10 @@ func (p *PaginatorComponent) updateActive(newIndex int) {
} }
} }
func (p *PaginatorComponent) SetStyle(c interface{}) { func (b *PaginatorComponent) Params(p interface{}) *PaginatorComponent {
p.Style = osui.SetDefaults(c.(*PaginatorStyle)).(*PaginatorStyle) param := p.(PaginatorParams)
b.Style = osui.SetDefaults(&param.Style).(*PaginatorStyle)
return b
} }
func Paginator(pages ...osui.Component) *PaginatorComponent { func Paginator(pages ...osui.Component) *PaginatorComponent {
-2
View File
@@ -21,8 +21,6 @@ func (t TextComponent) Render() string {
return t.Text return t.Text
} }
func (t *TextComponent) SetStyle(interface{}) {}
func Text(text string) *TextComponent { func Text(text string) *TextComponent {
return &TextComponent{Text: text} return &TextComponent{Text: text}
} }
+14 -27
View File
@@ -8,6 +8,20 @@ import (
"github.com/orus-dev/osui" "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 { func centerText(text string, width int) string {
if len(text) > width { if len(text) > width {
return 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) 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 { func findClosestComponent(a []osui.Component, i int, d string) int {
if len(a) == 0 || i < 0 || i >= len(a) { if len(a) == 0 || i < 0 || i >= len(a) {
return -1 // Invalid input return -1 // Invalid input
+4 -5
View File
@@ -9,6 +9,7 @@ import (
"runtime" "runtime"
"strings" "strings"
"github.com/nathan-fiscaletti/consolesize-go"
"github.com/orus-dev/osui/colors" "github.com/orus-dev/osui/colors"
"golang.org/x/term" "golang.org/x/term"
) )
@@ -173,10 +174,8 @@ func LogicValueInt(b bool, _if, _else int) int {
return _else return _else
} }
// Get the terminal size
func GetTerminalSize() (int, int) { func GetTerminalSize() (int, int) {
w, h, err := term.GetSize(0) return consolesize.GetConsoleSize()
if err != nil {
panic(err)
}
return w, h
} }