9 Commits
Author SHA1 Message Date
selimaj-dev 9cd88b0350 screen update 2024-10-05 15:28:25 +02:00
selimaj-dev d4f6c4104d fixed flickering on all platforms 2024-10-04 22:55:30 +02:00
selimaj-dev 0f052d7652 perfomance boost 2024-10-03 20:02:56 +02:00
selimaj-dev f2f7e51750 small performance increase 2024-10-03 19:29:18 +02:00
selimaj-dev 7e714abddf fixed color bug 2024-10-03 13:19:43 +02:00
selimaj-dev 3ceaa13b3f fixed color bug 2024-10-03 13:10:43 +02:00
selimaj-dev 8165e80b05 fixed ui when horizontal but color bug 2024-10-01 19:53:19 +02:00
selimaj-dev f77c04c908 fixed ui when horizontal 2024-10-01 16:37:54 +02:00
selimaj-dev 27530fe8cc fixed ui when horizontal 2024-10-01 16:34:50 +02:00
6 changed files with 116 additions and 114 deletions
+46 -46
View File
@@ -1,62 +1,62 @@
package main
import (
"github.com/orus-dev/osui"
"github.com/orus-dev/osui/colors"
"github.com/orus-dev/osui/ui"
"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()
screen := osui.NewScreen(App())
screen.Run()
}
func App() *ui.PaginatorComponent {
w, h := osui.GetTerminalSize()
w, h := osui.GetTerminalSize()
var keybindings ui.Class[*ui.TextComponent]
keybindings.SetProperties(func(tc *ui.TextComponent) *ui.TextComponent {
tc.Text = colors.Blue + tc.Text
tc.Data.Y = h - 2
return tc
})
var keybindings ui.Class[*ui.TextComponent]
keybindings.SetProperties(func(tc *ui.TextComponent) *ui.TextComponent {
tc.Text = colors.Blue + tc.Text
tc.Data.Y = h - 3
return tc
})
return ui.Paginator(
// Page 1
ui.Div(
ui.Text("Welcome to the example! The blue text on the bottom show the keybindings"),
keybindings.Class(ui.Text("Next Page: Tab. Previous Page: shift + tab")),
),
return ui.Paginator(
// Page 1
ui.Div(
ui.Text("Welcome to the example! The blue text on the bottom show the keybindings"),
keybindings.Class(ui.Text("Next Page: Tab. Previous Page: shift + tab")),
),
// Page 2
ui.Div(
ui.WithPosition(0, 0, ui.Text("This blue square is a div. A div stores multiple components into one")),
ui.WithPosition((w-30)/2, (h-4)/2,
ui.WithSize(30, 5,
ui.Div(ui.WithPosition(4, 1, ui.Button("Hello, World!").Params(ui.ButtonParams{Width: 19}))).Params(ui.DivParams{Style: ui.DivStyle{Outline: colors.Blue}}),
)),
keybindings.Class(ui.Text("Navigate: Ctrl+(W/A/S/D)")),
),
// Page 2
ui.Div(
ui.WithPosition(0, 0, ui.Text("This blue square is a div. A div stores multiple components into one")),
ui.WithPosition((w-30)/2, (h-4)/2,
ui.WithSize(32, 7,
ui.Div(ui.WithPosition(2, 2, ui.Text("This text is inside a div!"))).Params(ui.DivParams{Style: ui.DivStyle{Outline: colors.Blue, Foreground: colors.Red}}),
)),
keybindings.Class(ui.Text("Navigate: Ctrl+(W/A/S/D)")),
),
// Page 3
ui.Div(
ui.WithPosition(0, 0, ui.Text("This is a InputBox. It takes user input just like a GUI. "+colors.Red+colors.Bold+"NOTE: This InputBox is inside a div")),
ui.WithPosition((w-32)/2, (h-3)/2, ui.InputBox(30)),
keybindings.Class(ui.Text("Navigate: Ctrl+(W/A/S/D). Finish: Enter.")),
),
// Page 3
ui.Div(
ui.WithPosition(0, 0, ui.Text("This is a InputBox. It takes user input just like a GUI. "+colors.Red+colors.Bold+"NOTE: This InputBox is inside a div")),
ui.WithPosition((w-32)/2, (h-3)/2, ui.InputBox(30)),
keybindings.Class(ui.Text("Navigate: Ctrl+(W/A/S/D). Finish: Enter.")),
),
// Page 4
ui.Div(
ui.WithPosition(0, 0, ui.Text("This is a Button. To click it press Enter. "+colors.Red+colors.Bold+"NOTE: This Button is inside a div")),
ui.WithPosition((w-20)/2, (h-3)/2, ui.Button("This is a button")),
keybindings.Class(ui.Text("Navigate: Ctrl+(W/A/S/D). Click Button: Enter")),
),
// Page 4
ui.Div(
ui.WithPosition(0, 0, ui.Text("This is a Button. To click it press Enter. "+colors.Red+colors.Bold+"NOTE: This Button is inside a div")),
ui.WithPosition((w-20)/2, (h-3)/2, ui.Button("This is a button")),
keybindings.Class(ui.Text("Navigate: Ctrl+(W/A/S/D). Click Button: Enter")),
),
// Page 5
ui.Div(
ui.WithPosition(0, 0, ui.Text("This is a Menu. It prompts you to select an item. "+colors.Red+colors.Bold+"NOTE: This Menu is inside a div")),
ui.WithPosition(0, 2, ui.Menu("Item 1", "Item 2", "Item 3", "Item 4", "Item 5").Params(ui.MenuParams{OnSelected: func(mc *ui.MenuComponent, b bool) {}})),
keybindings.Class(ui.Text("Navigate: Ctrl+(W/A/S/D). Navigate Menu: W/S. Select: Enter. Exit Menu: Q. ")),
),
)
// Page 5
ui.Div(
ui.WithPosition(0, 0, ui.Text("This is a Menu. It prompts you to select an item. "+colors.Red+colors.Bold+"NOTE: This Menu is inside a div")),
ui.WithPosition(0, 2, ui.Menu("Item 1", "Item 2", "Item 3", "Item 4", "Item 5").Params(ui.MenuParams{OnSelected: func(mc *ui.MenuComponent, b bool) {}})),
keybindings.Class(ui.Text("Navigate: Ctrl+(W/A/S/D). Navigate Menu: W/S. Select: Enter. Exit Menu: Q. ")),
),
)
}
+15 -8
View File
@@ -2,9 +2,11 @@ package osui
import (
"fmt"
"os"
"strings"
"github.com/orus-dev/osui/colors"
"golang.org/x/term"
)
type ComponentData struct {
@@ -24,20 +26,19 @@ type Component interface {
}
type Screen struct {
component Component
Component Component
}
func NewScreen(c Component) *Screen {
HideCursor()
s := &Screen{component: c}
s := &Screen{Component: c}
return s
}
func (s *Screen) Render() {
Clear()
width, height := GetTerminalSize()
frame := NewFrame(width, height)
data := s.component.GetComponentData()
data := s.Component.GetComponentData()
if data.Height == 0 {
data.Height = height
}
@@ -47,17 +48,23 @@ func (s *Screen) Render() {
data.Screen = s
data.IsActive = true
data.DefaultColor = colors.Reset
RenderOnFrame(s.component, &frame)
fmt.Print(strings.Join(frame, "\n"))
RenderOnFrame(s.Component, &frame)
Clear()
fmt.Print(strings.Join(frame, ""))
}
func (s *Screen) Run() {
data := s.component.GetComponentData()
oldState, err := term.MakeRaw(int(os.Stdin.Fd()))
if err != nil {
panic(err)
}
defer term.Restore(int(os.Stdin.Fd()), oldState)
data := s.Component.GetComponentData()
data.Screen = s
for {
s.Render()
k, _ := ReadKey()
if s.component.Update(k) {
if s.Component.Update(k) {
ShowCursor()
return
}
+9 -9
View File
@@ -45,30 +45,30 @@ func (b *ButtonComponent) Render() string {
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+strings.Repeat("_", b.Data.Width-2)+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+colors.Combine(b.Style.ClickedBackground, b.Style.ClickedForeground)+centerText(b.Text, b.Data.Width-2)+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,
colors.Reset+b.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.Style.ActiveOutline+strings.Repeat("_", b.Data.Width)+colors.Reset+b.Data.DefaultColor,
colors.Reset+b.Style.ActiveOutline+strings.Repeat("_", b.Data.Width-2)+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+colors.Combine(b.Style.ActiveBackground, b.Style.ActiveForeground)+centerText(b.Text, b.Data.Width-2)+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,
colors.Reset+b.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.Style.Outline+strings.Repeat("_", b.Data.Width)+colors.Reset+b.Data.DefaultColor,
colors.Reset+b.Style.Outline+strings.Repeat("_", b.Data.Width-2)+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+colors.Combine(b.Style.Background, b.Style.Foreground)+centerText(b.Text, b.Data.Width-2)+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,
colors.Reset+b.Style.Outline+strings.Repeat("‾", b.Data.Width-2)+colors.Reset+b.Data.DefaultColor,
)
}
+5 -5
View File
@@ -33,7 +33,7 @@ func (d *DivComponent) GetComponentData() *osui.ComponentData {
func (d *DivComponent) Render() string {
osui.UseStyle(d.Style)
frame := osui.NewFrame(d.Data.Width, d.Data.Height)
frame := osui.NewFrame(d.Data.Width-2, d.Data.Height-2)
for i, c := range d.Components {
data := c.GetComponentData()
if i == d.ActiveComponent {
@@ -42,10 +42,10 @@ func (d *DivComponent) Render() string {
data.IsActive = false
}
if data.Width == 0 {
data.Width = d.Data.Width
data.Width = d.Data.Width - 2
}
if data.Height == 0 {
data.Height = d.Data.Height
data.Height = d.Data.Height - 2
}
data.DefaultColor = colors.Combine(d.Style.Background, d.Style.Foreground)
data.Screen = d.Data.Screen
@@ -58,10 +58,10 @@ func (d *DivComponent) Render() string {
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
frame[i] = d.Style.Outline + "│" + colors.Reset + 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
return " " + d.Style.Outline + strings.Repeat("_", d.Data.Width-2) + colors.Reset + "\n" + strings.Join(frame, "\n") + "\n " + d.Style.Outline + strings.Repeat("‾", d.Data.Width-2) + colors.Reset
}
func (d *DivComponent) Update(key string) bool {
+1 -1
View File
@@ -42,7 +42,7 @@ func (m *MenuComponent) Render() string {
if !m.Data.IsActive {
cursor = m.Style.CursorInactive
}
d, _ := osui.CompressString(m.Style.Cursor, "")
d, _ := osui.CompressString(m.Style.Cursor)
empty := strings.Repeat(" ", len(d))
for i, item := range m.Items {
+39 -44
View File
@@ -11,69 +11,65 @@ import (
"github.com/nathan-fiscaletti/consolesize-go"
"github.com/orus-dev/osui/colors"
"golang.org/x/term"
)
func renderLine(frameLine, line_ string, x int) string {
frame := []rune(frameLine)
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_)
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:]...)...)
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)
}
frame[i+j] = line[v]
v++
res.WriteRune(line[i-x])
} else {
if v, ok := fm[i]; ok {
res.WriteString(v)
}
res.WriteRune(frame[i])
}
if c == '\b' {
}
return res.String()
}
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 {
res = append(res, []rune(input)[i])
i++
}
}
return string(frame)
return string(res), matchesMap
}
func RenderOnFrame(c Component, frame *[]string) {
componentData := c.GetComponentData()
for i, lo := range strings.Split(c.Render(), "\n") {
for i, line := range strings.Split(c.Render(), "\n") {
if int(componentData.Y)+i < len(*frame) {
fo := (*frame)[int(componentData.Y)+i]
f, fa := CompressString(fo, "\b")
line, la := CompressString(lo, "\t")
r := renderLine(f, line, int(componentData.X))
(*frame)[int(componentData.Y)+i] = DecompressString(DecompressString(r, la, "\t"), fa, "\b")
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 CompressString(input, repl string) (string, []string) {
pattern := `\x1b\[([0-9;]*)[a-zA-Z]`
re := regexp.MustCompile(pattern)
matches := re.FindAllString(input, -1)
return re.ReplaceAllString(input, repl), matches
}
func DecompressString(modified string, outputArray []string, c string) string {
parts := strings.Split(modified, c)
reconstructed := ""
for i, part := range parts {
reconstructed += part
if i < len(outputArray) {
reconstructed += outputArray[i]
}
}
return reconstructed
}
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)
var b [3]byte
n, err := os.Stdin.Read(b[:])
if err != nil {
@@ -174,7 +170,6 @@ func LogicValueInt(b bool, _if, _else int) int {
return _else
}
// Get the terminal size
func GetTerminalSize() (int, int) {
return consolesize.GetConsoleSize()