Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
6cdb685eb5 | ||
|
|
cdb8576cc4 | ||
|
|
b2b5c9c00d | ||
|
|
dd8f1fbc1f | ||
|
|
e873c01e50 | ||
|
|
3bab6d3cd6 | ||
|
|
06f9e4577c | ||
|
|
716d1d4a08 | ||
|
|
8592b1cc50 | ||
|
|
90f106f127 | ||
|
|
5a94fc34ad | ||
|
|
fdbb54a557 | ||
|
|
5024087358 | ||
|
|
cf93b3383f | ||
|
|
3b2ff24c1f | ||
|
|
4214f96c61 | ||
|
|
3b3e92fae5 | ||
|
|
179de20dba |
@@ -23,3 +23,6 @@ go.work.sum
|
|||||||
|
|
||||||
# env file
|
# env file
|
||||||
.env
|
.env
|
||||||
|
|
||||||
|
# macOS
|
||||||
|
.DS_Store
|
||||||
@@ -1,18 +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() {
|
||||||
components := osui.NewRender()
|
app := ui.Div(
|
||||||
components.Add(ui.Text("Hello, World!"))
|
ui.Text("Hello, World!")
|
||||||
components.Render()
|
)
|
||||||
|
|
||||||
|
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 |
@@ -0,0 +1,60 @@
|
|||||||
|
package colors
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"regexp"
|
||||||
|
"strings"
|
||||||
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
Reset = "\x1b[0m"
|
||||||
|
Bold = "\x1b[1m"
|
||||||
|
Underline = "\x1b[4m"
|
||||||
|
Italic = "\x1b[3m"
|
||||||
|
Reverse = "\x1b[7m"
|
||||||
|
Strike = "\x1b[9m"
|
||||||
|
|
||||||
|
Black = "\x1b[30m"
|
||||||
|
Red = "\x1b[31m"
|
||||||
|
Green = "\x1b[32m"
|
||||||
|
Yellow = "\x1b[33m"
|
||||||
|
Blue = "\x1b[34m"
|
||||||
|
Magenta = "\x1b[35m"
|
||||||
|
Cyan = "\x1b[36m"
|
||||||
|
White = "\x1b[37m"
|
||||||
|
)
|
||||||
|
|
||||||
|
func Rgb(r, g, b uint8) string {
|
||||||
|
return fmt.Sprintf("\x1b[38;2;%d;%d;%dm", r, g, b)
|
||||||
|
}
|
||||||
|
|
||||||
|
func AsBg(s string) string {
|
||||||
|
re, _ := regexp.Compile(`\x1b\[3([0-9]+)`)
|
||||||
|
return re.ReplaceAllStringFunc(s, func(match string) string {
|
||||||
|
groups := re.FindStringSubmatch(match)
|
||||||
|
if len(groups) >= 2 {
|
||||||
|
return fmt.Sprintf("\x1b[4%s", groups[1])
|
||||||
|
}
|
||||||
|
return match
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
func AsFg(s string) string {
|
||||||
|
re, _ := regexp.Compile(`\x1b\[4([0-9]+)`)
|
||||||
|
return re.ReplaceAllStringFunc(s, func(match string) string {
|
||||||
|
groups := re.FindStringSubmatch(match)
|
||||||
|
if len(groups) >= 2 {
|
||||||
|
return fmt.Sprintf("\x1b[3%s", groups[1])
|
||||||
|
}
|
||||||
|
return match
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
func Combine(s, s1 string) string {
|
||||||
|
if strings.Contains(s, Reset) {
|
||||||
|
return s + s1
|
||||||
|
} else {
|
||||||
|
return s1 + s
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,54 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/orus-dev/osui"
|
||||||
|
"github.com/orus-dev/osui/colors"
|
||||||
|
. "github.com/orus-dev/osui/ui"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
screen := osui.NewScreen(App())
|
||||||
|
screen.Run()
|
||||||
|
}
|
||||||
|
|
||||||
|
func App() *PaginatorComponent {
|
||||||
|
w, h := osui.GetTerminalSize()
|
||||||
|
return Paginator(
|
||||||
|
// Page 1
|
||||||
|
Div(
|
||||||
|
Text("Welcome to the example! The blue text on the bottom show the keybindings"),
|
||||||
|
WithPosition(0, h-2, Text(colors.Blue+"Next Page: Tab. Previous Page: shift + tab")),
|
||||||
|
),
|
||||||
|
|
||||||
|
// Page 2
|
||||||
|
Div(
|
||||||
|
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,
|
||||||
|
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)")),
|
||||||
|
),
|
||||||
|
|
||||||
|
// Page 3
|
||||||
|
Div(
|
||||||
|
WithPosition(0, 0, Text("This is a InputBox. It takes user input just like a GUI. "+colors.Red+colors.Bold+"NOTE: This InputBox is inside a div")),
|
||||||
|
WithPosition((w-32)/2, (h-3)/2, InputBox(30)),
|
||||||
|
WithPosition(0, h-2, Text(colors.Blue+"Navigate: W/S. Select: Enter. Exit Menu: Q. ")),
|
||||||
|
),
|
||||||
|
|
||||||
|
// 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")),
|
||||||
|
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").Params(MenuParams{})),
|
||||||
|
WithPosition(0, h-2, Text(colors.Blue+"Navigate: Ctrl+(W/A/S/D). Navigate Menu: W/S. Select: Enter. Exit Menu: Q. ")),
|
||||||
|
),
|
||||||
|
)
|
||||||
|
}
|
||||||
@@ -1,16 +0,0 @@
|
|||||||
package main
|
|
||||||
|
|
||||||
import (
|
|
||||||
"github.com/orus-dev/osui"
|
|
||||||
"github.com/orus-dev/osui/styles"
|
|
||||||
"github.com/orus-dev/osui/ui"
|
|
||||||
)
|
|
||||||
|
|
||||||
func main() {
|
|
||||||
colors := ui.Text(
|
|
||||||
styles.Red + "Red!\n" + styles.Green + "Green!\n" + styles.Blue + "Blue!" + styles.Reset,
|
|
||||||
)
|
|
||||||
paginator := ui.Paginator(ui.Div(colors), ui.Div(ui.InputBox(30)))
|
|
||||||
screen := osui.NewScreen(paginator)
|
|
||||||
screen.Run()
|
|
||||||
}
|
|
||||||
@@ -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
|
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -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=
|
||||||
|
|||||||
@@ -0,0 +1,53 @@
|
|||||||
|
package isKey
|
||||||
|
|
||||||
|
func Enter(s string) bool {
|
||||||
|
return s == "\r" || s == "\n"
|
||||||
|
}
|
||||||
|
|
||||||
|
func Tab(s string) bool {
|
||||||
|
return s == "\t"
|
||||||
|
}
|
||||||
|
|
||||||
|
func Backspace(s string) bool {
|
||||||
|
return s == "\x7f" || s == "\b"
|
||||||
|
}
|
||||||
|
|
||||||
|
func Escape(s string) bool {
|
||||||
|
return s == "\x1b"
|
||||||
|
}
|
||||||
|
|
||||||
|
func Up(s string) bool {
|
||||||
|
return s == "\x1b[A"
|
||||||
|
}
|
||||||
|
|
||||||
|
func Down(s string) bool {
|
||||||
|
return s == "\x1b[B"
|
||||||
|
}
|
||||||
|
|
||||||
|
func Right(s string) bool {
|
||||||
|
return s == "\x1b[C"
|
||||||
|
}
|
||||||
|
|
||||||
|
func Left(s string) bool {
|
||||||
|
return s == "\x1b[D"
|
||||||
|
}
|
||||||
|
|
||||||
|
func Char(s string, s1 string) bool {
|
||||||
|
return s == s1
|
||||||
|
}
|
||||||
|
|
||||||
|
func CtrlW(s string) bool {
|
||||||
|
return s == "\x17"
|
||||||
|
}
|
||||||
|
|
||||||
|
func CtrlS(s string) bool {
|
||||||
|
return s == "\x13"
|
||||||
|
}
|
||||||
|
|
||||||
|
func CtrlA(s string) bool {
|
||||||
|
return s == "\x01"
|
||||||
|
}
|
||||||
|
|
||||||
|
func CtrlD(s string) bool {
|
||||||
|
return s == "\x04"
|
||||||
|
}
|
||||||
@@ -4,14 +4,15 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"golang.org/x/term"
|
"github.com/orus-dev/osui/colors"
|
||||||
)
|
)
|
||||||
|
|
||||||
type ComponentData struct {
|
type ComponentData struct {
|
||||||
X uint16
|
X int
|
||||||
Y uint16
|
Y int
|
||||||
Width int
|
Width int
|
||||||
Height int
|
Height int
|
||||||
|
DefaultColor string
|
||||||
IsActive bool
|
IsActive bool
|
||||||
Screen *Screen
|
Screen *Screen
|
||||||
}
|
}
|
||||||
@@ -32,26 +33,27 @@ func NewScreen(c Component) *Screen {
|
|||||||
return s
|
return s
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Screen) Render() error {
|
func (s *Screen) Render() {
|
||||||
Clear()
|
Clear()
|
||||||
width, height, err := term.GetSize(0)
|
width, height := GetTerminalSize()
|
||||||
if err != nil {
|
|
||||||
return fmt.Errorf("error getting the terminal size: %s", err)
|
|
||||||
}
|
|
||||||
frame := NewFrame(width, height)
|
frame := NewFrame(width, height)
|
||||||
data := s.component.GetComponentData()
|
data := s.component.GetComponentData()
|
||||||
data.Width = width
|
if data.Height == 0 {
|
||||||
data.Height = height
|
data.Height = height
|
||||||
|
}
|
||||||
|
if data.Width == 0 {
|
||||||
|
data.Width = width
|
||||||
|
}
|
||||||
data.Screen = s
|
data.Screen = s
|
||||||
data.IsActive = true
|
data.IsActive = true
|
||||||
|
data.DefaultColor = colors.Reset
|
||||||
RenderOnFrame(s.component, &frame)
|
RenderOnFrame(s.component, &frame)
|
||||||
fmt.Print(strings.Join(frame, "\n"))
|
fmt.Print(strings.Join(frame, "\n"))
|
||||||
return nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Screen) Run() {
|
func (s *Screen) Run() {
|
||||||
s.component.GetComponentData().Screen = s
|
data := s.component.GetComponentData()
|
||||||
s.component.Update("OSUI:RENDER")
|
data.Screen = s
|
||||||
for {
|
for {
|
||||||
s.Render()
|
s.Render()
|
||||||
k, _ := ReadKey()
|
k, _ := ReadKey()
|
||||||
|
|||||||
@@ -1,36 +0,0 @@
|
|||||||
package styles
|
|
||||||
|
|
||||||
import "fmt"
|
|
||||||
|
|
||||||
const (
|
|
||||||
Reset = "\033[0m"
|
|
||||||
Bold = "\033[1m"
|
|
||||||
Underline = "\033[4m"
|
|
||||||
Italic = "\033[3m"
|
|
||||||
Reverse = "\033[7m"
|
|
||||||
Strike = "\033[9m"
|
|
||||||
|
|
||||||
// Font Colors
|
|
||||||
Black = "\033[30m"
|
|
||||||
Red = "\033[31m"
|
|
||||||
Green = "\033[32m"
|
|
||||||
Yellow = "\033[33m"
|
|
||||||
Blue = "\033[34m"
|
|
||||||
Magenta = "\033[35m"
|
|
||||||
Cyan = "\033[36m"
|
|
||||||
White = "\033[37m"
|
|
||||||
|
|
||||||
// Background Colors
|
|
||||||
BgBlack = "\033[40m"
|
|
||||||
BgRed = "\033[41m"
|
|
||||||
BgGreen = "\033[42m"
|
|
||||||
BgYellow = "\033[43m"
|
|
||||||
BgBlue = "\033[44m"
|
|
||||||
BgMagenta = "\033[45m"
|
|
||||||
BgCyan = "\033[46m"
|
|
||||||
BgWhite = "\033[47m"
|
|
||||||
)
|
|
||||||
|
|
||||||
func Rgb(r, g, b uint8) string {
|
|
||||||
return fmt.Sprintf("\x1b[38;2;%d;%d;%dm", r, g, b)
|
|
||||||
}
|
|
||||||
@@ -0,0 +1,38 @@
|
|||||||
|
# Component template (use rename symbol)
|
||||||
|
|
||||||
|
```go
|
||||||
|
type HelloStyle struct {
|
||||||
|
Fg string `defaults:"" type:"fg"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type HelloComponent struct {
|
||||||
|
Data osui.ComponentData
|
||||||
|
Style *HelloStyle
|
||||||
|
Name string
|
||||||
|
}
|
||||||
|
|
||||||
|
func (h *HelloComponent) GetComponentData() *osui.ComponentData {
|
||||||
|
return &h.Data
|
||||||
|
}
|
||||||
|
|
||||||
|
func (h *HelloComponent) Render() string {
|
||||||
|
osui.UseStyle(h.Style)
|
||||||
|
return "Hello " + h.Style.Fg + h.Name + colors.Reset + "!"
|
||||||
|
}
|
||||||
|
|
||||||
|
func (h *HelloComponent) Update(key string) bool {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
func (b *MenuComponent) Params(param MenuParams) *MenuComponent {
|
||||||
|
b.Style = osui.SetDefaults(¶m.Style).(*MenuStyle)
|
||||||
|
return b
|
||||||
|
}
|
||||||
|
|
||||||
|
func Hello(name string) *HelloComponent {
|
||||||
|
return osui.NewComponent(&HelloComponent{
|
||||||
|
Name: name,
|
||||||
|
Style: osui.SetDefaults(&HelloStyle{}).(*HelloStyle),
|
||||||
|
}).(*HelloComponent)
|
||||||
|
}
|
||||||
|
```
|
||||||
+113
@@ -0,0 +1,113 @@
|
|||||||
|
package ui
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"strings"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/orus-dev/osui"
|
||||||
|
"github.com/orus-dev/osui/colors"
|
||||||
|
"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"`
|
||||||
|
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 {
|
||||||
|
Data osui.ComponentData
|
||||||
|
Style *ButtonStyle
|
||||||
|
Text string
|
||||||
|
Toggle bool
|
||||||
|
Clicked bool
|
||||||
|
on_click func(*ButtonComponent) bool
|
||||||
|
}
|
||||||
|
|
||||||
|
func (b *ButtonComponent) Render() string {
|
||||||
|
osui.UseStyle(b.Style)
|
||||||
|
|
||||||
|
if b.Clicked {
|
||||||
|
return fmt.Sprintf(" %s\n%s│%s│%s\n %s",
|
||||||
|
colors.Reset+b.Style.ClickedOutline+strings.Repeat("_", b.Data.Width)+colors.Reset+b.Data.DefaultColor,
|
||||||
|
colors.Reset+b.Style.ClickedOutline,
|
||||||
|
colors.Reset+colors.Combine(b.Style.ClickedBackground, b.Style.ClickedForeground)+centerText(b.Text, b.Data.Width)+colors.Reset+b.Style.ClickedOutline,
|
||||||
|
colors.Reset+b.Data.DefaultColor,
|
||||||
|
colors.Reset+b.Style.ClickedOutline+strings.Repeat("‾", b.Data.Width)+colors.Reset+b.Data.DefaultColor,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
if b.Data.IsActive {
|
||||||
|
return fmt.Sprintf(" %s\n%s│%s│%s\n %s",
|
||||||
|
colors.Reset+b.Style.ActiveOutline+strings.Repeat("_", b.Data.Width)+colors.Reset+b.Data.DefaultColor,
|
||||||
|
colors.Reset+b.Style.ActiveOutline,
|
||||||
|
colors.Reset+colors.Combine(b.Style.ActiveBackground, b.Style.ActiveForeground)+centerText(b.Text, b.Data.Width)+colors.Reset+b.Style.ActiveOutline,
|
||||||
|
colors.Reset+b.Data.DefaultColor,
|
||||||
|
colors.Reset+b.Style.ActiveOutline+strings.Repeat("‾", b.Data.Width)+colors.Reset+b.Data.DefaultColor,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
return fmt.Sprintf(" %s\n%s│%s│%s\n %s",
|
||||||
|
colors.Reset+b.Style.Outline+strings.Repeat("_", b.Data.Width)+colors.Reset+b.Data.DefaultColor,
|
||||||
|
colors.Reset+b.Style.Outline,
|
||||||
|
colors.Reset+colors.Combine(b.Style.Background, b.Style.Foreground)+centerText(b.Text, b.Data.Width)+colors.Reset+b.Style.Outline,
|
||||||
|
colors.Reset+b.Data.DefaultColor,
|
||||||
|
colors.Reset+b.Style.Outline+strings.Repeat("‾", b.Data.Width)+colors.Reset+b.Data.DefaultColor,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (b *ButtonComponent) Update(key string) bool {
|
||||||
|
if isKey.Enter(key) {
|
||||||
|
if !b.on_click(b) {
|
||||||
|
if b.Toggle {
|
||||||
|
b.Clicked = !b.Clicked
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
b.Clicked = true
|
||||||
|
b.Data.Screen.Render()
|
||||||
|
time.Sleep(time.Millisecond * 120)
|
||||||
|
b.Clicked = false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
func (b *ButtonComponent) GetComponentData() *osui.ComponentData {
|
||||||
|
return &b.Data
|
||||||
|
}
|
||||||
|
|
||||||
|
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,
|
||||||
|
},
|
||||||
|
on_click: func(bc *ButtonComponent) bool { return false },
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -4,58 +4,81 @@ import (
|
|||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/orus-dev/osui"
|
"github.com/orus-dev/osui"
|
||||||
|
"github.com/orus-dev/osui/colors"
|
||||||
|
"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"`
|
||||||
|
Outline string `default:"" type:"fg"`
|
||||||
|
}
|
||||||
|
|
||||||
type DivComponent struct {
|
type DivComponent struct {
|
||||||
Data osui.ComponentData
|
Data osui.ComponentData
|
||||||
|
Style *DivStyle
|
||||||
Components []osui.Component
|
Components []osui.Component
|
||||||
ActiveComponent int
|
ActiveComponent int
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *DivComponent) GetComponentData() *osui.ComponentData {
|
func (d *DivComponent) GetComponentData() *osui.ComponentData {
|
||||||
return &p.Data
|
return &d.Data
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p DivComponent) Render() string {
|
func (d *DivComponent) Render() string {
|
||||||
frame := osui.NewFrame(p.Data.Width, p.Data.Height)
|
osui.UseStyle(d.Style)
|
||||||
for _, c := range p.Components {
|
frame := osui.NewFrame(d.Data.Width, d.Data.Height)
|
||||||
|
for i, c := range d.Components {
|
||||||
data := c.GetComponentData()
|
data := c.GetComponentData()
|
||||||
data.Width = p.Data.Width
|
if i == d.ActiveComponent {
|
||||||
data.Height = p.Data.Height
|
data.IsActive = d.Data.IsActive
|
||||||
data.Screen = p.Data.Screen
|
} 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.Background, d.Style.Foreground)
|
||||||
|
data.Screen = d.Data.Screen
|
||||||
osui.RenderOnFrame(c, &frame)
|
osui.RenderOnFrame(c, &frame)
|
||||||
}
|
}
|
||||||
|
if d.Style.Outline == "" {
|
||||||
|
for i, f := range frame {
|
||||||
|
frame[i] = colors.Combine(d.Style.Foreground, d.Style.Background) + f + colors.Reset
|
||||||
|
}
|
||||||
return strings.Join(frame, "\n")
|
return strings.Join(frame, "\n")
|
||||||
|
} else {
|
||||||
|
for i, f := range frame {
|
||||||
|
frame[i] = d.Style.Outline + "│" + colors.Combine(d.Style.Foreground, d.Style.Background) + f + colors.Reset + d.Style.Outline + "│" + colors.Reset
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return " " + d.Style.Outline + strings.Repeat("_", d.Data.Width) + colors.Reset + "\n" + strings.Join(frame, "\n") + "\n " + d.Style.Outline + strings.Repeat("‾", d.Data.Width) + colors.Reset
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *DivComponent) Run() {
|
func (d *DivComponent) Update(key string) bool {
|
||||||
for _, c := range p.Components {
|
if isKey.CtrlW(key) {
|
||||||
d := c.GetComponentData()
|
d.updateActive(findClosestComponent(d.Components, d.ActiveComponent, "up"))
|
||||||
d.Screen = p.Data.Screen
|
} else if isKey.CtrlS(key) {
|
||||||
}
|
d.updateActive(findClosestComponent(d.Components, d.ActiveComponent, "down"))
|
||||||
for {
|
} else if isKey.CtrlA(key) {
|
||||||
key, _ := osui.ReadKey()
|
d.updateActive(findClosestComponent(d.Components, d.ActiveComponent, "left"))
|
||||||
if p.Update(key) {
|
} else if isKey.CtrlD(key) {
|
||||||
return
|
d.updateActive(findClosestComponent(d.Components, d.ActiveComponent, "right"))
|
||||||
}
|
} else {
|
||||||
}
|
if len(d.Components) > 0 {
|
||||||
}
|
d.Components[d.ActiveComponent].GetComponentData().IsActive = d.Data.IsActive
|
||||||
|
if d.Components[d.ActiveComponent].Update(key) {
|
||||||
func (p *DivComponent) Update(key string) bool {
|
if d.ActiveComponent < len(d.Components)-1 {
|
||||||
switch key {
|
d.updateActive(d.ActiveComponent + 1)
|
||||||
|
|
||||||
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 {
|
} else {
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
@@ -65,17 +88,21 @@ func (p *DivComponent) Update(key string) bool {
|
|||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *DivComponent) updateActive(newIndex int) {
|
func (b *DivComponent) Params(param DivParams) *DivComponent {
|
||||||
if newIndex >= 0 && newIndex < len(p.Components) && len(p.Components) > 0 {
|
b.Style = osui.SetDefaults(¶m.Style).(*DivStyle)
|
||||||
p.Components[p.ActiveComponent].GetComponentData().IsActive = false
|
b.Data.Width = osui.LogicValueInt(param.Width == 0, 20, param.Width)
|
||||||
p.ActiveComponent = newIndex
|
return b
|
||||||
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 {
|
func Div(components ...osui.Component) *DivComponent {
|
||||||
return &DivComponent{
|
return &DivComponent{
|
||||||
Components: components,
|
Components: components,
|
||||||
|
Style: osui.SetDefaults(&DivStyle{}).(*DivStyle),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+52
-33
@@ -5,11 +5,28 @@ import (
|
|||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/orus-dev/osui"
|
"github.com/orus-dev/osui"
|
||||||
|
"github.com/orus-dev/osui/colors"
|
||||||
|
"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"`
|
||||||
|
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
|
||||||
InputData string
|
InputData string
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -18,50 +35,45 @@ func (s *InputBoxComponent) GetComponentData() *osui.ComponentData {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (s InputBoxComponent) Render() string {
|
func (s InputBoxComponent) Render() string {
|
||||||
|
osui.UseStyle(s.Style)
|
||||||
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|\n %s",
|
" %s\n%s│%s%s│%s\n %s",
|
||||||
strings.Repeat("_", int(s.max_size)),
|
colors.Reset+s.Style.Outline+strings.Repeat("_", int(s.max_size))+colors.Reset,
|
||||||
s.InputData+osui.LogicValue(s.Data.IsActive, "█", ""),
|
colors.Reset+s.Style.Outline,
|
||||||
strings.Repeat(" ", int(s.max_size)-len(s.InputData)-osui.LogicValueInt(s.Data.IsActive, 1, 0)),
|
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)),
|
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(
|
|
||||||
" %s\n|%s\n %s",
|
|
||||||
strings.Repeat("_", int(s.max_size)),
|
|
||||||
s.InputData+osui.LogicValue(s.Data.IsActive, "█", "|"),
|
|
||||||
strings.Repeat("‾", int(s.max_size)),
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (s *InputBoxComponent) Read() error {
|
return fmt.Sprintf(
|
||||||
for {
|
" %s\n%s│%s%s\n %s",
|
||||||
key, err := osui.ReadKey()
|
colors.Reset+s.Style.Outline+strings.Repeat("_", int(s.max_size))+colors.Reset,
|
||||||
if err != nil {
|
colors.Reset+s.Style.Outline,
|
||||||
fmt.Print("\n\n")
|
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),
|
||||||
fmt.Println(err)
|
colors.Reset+s.Data.DefaultColor,
|
||||||
break
|
colors.Reset+s.Style.Outline+strings.Repeat("‾", int(s.max_size))+colors.Reset+s.Data.DefaultColor,
|
||||||
}
|
)
|
||||||
if s.Update(key) {
|
|
||||||
fmt.Print("\n\n")
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *InputBoxComponent) Update(key string) bool {
|
func (s *InputBoxComponent) Update(key string) bool {
|
||||||
switch key {
|
if isKey.Enter(key) {
|
||||||
case "":
|
|
||||||
case osui.Key.Enter:
|
|
||||||
return true
|
return true
|
||||||
case osui.Key.Backspace:
|
} else if isKey.Backspace(key) {
|
||||||
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]
|
||||||
}
|
}
|
||||||
|
} else if isKey.Left(key) {
|
||||||
default:
|
if s.cursor > 0 {
|
||||||
|
s.cursor--
|
||||||
|
}
|
||||||
|
} else if isKey.Right(key) {
|
||||||
|
if s.cursor < s.max_size {
|
||||||
|
s.cursor++
|
||||||
|
}
|
||||||
|
} else {
|
||||||
if len(key) == 1 {
|
if len(key) == 1 {
|
||||||
if int(s.max_size) > len(s.InputData) {
|
if int(s.max_size) > len(s.InputData) {
|
||||||
s.InputData += key
|
s.InputData += key
|
||||||
@@ -71,6 +83,13 @@ func (s *InputBoxComponent) Update(key string) bool {
|
|||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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 {
|
func InputBox(max_size uint) *InputBoxComponent {
|
||||||
return &InputBoxComponent{max_size: max_size}
|
return &InputBoxComponent{max_size: max_size, Style: osui.SetDefaults(&InputBoxStyle{}).(*InputBoxStyle)}
|
||||||
}
|
}
|
||||||
|
|||||||
+92
@@ -0,0 +1,92 @@
|
|||||||
|
package ui
|
||||||
|
|
||||||
|
import (
|
||||||
|
"strings"
|
||||||
|
|
||||||
|
"github.com/orus-dev/osui"
|
||||||
|
"github.com/orus-dev/osui/colors"
|
||||||
|
"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 {
|
||||||
|
Data osui.ComponentData
|
||||||
|
Style *MenuStyle
|
||||||
|
Items []string
|
||||||
|
SelectedItem int
|
||||||
|
onSelected func(*MenuComponent, bool)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *MenuComponent) GetComponentData() *osui.ComponentData {
|
||||||
|
return &m.Data
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *MenuComponent) Render() string {
|
||||||
|
osui.UseStyle(m.Style)
|
||||||
|
res := []string{}
|
||||||
|
|
||||||
|
cursor := m.Style.Cursor
|
||||||
|
if !m.Data.IsActive {
|
||||||
|
cursor = m.Style.CursorInactive
|
||||||
|
}
|
||||||
|
d, _ := osui.CompressString(m.Style.Cursor, "")
|
||||||
|
empty := strings.Repeat(" ", len(d))
|
||||||
|
|
||||||
|
for i, item := range m.Items {
|
||||||
|
if i == m.SelectedItem {
|
||||||
|
res = append(res, cursor+colors.Combine(m.Style.SelectedFg, m.Style.SelectedBg)+item+colors.Reset+m.Data.DefaultColor)
|
||||||
|
} else {
|
||||||
|
res = append(res, empty+colors.Combine(m.Style.Fg, m.Style.Bg)+item+colors.Reset+m.Data.DefaultColor)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return strings.Join(res, "\n")
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *MenuComponent) Update(key string) bool {
|
||||||
|
if isKey.Char(key, "s") {
|
||||||
|
if m.SelectedItem+1 < len(m.Items) {
|
||||||
|
m.SelectedItem++
|
||||||
|
} else {
|
||||||
|
m.SelectedItem = 0
|
||||||
|
}
|
||||||
|
} else if isKey.Char(key, "w") {
|
||||||
|
if m.SelectedItem > 0 {
|
||||||
|
m.SelectedItem--
|
||||||
|
} else {
|
||||||
|
m.SelectedItem = len(m.Items) - 1
|
||||||
|
}
|
||||||
|
} else if isKey.Enter(key) {
|
||||||
|
m.onSelected(m, true)
|
||||||
|
return true
|
||||||
|
} else if isKey.Char(key, "q") {
|
||||||
|
m.onSelected(m, false)
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
func (b *MenuComponent) Params(param MenuParams) *MenuComponent {
|
||||||
|
b.Style = osui.SetDefaults(¶m.Style).(*MenuStyle)
|
||||||
|
return b
|
||||||
|
}
|
||||||
|
|
||||||
|
func Menu(items ...string) *MenuComponent {
|
||||||
|
return &MenuComponent{
|
||||||
|
Items: items,
|
||||||
|
Style: osui.SetDefaults(&MenuStyle{}).(*MenuStyle),
|
||||||
|
}
|
||||||
|
}
|
||||||
+42
-24
@@ -5,11 +5,19 @@ import (
|
|||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/orus-dev/osui"
|
"github.com/orus-dev/osui"
|
||||||
"github.com/orus-dev/osui/styles"
|
"github.com/orus-dev/osui/colors"
|
||||||
|
"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:""`
|
||||||
}
|
}
|
||||||
|
|
||||||
type PaginatorComponent struct {
|
type PaginatorComponent struct {
|
||||||
@@ -23,46 +31,53 @@ func (p *PaginatorComponent) GetComponentData() *osui.ComponentData {
|
|||||||
return &p.Data
|
return &p.Data
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p PaginatorComponent) Render() string {
|
func (p *PaginatorComponent) Render() string {
|
||||||
pgs := strings.Repeat(" ", p.Data.Width/2)
|
width, _ := osui.GetTerminalSize()
|
||||||
for page, c := range p.Components {
|
pgs := strings.Repeat(" ", (width-len(p.Components))/2)
|
||||||
if page == p.ActiveComponent {
|
osui.UseStyle(p.Style)
|
||||||
pgs += p.Style.Active + "•" + styles.Reset
|
frame := osui.NewFrame(p.Data.Width, p.Data.Height)
|
||||||
|
for i, c := range p.Components {
|
||||||
|
data := c.GetComponentData()
|
||||||
|
if i == p.ActiveComponent {
|
||||||
|
pgs += p.Style.Active + "•" + colors.Reset
|
||||||
|
data.IsActive = true
|
||||||
} else {
|
} else {
|
||||||
pgs += styles.Reset + "•"
|
pgs += colors.Reset + p.Style.Inactive + "•"
|
||||||
|
data.IsActive = false
|
||||||
}
|
}
|
||||||
d := c.GetComponentData()
|
if data.Width == 0 {
|
||||||
if d.Screen == nil {
|
data.Width = p.Data.Width
|
||||||
d.Screen = p.Data.Screen
|
|
||||||
}
|
}
|
||||||
d.Height = p.Data.Height - 1
|
if data.Height == 0 {
|
||||||
d.Width = p.Data.Width
|
data.Height = p.Data.Height
|
||||||
|
}
|
||||||
|
data.DefaultColor = p.Data.DefaultColor
|
||||||
|
data.Screen = p.Data.Screen
|
||||||
}
|
}
|
||||||
frame := osui.NewFrame(p.Data.Width, p.Data.Height-3)
|
|
||||||
osui.RenderOnFrame(p.Components[p.ActiveComponent], &frame)
|
osui.RenderOnFrame(p.Components[p.ActiveComponent], &frame)
|
||||||
return fmt.Sprintf("%s\n%s", pgs, styles.Reset+strings.Join(frame, "\n"))
|
for i, f := range frame {
|
||||||
|
frame[i] = colors.Reset + p.Data.DefaultColor + f + colors.Reset
|
||||||
|
}
|
||||||
|
return fmt.Sprintf("%s\n%s", pgs, colors.Reset+strings.Join(frame, "\n"))
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *PaginatorComponent) Update(key string) bool {
|
func (p *PaginatorComponent) Update(key string) bool {
|
||||||
switch key {
|
if key == "\x1b[Z" {
|
||||||
case "":
|
|
||||||
p.updateActive(p.ActiveComponent)
|
|
||||||
case "\x1b[Z":
|
|
||||||
if p.ActiveComponent > 0 {
|
if p.ActiveComponent > 0 {
|
||||||
p.updateActive(p.ActiveComponent - 1)
|
p.updateActive(p.ActiveComponent - 1)
|
||||||
} else {
|
} else {
|
||||||
p.updateActive(len(p.Components) - 1)
|
p.updateActive(len(p.Components) - 1)
|
||||||
}
|
}
|
||||||
case osui.Key.Tab:
|
} else if isKey.Tab(key) {
|
||||||
if p.ActiveComponent < len(p.Components)-1 {
|
if p.ActiveComponent < len(p.Components)-1 {
|
||||||
p.updateActive(p.ActiveComponent + 1)
|
p.updateActive(p.ActiveComponent + 1)
|
||||||
} else {
|
} else {
|
||||||
p.updateActive(0)
|
p.updateActive(0)
|
||||||
}
|
}
|
||||||
case osui.Key.Escape:
|
} else if isKey.Escape(key) {
|
||||||
fmt.Print("\n\n")
|
fmt.Print("\n\n")
|
||||||
return true
|
return true
|
||||||
default:
|
} else {
|
||||||
if len(p.Components) > 0 {
|
if len(p.Components) > 0 {
|
||||||
p.Components[p.ActiveComponent].GetComponentData().IsActive = p.Data.IsActive
|
p.Components[p.ActiveComponent].GetComponentData().IsActive = p.Data.IsActive
|
||||||
if p.Components[p.ActiveComponent].Update(key) {
|
if p.Components[p.ActiveComponent].Update(key) {
|
||||||
@@ -79,13 +94,16 @@ func (p *PaginatorComponent) Update(key string) bool {
|
|||||||
|
|
||||||
func (p *PaginatorComponent) updateActive(newIndex int) {
|
func (p *PaginatorComponent) updateActive(newIndex int) {
|
||||||
if newIndex >= 0 && newIndex < len(p.Components) && len(p.Components) > 0 {
|
if newIndex >= 0 && newIndex < len(p.Components) && len(p.Components) > 0 {
|
||||||
p.Components[p.ActiveComponent].GetComponentData().IsActive = false
|
|
||||||
p.ActiveComponent = newIndex
|
p.ActiveComponent = newIndex
|
||||||
p.Components[p.ActiveComponent].GetComponentData().IsActive = p.Data.IsActive
|
|
||||||
p.Components[p.ActiveComponent].Update("")
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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 {
|
func Paginator(pages ...osui.Component) *PaginatorComponent {
|
||||||
return &PaginatorComponent{
|
return &PaginatorComponent{
|
||||||
Components: pages,
|
Components: pages,
|
||||||
|
|||||||
+83
@@ -0,0 +1,83 @@
|
|||||||
|
package ui
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"math"
|
||||||
|
"strings"
|
||||||
|
|
||||||
|
"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]
|
||||||
|
}
|
||||||
|
padding := (width - len(text)) / 2
|
||||||
|
leftPadding := strings.Repeat(" ", padding)
|
||||||
|
rightPadding := strings.Repeat(" ", padding)
|
||||||
|
|
||||||
|
return fmt.Sprintf("%s%s%s", leftPadding, text, rightPadding)
|
||||||
|
}
|
||||||
|
|
||||||
|
func findClosestComponent(a []osui.Component, i int, d string) int {
|
||||||
|
if len(a) == 0 || i < 0 || i >= len(a) {
|
||||||
|
return -1 // Invalid input
|
||||||
|
}
|
||||||
|
|
||||||
|
current := a[i].GetComponentData()
|
||||||
|
closestIndex := -1
|
||||||
|
minDistance := math.MaxFloat64
|
||||||
|
|
||||||
|
for j, c := range a {
|
||||||
|
component := c.GetComponentData()
|
||||||
|
if i == j {
|
||||||
|
continue // Skip the current component
|
||||||
|
}
|
||||||
|
|
||||||
|
// Check if the component is in the correct direction
|
||||||
|
isValidDirection := false
|
||||||
|
switch d {
|
||||||
|
case "up":
|
||||||
|
if component.Y < current.Y {
|
||||||
|
isValidDirection = true
|
||||||
|
}
|
||||||
|
case "down":
|
||||||
|
if component.Y > current.Y {
|
||||||
|
isValidDirection = true
|
||||||
|
}
|
||||||
|
case "left":
|
||||||
|
if component.X < current.X {
|
||||||
|
isValidDirection = true
|
||||||
|
}
|
||||||
|
case "right":
|
||||||
|
if component.X > current.X {
|
||||||
|
isValidDirection = true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// If the direction is valid, calculate distance
|
||||||
|
if isValidDirection {
|
||||||
|
distance := math.Hypot(float64(component.X-current.X), float64(component.Y-current.Y))
|
||||||
|
if distance < minDistance {
|
||||||
|
minDistance = distance
|
||||||
|
closestIndex = j
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return closestIndex
|
||||||
|
}
|
||||||
@@ -9,24 +9,31 @@ import (
|
|||||||
"runtime"
|
"runtime"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
|
"github.com/nathan-fiscaletti/consolesize-go"
|
||||||
|
"github.com/orus-dev/osui/colors"
|
||||||
"golang.org/x/term"
|
"golang.org/x/term"
|
||||||
)
|
)
|
||||||
|
|
||||||
func renderLine(frameLine, line string, x int) string {
|
func renderLine(frameLine, line_ string, x int) string {
|
||||||
result := ""
|
frame := []rune(frameLine)
|
||||||
if x >= len(frameLine) {
|
line := []rune(line_)
|
||||||
return result
|
|
||||||
|
i := 0
|
||||||
|
v := 0
|
||||||
|
for j, c := range frame {
|
||||||
|
if i+j >= x && v < len(line) {
|
||||||
|
if c != '\b' && line[v] == '\t' {
|
||||||
|
frame = append(frame[:i+j], append([]rune{' '}, frame[i+j:]...)...)
|
||||||
|
}
|
||||||
|
frame[i+j] = line[v]
|
||||||
|
v++
|
||||||
|
}
|
||||||
|
if c == '\b' {
|
||||||
|
i++
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
lChars := []rune(line)
|
return string(frame)
|
||||||
for i, c := range frameLine {
|
|
||||||
if i >= x && len(lChars) > i-x {
|
|
||||||
result += string(lChars[i-x])
|
|
||||||
} else {
|
|
||||||
result += string(c)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return result
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func RenderOnFrame(c Component, frame *[]string) {
|
func RenderOnFrame(c Component, frame *[]string) {
|
||||||
@@ -43,7 +50,7 @@ func RenderOnFrame(c Component, frame *[]string) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func CompressString(input, repl string) (string, []string) {
|
func CompressString(input, repl string) (string, []string) {
|
||||||
pattern := `\x1b\[([0-9;]*)m`
|
pattern := `\x1b\[([0-9;]*)[a-zA-Z]`
|
||||||
re := regexp.MustCompile(pattern)
|
re := regexp.MustCompile(pattern)
|
||||||
matches := re.FindAllString(input, -1)
|
matches := re.FindAllString(input, -1)
|
||||||
return re.ReplaceAllString(input, repl), matches
|
return re.ReplaceAllString(input, repl), matches
|
||||||
@@ -99,11 +106,11 @@ func Clear() {
|
|||||||
func SetDefaults(p interface{}) interface{} {
|
func SetDefaults(p interface{}) interface{} {
|
||||||
v := reflect.ValueOf(p)
|
v := reflect.ValueOf(p)
|
||||||
if v.Kind() != reflect.Ptr {
|
if v.Kind() != reflect.Ptr {
|
||||||
panic("setDefaults: expected a pointer to a struct")
|
panic("SetDefaults: expected a pointer to a struct")
|
||||||
}
|
}
|
||||||
val := v.Elem()
|
val := v.Elem()
|
||||||
if val.Kind() != reflect.Struct {
|
if val.Kind() != reflect.Struct {
|
||||||
panic("setDefaults: expected a pointer to a struct")
|
panic("SetDefaults: expected a pointer to a struct")
|
||||||
}
|
}
|
||||||
typ := val.Type()
|
typ := val.Type()
|
||||||
for i := 0; i < val.NumField(); i++ {
|
for i := 0; i < val.NumField(); i++ {
|
||||||
@@ -118,6 +125,33 @@ func SetDefaults(p interface{}) interface{} {
|
|||||||
return p
|
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")
|
||||||
}
|
}
|
||||||
@@ -140,26 +174,8 @@ func LogicValueInt(b bool, _if, _else int) int {
|
|||||||
return _else
|
return _else
|
||||||
}
|
}
|
||||||
|
|
||||||
type Key_ = string
|
|
||||||
|
|
||||||
type key struct {
|
// Get the terminal size
|
||||||
Enter Key_
|
func GetTerminalSize() (int, int) {
|
||||||
Tab Key_
|
return consolesize.GetConsoleSize()
|
||||||
Backspace Key_
|
|
||||||
Escape Key_
|
|
||||||
Up Key_
|
|
||||||
Down Key_
|
|
||||||
Right Key_
|
|
||||||
Left Key_
|
|
||||||
}
|
|
||||||
|
|
||||||
var Key = key{
|
|
||||||
Enter: "\r",
|
|
||||||
Tab: "\t",
|
|
||||||
Backspace: "\x7f",
|
|
||||||
Escape: "\x1b",
|
|
||||||
Up: "\x1b[A",
|
|
||||||
Down: "\x1b[B",
|
|
||||||
Right: "\x1b[C",
|
|
||||||
Left: "\x1b[D",
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user