Compare commits
34
Commits
v0.0.1-alpha4
...
master
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
553804ec51 | ||
|
|
905bb6711e | ||
|
|
8ea250c1b1 | ||
|
|
039bc27999 | ||
|
|
d4f6c4104d | ||
|
|
0f052d7652 | ||
|
|
f2f7e51750 | ||
|
|
7e714abddf | ||
|
|
3ceaa13b3f | ||
|
|
8165e80b05 | ||
|
|
f77c04c908 | ||
|
|
27530fe8cc | ||
|
|
705bd8d61b | ||
|
|
bf70ceacf1 | ||
|
|
6cdb685eb5 | ||
|
|
cdb8576cc4 | ||
|
|
b2b5c9c00d | ||
|
|
dd8f1fbc1f | ||
|
|
e873c01e50 | ||
|
|
3bab6d3cd6 | ||
|
|
06f9e4577c | ||
|
|
716d1d4a08 | ||
|
|
8592b1cc50 | ||
|
|
90f106f127 | ||
|
|
5a94fc34ad | ||
|
|
fdbb54a557 | ||
|
|
5024087358 | ||
|
|
cf93b3383f | ||
|
|
3b2ff24c1f | ||
|
|
4214f96c61 | ||
|
|
3b3e92fae5 | ||
|
|
179de20dba | ||
|
|
8f375efe41 | ||
|
|
905a77f8b0 |
@@ -23,3 +23,6 @@ go.work.sum
|
||||
|
||||
# env file
|
||||
.env
|
||||
|
||||
# macOS
|
||||
.DS_Store
|
||||
@@ -1,18 +1,30 @@
|
||||
# osui
|
||||
A Component-based TUI library written in go!
|
||||
<div style="text-align: center;">
|
||||
<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
|
||||
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() {
|
||||
components := osui.NewRender()
|
||||
components.Add(ui.Text("Hello, World!"))
|
||||
components.Render()
|
||||
app := ui.Div(
|
||||
ui.Text("Hello, World!")
|
||||
)
|
||||
|
||||
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,59 @@
|
||||
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,126 @@
|
||||
package osui
|
||||
|
||||
import (
|
||||
"github.com/orus-dev/osui/colors"
|
||||
)
|
||||
|
||||
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"`
|
||||
CursorColor string `json:"cursorColor"`
|
||||
InactiveCursor string `json:"inactiveCursor"`
|
||||
}
|
||||
|
||||
func (s *Style) SetDefaults() {
|
||||
defaultValue(&s.Cursor, "> ")
|
||||
defaultValue(&s.SelectedForeground, colors.Black)
|
||||
defaultValue(&s.SelectedBackground, colors.Magenta)
|
||||
defaultValue(&s.ActiveOutline, colors.Blue)
|
||||
defaultValue(&s.ClickedForeground, colors.Blue)
|
||||
defaultValue(&s.ClickedOutline, colors.Blue)
|
||||
s.UseStyle()
|
||||
}
|
||||
|
||||
func (s *Style) UseStyle() {
|
||||
s.Foreground = colors.AsFg(s.Foreground)
|
||||
s.Background = colors.AsBg(s.Background)
|
||||
s.Outline = colors.AsFg(s.Outline)
|
||||
|
||||
s.ActiveForeground = colors.AsFg(s.ActiveForeground)
|
||||
s.ActiveBackground = colors.AsBg(s.ActiveBackground)
|
||||
s.ActiveOutline = colors.AsFg(s.ActiveOutline)
|
||||
|
||||
s.ClickedForeground = colors.AsFg(s.ClickedForeground)
|
||||
s.ClickedBackground = colors.AsBg(s.ClickedBackground)
|
||||
s.ClickedOutline = colors.AsFg(s.ClickedOutline)
|
||||
|
||||
s.SelectedForeground = colors.AsFg(s.SelectedForeground)
|
||||
s.SelectedBackground = colors.AsBg(s.SelectedBackground)
|
||||
s.SelectedOutline = colors.AsFg(s.SelectedOutline)
|
||||
}
|
||||
|
||||
func defaultValue(sd *string, s string) {
|
||||
if *sd == "" {
|
||||
*sd = s
|
||||
}
|
||||
}
|
||||
|
||||
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()
|
||||
Keys map[string]string
|
||||
Toggle bool `json:"useToggle"`
|
||||
}
|
||||
|
||||
func (p *Param) SetDefaultBindings(keys map[string]string) {
|
||||
p.Style.SetDefaults()
|
||||
if p.Keys == nil {
|
||||
p.Keys = keys
|
||||
return
|
||||
}
|
||||
for k, f := range keys {
|
||||
if _, ok := p.Keys[k]; !ok {
|
||||
p.Keys[k] = f
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
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() {}
|
||||
}
|
||||
|
||||
data.Toggle = p.Toggle
|
||||
|
||||
data.Keys = p.Keys
|
||||
|
||||
return c
|
||||
}
|
||||
|
||||
type ComponentData struct {
|
||||
X int
|
||||
Y int
|
||||
Width int
|
||||
Height int
|
||||
DefaultColor string
|
||||
IsActive bool
|
||||
Style Style
|
||||
OnClick func()
|
||||
Keys map[string]string
|
||||
Toggle bool
|
||||
Screen *Screen
|
||||
}
|
||||
@@ -0,0 +1,46 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"github.com/orus-dev/osui"
|
||||
"github.com/orus-dev/osui/ui"
|
||||
)
|
||||
|
||||
func main() {
|
||||
// screen := osui.NewScreen()
|
||||
// my_btn := ui.Button(osui.Param{},
|
||||
// "Button",
|
||||
// )
|
||||
// screen.Run(my_btn)
|
||||
|
||||
// var menu ui.Id[*ui.MenuComponent]
|
||||
// screen := osui.NewScreen()
|
||||
// screen.Run(ui.Div(osui.Param{},
|
||||
|
||||
// menu.Id(ui.Menu(osui.Param{OnClick: func() {
|
||||
// screen.SetComponent(ui.Text(osui.Param{}, menu.Component.Items[menu.Component.SelectedItem]))
|
||||
// screen.Exit()
|
||||
// }},
|
||||
// "Item 1", "Item 24324322", "Item 366",
|
||||
// )),
|
||||
|
||||
// ))
|
||||
|
||||
// screen := osui.NewScreen()
|
||||
// my_btn := ui.InputBox(osui.Param{},
|
||||
// 24,
|
||||
// )
|
||||
// screen.Run(my_btn)
|
||||
|
||||
screen := osui.NewScreen()
|
||||
|
||||
my_btn := ui.Paginator(osui.Param{OnClick: func() {screen.Exit()}},
|
||||
ui.Div(osui.Param{},
|
||||
ui.Text(osui.Param{}, "Page 1"),
|
||||
),
|
||||
ui.Div(osui.Param{},
|
||||
ui.Text(osui.Param{}, "Page 2"),
|
||||
),
|
||||
)
|
||||
|
||||
screen.Run(my_btn)
|
||||
}
|
||||
@@ -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
|
||||
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=
|
||||
|
||||
+55
@@ -0,0 +1,55 @@
|
||||
package keys
|
||||
|
||||
const (
|
||||
Enter = "Enter"
|
||||
Tab = "Tab"
|
||||
ShiftTab = "ShiftTab"
|
||||
Backspace = "Backspace"
|
||||
Escape = "Escape"
|
||||
Up = "Up"
|
||||
Down = "Down"
|
||||
Left = "Left"
|
||||
Right = "Right"
|
||||
CtrlW = "CtrlW"
|
||||
CtrlS = "CtrlS"
|
||||
CtrlA = "CtrlA"
|
||||
CtrlD = "CtrlD"
|
||||
Error = "Error"
|
||||
)
|
||||
|
||||
func Char(key string) string {
|
||||
return "Char(" + key + ")"
|
||||
}
|
||||
|
||||
func GetKeyName(key string) string {
|
||||
switch key {
|
||||
case "\r", "\n":
|
||||
return Enter
|
||||
case "\x7f", "\b":
|
||||
return Backspace
|
||||
case "\t":
|
||||
return Tab
|
||||
case "\x1b[Z":
|
||||
return ShiftTab
|
||||
case "\x1b":
|
||||
return Escape
|
||||
case "\x1b[A":
|
||||
return Up
|
||||
case "\x1b[B":
|
||||
return Down
|
||||
case "\x1b[C":
|
||||
return Right
|
||||
case "\x1b[D":
|
||||
return Left
|
||||
case "\x17":
|
||||
return CtrlW
|
||||
case "\x13":
|
||||
return CtrlS
|
||||
case "\x01":
|
||||
return CtrlA
|
||||
case "\x04":
|
||||
return CtrlD
|
||||
default:
|
||||
return Char(key)
|
||||
}
|
||||
}
|
||||
@@ -2,52 +2,106 @@ package osui
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/orus-dev/osui/colors"
|
||||
"golang.org/x/term"
|
||||
)
|
||||
|
||||
type ComponentData struct {
|
||||
X uint16
|
||||
Y uint16
|
||||
Screen *Screen
|
||||
type Key struct {
|
||||
Chars [3]rune
|
||||
Name string
|
||||
}
|
||||
|
||||
type UpdateContext struct {
|
||||
UpdateKind uint8
|
||||
Tick uint8
|
||||
Key Key
|
||||
}
|
||||
|
||||
type Component interface {
|
||||
Render() string
|
||||
GetComponentData() *ComponentData
|
||||
Update(UpdateContext) bool
|
||||
}
|
||||
|
||||
type Screen struct {
|
||||
components []Component
|
||||
Component Component
|
||||
tickRate uint8
|
||||
running bool
|
||||
}
|
||||
|
||||
func NewScreen(c ...Component) *Screen {
|
||||
s := &Screen{components: c}
|
||||
for _, component := range s.components {
|
||||
componentData := component.GetComponentData()
|
||||
if componentData.Screen == nil {
|
||||
componentData.Screen = s
|
||||
} else {
|
||||
componentData.Screen.components = s.components
|
||||
}
|
||||
}
|
||||
return s
|
||||
func NewScreen() *Screen {
|
||||
s := Screen{}
|
||||
s.tickRate = 1
|
||||
return &s
|
||||
}
|
||||
|
||||
func (s *Screen) Render() error {
|
||||
func (s *Screen) Render() {
|
||||
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)
|
||||
RenderOnFrame(s.Component, &frame)
|
||||
Clear()
|
||||
width, height, err := term.GetSize(0)
|
||||
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 {
|
||||
return fmt.Errorf("error getting the terminal size: %s", err)
|
||||
panic(err)
|
||||
}
|
||||
frame := make([]string, height)
|
||||
for i := 0; i < height; i++ {
|
||||
frame[i] = strings.Repeat(" ", width)
|
||||
defer term.Restore(int(os.Stdin.Fd()), oldState)
|
||||
s.running = true
|
||||
for s.running {
|
||||
s.Render()
|
||||
s.Component.Update(UpdateContext{UpdateKind: UpdateKindKey, Key: ReadKey()})
|
||||
}
|
||||
for _, component := range s.components {
|
||||
RenderOnFrame(component, &frame)
|
||||
defer term.Restore(int(os.Stdin.Fd()), oldState)
|
||||
}
|
||||
fmt.Print(strings.Join(frame, "\n"))
|
||||
return nil
|
||||
|
||||
func (s *Screen) _() {
|
||||
var tick uint8 = 0
|
||||
for s.running {
|
||||
s.Component.Update(UpdateContext{UpdateKind: UpdateKindTick, Tick: tick})
|
||||
if tick == 255 {
|
||||
tick = 0
|
||||
}
|
||||
time.Sleep(time.Duration(1000/int(s.tickRate)) * time.Millisecond)
|
||||
}
|
||||
}
|
||||
|
||||
func (s *Screen) SetComponent(c Component) {
|
||||
s.Component = c
|
||||
width, height := GetTerminalSize()
|
||||
data := s.Component.GetComponentData()
|
||||
if data.Height == 0 {
|
||||
data.Height = height
|
||||
}
|
||||
if data.Width == 0 {
|
||||
data.Width = width
|
||||
}
|
||||
data.Screen = s
|
||||
data.IsActive = true
|
||||
data.DefaultColor = colors.Reset
|
||||
}
|
||||
|
||||
func (s *Screen) Exit() {
|
||||
s.running = false
|
||||
ShowCursor()
|
||||
}
|
||||
|
||||
const (
|
||||
UpdateKindKey = uint8(iota)
|
||||
UpdateKindTick
|
||||
)
|
||||
|
||||
@@ -0,0 +1,86 @@
|
||||
package ui
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/orus-dev/osui"
|
||||
"github.com/orus-dev/osui/colors"
|
||||
"github.com/orus-dev/osui/keys"
|
||||
)
|
||||
|
||||
type ButtonComponent struct {
|
||||
Data osui.ComponentData
|
||||
Text string
|
||||
clicked bool
|
||||
}
|
||||
|
||||
func (b *ButtonComponent) Render() string {
|
||||
b.Data.Style.UseStyle()
|
||||
|
||||
if b.clicked {
|
||||
return fmt.Sprintf(" %s\n%s│%s│%s\n %s",
|
||||
colors.Reset+b.Data.Style.ClickedOutline+strings.Repeat("_", b.Data.Width-2)+colors.Reset+b.Data.DefaultColor,
|
||||
colors.Reset+b.Data.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.Style.ClickedOutline+strings.Repeat("‾", b.Data.Width-2)+colors.Reset+b.Data.DefaultColor,
|
||||
)
|
||||
}
|
||||
|
||||
if b.Data.IsActive {
|
||||
return fmt.Sprintf(" %s\n%s│%s│%s\n %s",
|
||||
colors.Reset+b.Data.Style.ActiveOutline+strings.Repeat("_", b.Data.Width-2)+colors.Reset+b.Data.DefaultColor,
|
||||
colors.Reset+b.Data.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.Style.ActiveOutline+strings.Repeat("‾", b.Data.Width-2)+colors.Reset+b.Data.DefaultColor,
|
||||
)
|
||||
}
|
||||
|
||||
return fmt.Sprintf(" %s\n%s│%s│%s\n %s",
|
||||
colors.Reset+b.Data.Style.Outline+strings.Repeat("_", b.Data.Width-2)+colors.Reset+b.Data.DefaultColor,
|
||||
colors.Reset+b.Data.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.Style.Outline+strings.Repeat("‾", b.Data.Width-2)+colors.Reset+b.Data.DefaultColor,
|
||||
)
|
||||
}
|
||||
|
||||
func (b *ButtonComponent) Update(ctx osui.UpdateContext) bool {
|
||||
if ctx.UpdateKind == osui.UpdateKindKey {
|
||||
switch b.Data.Keys[ctx.Key.Name] {
|
||||
case "click":
|
||||
b.Data.OnClick()
|
||||
if b.Data.Toggle {
|
||||
b.clicked = !b.clicked
|
||||
return false
|
||||
}
|
||||
b.clicked = true
|
||||
b.Data.Screen.Render()
|
||||
time.Sleep(time.Millisecond * 120)
|
||||
b.clicked = false
|
||||
}
|
||||
} else if ctx.UpdateKind == osui.UpdateKindTick {
|
||||
}
|
||||
|
||||
return false
|
||||
}
|
||||
|
||||
func (b *ButtonComponent) GetComponentData() *osui.ComponentData {
|
||||
return &b.Data
|
||||
}
|
||||
|
||||
func Button(param osui.Param, text string) *ButtonComponent {
|
||||
param.SetDefaultBindings(map[string]string{
|
||||
keys.Enter: "click",
|
||||
})
|
||||
return param.UseParam(&ButtonComponent{Text: text,
|
||||
Data: osui.ComponentData{
|
||||
Width: 20,
|
||||
Height: 1,
|
||||
},
|
||||
clicked: false,
|
||||
}).(*ButtonComponent)
|
||||
}
|
||||
@@ -1,70 +0,0 @@
|
||||
package ui
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"github.com/orus-dev/osui"
|
||||
)
|
||||
|
||||
type TextComponent struct {
|
||||
Data osui.ComponentData
|
||||
Text string
|
||||
}
|
||||
|
||||
func (t *TextComponent) GetComponentData() *osui.ComponentData {
|
||||
return &t.Data
|
||||
}
|
||||
|
||||
func (t TextComponent) Render() string {
|
||||
return t.Text
|
||||
}
|
||||
|
||||
type InputBoxComponent struct {
|
||||
Data osui.ComponentData
|
||||
max_size uint
|
||||
InputData string
|
||||
}
|
||||
|
||||
func (s *InputBoxComponent) GetComponentData() *osui.ComponentData {
|
||||
return &s.Data
|
||||
}
|
||||
|
||||
func (s InputBoxComponent) Render() string {
|
||||
return fmt.Sprintf(
|
||||
" %s\n|%s%s|\n %s",
|
||||
strings.Repeat("_", int(s.max_size)),
|
||||
s.InputData,
|
||||
strings.Repeat(" ", int(s.max_size)-len(s.InputData)),
|
||||
strings.Repeat("‾", int(s.max_size)),
|
||||
)
|
||||
}
|
||||
|
||||
func (s *InputBoxComponent) Read() error {
|
||||
for {
|
||||
fmt.Printf("\x1B[%d;%dH", s.Data.Y+2, int(s.Data.X+2)+len(s.InputData))
|
||||
key, err := osui.ReadKey()
|
||||
if err != nil {
|
||||
fmt.Print("\n\n")
|
||||
fmt.Println(err)
|
||||
break
|
||||
}
|
||||
switch key {
|
||||
case osui.Key.Enter:
|
||||
fmt.Print("\n\n")
|
||||
return nil
|
||||
|
||||
case osui.Key.Backspace:
|
||||
if len(s.InputData) > 0 {
|
||||
s.InputData = s.InputData[:len(s.InputData)-1]
|
||||
}
|
||||
|
||||
default:
|
||||
if int(s.max_size) > len(s.InputData) {
|
||||
s.InputData += key
|
||||
}
|
||||
}
|
||||
s.Data.Screen.Render()
|
||||
}
|
||||
return nil
|
||||
}
|
||||
@@ -0,0 +1,97 @@
|
||||
package ui
|
||||
|
||||
import (
|
||||
"strings"
|
||||
|
||||
"github.com/orus-dev/osui"
|
||||
"github.com/orus-dev/osui/colors"
|
||||
"github.com/orus-dev/osui/keys"
|
||||
)
|
||||
|
||||
type DivComponent struct {
|
||||
Data osui.ComponentData
|
||||
Components []osui.Component
|
||||
ActiveComponent int
|
||||
}
|
||||
|
||||
func (d *DivComponent) GetComponentData() *osui.ComponentData {
|
||||
return &d.Data
|
||||
}
|
||||
|
||||
func (d *DivComponent) Render() string {
|
||||
d.Data.Style.UseStyle()
|
||||
frame := osui.NewFrame(d.Data.Width-2, d.Data.Height-2)
|
||||
for i, c := range d.Components {
|
||||
data := c.GetComponentData()
|
||||
if i == d.ActiveComponent {
|
||||
data.IsActive = d.Data.IsActive
|
||||
} else {
|
||||
data.IsActive = false
|
||||
}
|
||||
if data.Width == 0 {
|
||||
data.Width = d.Data.Width - 2
|
||||
}
|
||||
if data.Height == 0 {
|
||||
data.Height = d.Data.Height - 2
|
||||
}
|
||||
data.DefaultColor = colors.Combine(d.Data.Style.Background, d.Data.Style.Foreground)
|
||||
data.Screen = d.Data.Screen
|
||||
osui.RenderOnFrame(c, &frame)
|
||||
}
|
||||
if d.Data.Style.Outline == "" {
|
||||
for i, f := range frame {
|
||||
frame[i] = colors.Combine(d.Data.Style.Foreground, d.Data.Style.Background) + f + colors.Reset
|
||||
}
|
||||
return strings.Join(frame, "\n")
|
||||
} else {
|
||||
for i, f := range frame {
|
||||
frame[i] = d.Data.Style.Outline + "│" + colors.Reset + colors.Combine(d.Data.Style.Foreground, d.Data.Style.Background) + f + colors.Reset + d.Data.Style.Outline + "│" + colors.Reset
|
||||
}
|
||||
}
|
||||
return " " + d.Data.Style.Outline + strings.Repeat("_", d.Data.Width-2) + colors.Reset + "\n" + strings.Join(frame, "\n") + "\n " + d.Data.Style.Outline + strings.Repeat("‾", d.Data.Width-2) + colors.Reset
|
||||
}
|
||||
|
||||
func (d *DivComponent) Update(ctx osui.UpdateContext) bool {
|
||||
if ctx.UpdateKind == osui.UpdateKindKey {
|
||||
switch d.Data.Keys[ctx.Key.Name] {
|
||||
case "up":
|
||||
d.updateActive(findClosestComponent(d.Components, d.ActiveComponent, "up"))
|
||||
case "down":
|
||||
d.updateActive(findClosestComponent(d.Components, d.ActiveComponent, "down"))
|
||||
case "left":
|
||||
d.updateActive(findClosestComponent(d.Components, d.ActiveComponent, "left"))
|
||||
case "right":
|
||||
d.updateActive(findClosestComponent(d.Components, d.ActiveComponent, "right"))
|
||||
default:
|
||||
d.Components[d.ActiveComponent].GetComponentData().IsActive = d.Data.IsActive
|
||||
if d.Components[d.ActiveComponent].Update(ctx) {
|
||||
d.updateActive(findClosestComponent(d.Components, d.ActiveComponent, "down"))
|
||||
}
|
||||
}
|
||||
} else if ctx.UpdateKind == osui.UpdateKindTick {
|
||||
d.Components[d.ActiveComponent].GetComponentData().IsActive = d.Data.IsActive
|
||||
if d.Components[d.ActiveComponent].Update(ctx) {
|
||||
d.updateActive(findClosestComponent(d.Components, d.ActiveComponent, "down"))
|
||||
}
|
||||
}
|
||||
|
||||
return false
|
||||
}
|
||||
|
||||
func (d *DivComponent) updateActive(newIndex int) {
|
||||
if newIndex >= 0 && newIndex < len(d.Components) && len(d.Components) > 0 {
|
||||
d.ActiveComponent = newIndex
|
||||
}
|
||||
}
|
||||
|
||||
func Div(param osui.Param, components ...osui.Component) *DivComponent {
|
||||
param.SetDefaultBindings(map[string]string{
|
||||
keys.CtrlW: "up",
|
||||
keys.CtrlS: "down",
|
||||
keys.CtrlA: "left",
|
||||
keys.CtrlD: "right",
|
||||
})
|
||||
return param.UseParam(&DivComponent{
|
||||
Components: components,
|
||||
}).(*DivComponent)
|
||||
}
|
||||
@@ -0,0 +1,85 @@
|
||||
package ui
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"github.com/orus-dev/osui"
|
||||
"github.com/orus-dev/osui/colors"
|
||||
"github.com/orus-dev/osui/keys"
|
||||
)
|
||||
|
||||
type InputBoxComponent struct {
|
||||
Data osui.ComponentData
|
||||
max_size uint
|
||||
cursor uint
|
||||
InputData string
|
||||
}
|
||||
|
||||
func (s *InputBoxComponent) GetComponentData() *osui.ComponentData {
|
||||
return &s.Data
|
||||
}
|
||||
|
||||
func (s InputBoxComponent) Render() string {
|
||||
s.Data.Style.UseStyle()
|
||||
if s.max_size > uint(len(s.InputData)) {
|
||||
return fmt.Sprintf(
|
||||
" %s\n%s│%s%s│%s\n %s",
|
||||
colors.Reset+s.Data.Style.Outline+strings.Repeat("_", int(s.max_size))+colors.Reset,
|
||||
colors.Reset+s.Data.Style.Outline,
|
||||
colors.Combine(s.Data.Style.Foreground, s.Data.Style.Background)+s.InputData+osui.LogicValue(s.Data.IsActive, s.Data.Style.CursorColor+"█"+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.Data.Style.Outline,
|
||||
colors.Reset+s.Data.DefaultColor,
|
||||
s.Data.Style.Outline+strings.Repeat("‾", int(s.max_size))+colors.Reset+s.Data.DefaultColor,
|
||||
)
|
||||
}
|
||||
|
||||
return fmt.Sprintf(
|
||||
" %s\n%s│%s%s\n %s",
|
||||
colors.Reset+s.Data.Style.Outline+strings.Repeat("_", int(s.max_size))+colors.Reset,
|
||||
colors.Reset+s.Data.Style.Outline,
|
||||
colors.Combine(s.Data.Style.Foreground, s.Data.Style.Background)+s.InputData+osui.LogicValue(s.Data.IsActive, s.Data.Style.CursorColor+"█"+colors.Reset, s.Data.Style.Outline+"|"+colors.Reset),
|
||||
colors.Reset+s.Data.DefaultColor,
|
||||
colors.Reset+s.Data.Style.Outline+strings.Repeat("‾", int(s.max_size))+colors.Reset+s.Data.DefaultColor,
|
||||
)
|
||||
}
|
||||
|
||||
func (s *InputBoxComponent) Update(ctx osui.UpdateContext) bool {
|
||||
if ctx.UpdateKind == osui.UpdateKindKey {
|
||||
switch s.Data.Keys[ctx.Key.Name] {
|
||||
case "done":
|
||||
s.Data.OnClick()
|
||||
return true
|
||||
case "remove":
|
||||
if len(s.InputData) > 0 {
|
||||
s.InputData = s.InputData[:len(s.InputData)-1]
|
||||
}
|
||||
case "left":
|
||||
if s.cursor > 0 {
|
||||
s.cursor--
|
||||
}
|
||||
case "right":
|
||||
if s.cursor < s.max_size {
|
||||
s.cursor++
|
||||
}
|
||||
default:
|
||||
if ctx.Key.Chars[1] == '\x00' {
|
||||
if int(s.max_size) > len(s.InputData) {
|
||||
s.InputData += string(ctx.Key.Chars[0])
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return false
|
||||
}
|
||||
|
||||
func InputBox(param osui.Param, max_size uint) *InputBoxComponent {
|
||||
param.SetDefaultBindings(map[string]string{
|
||||
keys.Enter: "done",
|
||||
keys.Backspace: "remove",
|
||||
keys.Left: "left",
|
||||
keys.Right: "right",
|
||||
})
|
||||
return param.UseParam(&InputBoxComponent{max_size: max_size, Data: osui.ComponentData{}}).(*InputBoxComponent)
|
||||
}
|
||||
+78
@@ -0,0 +1,78 @@
|
||||
package ui
|
||||
|
||||
import (
|
||||
"strings"
|
||||
|
||||
"github.com/orus-dev/osui"
|
||||
"github.com/orus-dev/osui/colors"
|
||||
"github.com/orus-dev/osui/keys"
|
||||
)
|
||||
|
||||
type MenuComponent struct {
|
||||
Data osui.ComponentData
|
||||
Items []string
|
||||
SelectedItem int
|
||||
}
|
||||
|
||||
func (m *MenuComponent) GetComponentData() *osui.ComponentData {
|
||||
return &m.Data
|
||||
}
|
||||
|
||||
func (m *MenuComponent) Render() string {
|
||||
m.Data.Style.UseStyle()
|
||||
res := []string{}
|
||||
|
||||
cursor := m.Data.Style.Cursor
|
||||
if !m.Data.IsActive {
|
||||
cursor = m.Data.Style.InactiveCursor
|
||||
}
|
||||
d, _ := osui.CompressString(m.Data.Style.Cursor)
|
||||
empty := strings.Repeat(" ", len(d))
|
||||
|
||||
for i, item := range m.Items {
|
||||
if i == m.SelectedItem {
|
||||
res = append(res, colors.Reset+cursor+m.Data.Style.SelectedBackground+m.Data.Style.SelectedForeground+item+"\x1b[0m ")
|
||||
} else {
|
||||
res = append(res, colors.Reset+empty+m.Data.Style.Foreground+m.Data.Style.Background+item)
|
||||
}
|
||||
}
|
||||
|
||||
return strings.Join(res, "\n")
|
||||
}
|
||||
|
||||
func (m *MenuComponent) Update(ctx osui.UpdateContext) bool {
|
||||
if ctx.UpdateKind == osui.UpdateKindKey {
|
||||
switch m.Data.Keys[ctx.Key.Name] {
|
||||
case "up":
|
||||
if m.SelectedItem > 0 {
|
||||
m.SelectedItem--
|
||||
} else {
|
||||
m.SelectedItem = len(m.Items) - 1
|
||||
}
|
||||
case "down":
|
||||
if m.SelectedItem+1 < len(m.Items) {
|
||||
m.SelectedItem++
|
||||
} else {
|
||||
m.SelectedItem = 0
|
||||
}
|
||||
case "select":
|
||||
m.Data.OnClick()
|
||||
return true
|
||||
case "exit":
|
||||
return true
|
||||
}
|
||||
}
|
||||
|
||||
return false
|
||||
}
|
||||
|
||||
func Menu(param osui.Param, items ...string) *MenuComponent {
|
||||
param.SetDefaultBindings(map[string]string{
|
||||
keys.Up: "up",
|
||||
keys.Down: "down",
|
||||
keys.Enter: "select",
|
||||
})
|
||||
return param.UseParam(&MenuComponent{
|
||||
Items: items,
|
||||
}).(*MenuComponent)
|
||||
}
|
||||
+102
@@ -0,0 +1,102 @@
|
||||
package ui
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"github.com/orus-dev/osui"
|
||||
"github.com/orus-dev/osui/colors"
|
||||
"github.com/orus-dev/osui/keys"
|
||||
)
|
||||
|
||||
type PaginatorComponent struct {
|
||||
Data osui.ComponentData
|
||||
Components []osui.Component
|
||||
ActiveComponent int
|
||||
}
|
||||
|
||||
func (p *PaginatorComponent) GetComponentData() *osui.ComponentData {
|
||||
return &p.Data
|
||||
}
|
||||
|
||||
func (p *PaginatorComponent) Render() string {
|
||||
width, _ := osui.GetTerminalSize()
|
||||
pgs := strings.Repeat(" ", (width-len(p.Components))/2)
|
||||
p.Data.Style.UseStyle()
|
||||
frame := osui.NewFrame(p.Data.Width, p.Data.Height)
|
||||
for i, c := range p.Components {
|
||||
data := c.GetComponentData()
|
||||
if i == p.ActiveComponent {
|
||||
pgs += p.Data.Style.SelectedForeground + data.Style.SelectedBackground + "•" + colors.Reset
|
||||
data.IsActive = true
|
||||
} else {
|
||||
pgs += colors.Reset + p.Data.Style.Foreground + data.Style.Background + "•"
|
||||
data.IsActive = false
|
||||
}
|
||||
if data.Width == 0 {
|
||||
data.Width = p.Data.Width
|
||||
}
|
||||
if data.Height == 0 {
|
||||
data.Height = p.Data.Height
|
||||
}
|
||||
data.DefaultColor = p.Data.DefaultColor
|
||||
data.Screen = p.Data.Screen
|
||||
}
|
||||
osui.RenderOnFrame(p.Components[p.ActiveComponent], &frame)
|
||||
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(ctx osui.UpdateContext) bool {
|
||||
if ctx.UpdateKind == osui.UpdateKindKey {
|
||||
switch p.Data.Keys[ctx.Key.Name] {
|
||||
case "previous":
|
||||
if p.ActiveComponent > 0 {
|
||||
p.updateActive(p.ActiveComponent - 1)
|
||||
} else {
|
||||
p.updateActive(len(p.Components) - 1)
|
||||
}
|
||||
case "next":
|
||||
if p.ActiveComponent < len(p.Components)-1 {
|
||||
p.updateActive(p.ActiveComponent + 1)
|
||||
} else {
|
||||
p.updateActive(0)
|
||||
}
|
||||
case "exit":
|
||||
p.Data.OnClick()
|
||||
fmt.Print("\n\n")
|
||||
return true
|
||||
default:
|
||||
if len(p.Components) > 0 {
|
||||
p.Components[p.ActiveComponent].GetComponentData().IsActive = p.Data.IsActive
|
||||
if p.Components[p.ActiveComponent].Update(ctx) {
|
||||
if p.ActiveComponent < len(p.Components)-1 {
|
||||
p.updateActive(p.ActiveComponent + 1)
|
||||
} else {
|
||||
p.updateActive(0)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func (p *PaginatorComponent) updateActive(newIndex int) {
|
||||
if newIndex >= 0 && newIndex < len(p.Components) && len(p.Components) > 0 {
|
||||
p.ActiveComponent = newIndex
|
||||
}
|
||||
}
|
||||
|
||||
func Paginator(param osui.Param, pages ...osui.Component) *PaginatorComponent {
|
||||
param.SetDefaultBindings(map[string]string{
|
||||
keys.ShiftTab: "previous",
|
||||
keys.Tab: "next",
|
||||
keys.Escape: "exit",
|
||||
})
|
||||
return param.UseParam(&PaginatorComponent{
|
||||
Components: pages,
|
||||
}).(*PaginatorComponent)
|
||||
}
|
||||
+27
@@ -0,0 +1,27 @@
|
||||
package ui
|
||||
|
||||
import (
|
||||
"github.com/orus-dev/osui"
|
||||
)
|
||||
|
||||
type TextComponent struct {
|
||||
Data osui.ComponentData
|
||||
Text string
|
||||
}
|
||||
|
||||
func (t *TextComponent) Update(ctx osui.UpdateContext) bool {
|
||||
return false
|
||||
}
|
||||
|
||||
func (t *TextComponent) GetComponentData() *osui.ComponentData {
|
||||
return &t.Data
|
||||
}
|
||||
|
||||
func (t TextComponent) Render() string {
|
||||
return t.Text
|
||||
}
|
||||
|
||||
func Text(param osui.Param, text string) *TextComponent {
|
||||
param.SetDefaultBindings(map[string]string{})
|
||||
return param.UseParam(&TextComponent{Text: text}).(*TextComponent)
|
||||
}
|
||||
@@ -1,9 +0,0 @@
|
||||
package ui
|
||||
|
||||
func Text(text string) *TextComponent {
|
||||
return &TextComponent{Text: text}
|
||||
}
|
||||
|
||||
func InputBox(max_size uint) *InputBoxComponent {
|
||||
return &InputBoxComponent{max_size: max_size}
|
||||
}
|
||||
+115
@@ -0,0 +1,115 @@
|
||||
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
|
||||
}
|
||||
|
||||
type Id[T osui.Component] struct {
|
||||
changer func(T) T
|
||||
Component T
|
||||
}
|
||||
|
||||
func (i *Id[T]) Id(c T) T {
|
||||
i.Component = c
|
||||
if i.changer != nil {
|
||||
return i.changer(i.Component)
|
||||
}
|
||||
return i.Component
|
||||
}
|
||||
|
||||
func (i *Id[T]) SetProperties(c func(T) T) {
|
||||
i.changer = c
|
||||
}
|
||||
|
||||
type Class[T osui.Component] struct {
|
||||
changer func(T) T
|
||||
}
|
||||
|
||||
func (c *Class[T]) Class(co T) T {
|
||||
if c.changer != nil {
|
||||
return c.changer(co)
|
||||
}
|
||||
return co
|
||||
}
|
||||
|
||||
func (c *Class[T]) SetProperties(ch func(T) T) {
|
||||
c.changer = ch
|
||||
}
|
||||
@@ -3,75 +3,123 @@ package osui
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"os/exec"
|
||||
"regexp"
|
||||
"runtime"
|
||||
"strings"
|
||||
|
||||
"golang.org/x/term"
|
||||
"github.com/nathan-fiscaletti/consolesize-go"
|
||||
"github.com/orus-dev/osui/keys"
|
||||
)
|
||||
|
||||
func renderLine(frameLine, line string, x int) string {
|
||||
result := ""
|
||||
if x >= len(frameLine) {
|
||||
return result
|
||||
var re = regexp.MustCompile(`(\x1b\[([0-9;]*)[a-zA-Z])+`)
|
||||
|
||||
func RenderLine(frame_, line_ string, x int, fm, lm map[int]string) string {
|
||||
var res strings.Builder
|
||||
frame := []rune(frame_)
|
||||
line := []rune(line_)
|
||||
|
||||
flen, llen := len(frame), len(line)
|
||||
for i := 0; i < flen; i++ {
|
||||
if i >= x && i-x < llen {
|
||||
if v, ok := lm[i-x]; ok {
|
||||
res.WriteString(v)
|
||||
}
|
||||
res.WriteRune(line[i-x])
|
||||
} else {
|
||||
if v, ok := fm[i]; ok {
|
||||
res.WriteString(v)
|
||||
}
|
||||
res.WriteRune(frame[i])
|
||||
}
|
||||
}
|
||||
return res.String()
|
||||
}
|
||||
|
||||
lChars := []rune(line)
|
||||
for i, c := range frameLine {
|
||||
if i >= x && len(lChars) > i-x {
|
||||
result += string(lChars[i-x])
|
||||
func CompressString(input string) (string, map[int]string) {
|
||||
matchesMap := make(map[int]string)
|
||||
res := []rune{}
|
||||
|
||||
i := 0
|
||||
for i < len([]rune(input)) {
|
||||
loc := re.FindStringIndex(string([]rune(input)[i:]))
|
||||
|
||||
if loc != nil && loc[0] == 0 {
|
||||
ansiSeq := re.FindString(string([]rune(input)[i:]))
|
||||
matchesMap[len(res)] = ansiSeq
|
||||
i += len([]rune(ansiSeq))
|
||||
} else {
|
||||
result += string(c)
|
||||
res = append(res, []rune(input)[i])
|
||||
i++
|
||||
}
|
||||
}
|
||||
return result
|
||||
|
||||
return string(res), matchesMap
|
||||
}
|
||||
|
||||
func RenderOnFrame(c Component, frame *[]string) {
|
||||
componentData := c.GetComponentData()
|
||||
for i, line := range strings.Split(c.Render(), "\n") {
|
||||
if int(componentData.Y)+i < len(*frame) {
|
||||
(*frame)[int(componentData.Y)+i] = renderLine((*frame)[int(componentData.Y)+i], line, int(componentData.X))
|
||||
fo, fm := CompressString((*frame)[int(componentData.Y)+i])
|
||||
lo, lm := CompressString(line)
|
||||
(*frame)[int(componentData.Y)+i] = RenderLine(fo, lo, componentData.X, fm, lm)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func ReadKey() (string, error) {
|
||||
oldState, err := term.MakeRaw(int(os.Stdin.Fd()))
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
defer term.Restore(int(os.Stdin.Fd()), oldState)
|
||||
func ReadKey() Key {
|
||||
var b [3]byte
|
||||
n, err := os.Stdin.Read(b[:])
|
||||
if err != nil {
|
||||
return "", err
|
||||
return Key{Chars: [3]rune{'E', 'r', 'r'}, Name: err.Error()}
|
||||
}
|
||||
return string(b[:n]), nil
|
||||
return Key{Chars: [3]rune{rune(b[0]), rune(b[1]), rune(b[2])}, Name: keys.GetKeyName(string(b[:n]))}
|
||||
}
|
||||
|
||||
func NewFrame(width, height int) []string {
|
||||
frame := make([]string, height)
|
||||
for i := 0; i < height; i++ {
|
||||
frame[i] = strings.Repeat(" ", width)
|
||||
}
|
||||
return frame
|
||||
}
|
||||
|
||||
func Clear() {
|
||||
fmt.Print("\x1b[H\x1b[2J\x1b[3J")
|
||||
switch runtime.GOOS {
|
||||
case "windows":
|
||||
cmd := exec.Command("cmd", "/c", "cls")
|
||||
cmd.Stdout = os.Stdout
|
||||
cmd.Run()
|
||||
default:
|
||||
cmd := exec.Command("clear")
|
||||
cmd.Stdout = os.Stdout
|
||||
cmd.Run()
|
||||
}
|
||||
}
|
||||
|
||||
type Key_ = string
|
||||
|
||||
type key struct {
|
||||
Enter Key_
|
||||
Tab Key_
|
||||
Backspace Key_
|
||||
Escape Key_
|
||||
Up Key_
|
||||
Down Key_
|
||||
Right Key_
|
||||
Left Key_
|
||||
func ShowCursor() {
|
||||
fmt.Print("\033[?25h")
|
||||
}
|
||||
|
||||
var Key = key{
|
||||
Enter: "\r",
|
||||
Tab: "\t",
|
||||
Backspace: "\x7f",
|
||||
Escape: "\x1b",
|
||||
Up: "\x1b[A",
|
||||
Down: "\x1b[B",
|
||||
Right: "\x1b[C",
|
||||
Left: "\x1b[D",
|
||||
func HideCursor() {
|
||||
fmt.Print("\033[?25l")
|
||||
}
|
||||
|
||||
func LogicValue(b bool, _if, _else string) string {
|
||||
if b {
|
||||
return _if
|
||||
}
|
||||
return _else
|
||||
}
|
||||
|
||||
func LogicValueInt(b bool, _if, _else int) int {
|
||||
if b {
|
||||
return _if
|
||||
}
|
||||
return _else
|
||||
}
|
||||
|
||||
func GetTerminalSize() (int, int) {
|
||||
return consolesize.GetConsoleSize()
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user