perfomance boost

This commit is contained in:
2024-10-03 20:02:56 +02:00
parent f2f7e51750
commit 0f052d7652
5 changed files with 80 additions and 28 deletions
+47 -15
View File
@@ -2,29 +2,61 @@ package main
import ( import (
"github.com/orus-dev/osui" "github.com/orus-dev/osui"
"github.com/orus-dev/osui/colors"
"github.com/orus-dev/osui/ui" "github.com/orus-dev/osui/ui"
) )
var students []string = []string{}
func main() { func main() {
screen := osui.NewScreen(App()) screen := osui.NewScreen(App())
screen.Run() screen.Run()
} }
func App() *ui.DivComponent { func App() *ui.PaginatorComponent {
return renderRoot() w, h := osui.GetTerminalSize()
}
func renderRoot() *ui.DivComponent { var keybindings ui.Class[*ui.TextComponent]
var root ui.Id[*ui.DivComponent] keybindings.SetProperties(func(tc *ui.TextComponent) *ui.TextComponent {
tc.Text = colors.Blue + tc.Text
tc.Data.Y = h - 3
return tc
})
return root.Id(ui.Div( return ui.Paginator(
ui.WithPosition(0, 0, ui.Text("student managment system (terminal edition permium) made by fefek")), // Page 1
ui.WithPosition(0, 2, ui.Button("add student")), ui.Div(
ui.WithPosition(25, 2, ui.Button("delete student")), ui.Text("Welcome to the example! The blue text on the bottom show the keybindings"),
ui.WithPosition(50, 2, ui.Button("info")), keybindings.Class(ui.Text("Next Page: Tab. Previous Page: shift + tab")),
ui.WithPosition(0, 5, ui.Text("students")), ),
ui.WithPosition(0, 7, ui.Menu(students...)),
)) // 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(6, 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 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 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. ")),
),
)
} }
+2
View File
@@ -52,6 +52,7 @@ func (s *Screen) Render() {
} }
func (s *Screen) Run() { func (s *Screen) Run() {
DisableEcho()
data := s.component.GetComponentData() data := s.component.GetComponentData()
data.Screen = s data.Screen = s
for { for {
@@ -59,6 +60,7 @@ func (s *Screen) Run() {
k, _ := ReadKey() k, _ := ReadKey()
if s.component.Update(k) { if s.component.Update(k) {
ShowCursor() ShowCursor()
EnableEcho()
return return
} }
} }
+9 -9
View File
@@ -45,30 +45,30 @@ func (b *ButtonComponent) Render() string {
if b.Clicked { if b.Clicked {
return fmt.Sprintf(" %s\n%s│%s│%s\n %s", 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+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.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 { if b.Data.IsActive {
return fmt.Sprintf(" %s\n%s│%s│%s\n %s", 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+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.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", 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+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.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,
) )
} }
+4 -4
View File
@@ -33,7 +33,7 @@ func (d *DivComponent) GetComponentData() *osui.ComponentData {
func (d *DivComponent) Render() string { func (d *DivComponent) Render() string {
osui.UseStyle(d.Style) 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 { for i, c := range d.Components {
data := c.GetComponentData() data := c.GetComponentData()
if i == d.ActiveComponent { if i == d.ActiveComponent {
@@ -42,10 +42,10 @@ func (d *DivComponent) Render() string {
data.IsActive = false data.IsActive = false
} }
if data.Width == 0 { if data.Width == 0 {
data.Width = d.Data.Width data.Width = d.Data.Width - 2
} }
if data.Height == 0 { 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.DefaultColor = colors.Combine(d.Style.Background, d.Style.Foreground)
data.Screen = d.Data.Screen data.Screen = d.Data.Screen
@@ -61,7 +61,7 @@ func (d *DivComponent) Render() string {
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.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 { func (d *DivComponent) Update(key string) bool {
+18
View File
@@ -84,6 +84,24 @@ func ReadKey() (string, error) {
return string(b[:n]), nil return string(b[:n]), nil
} }
func DisableEcho() {
switch runtime.GOOS {
case "windows":
// Need to implement this later
default:
exec.Command("stty", "-F", "/dev/tty", "-echo").Run()
}
}
func EnableEcho() {
switch runtime.GOOS {
case "windows":
// Need to implement this later
default:
exec.Command("stty", "-F", "/dev/tty", "echo").Run()
}
}
func NewFrame(width, height int) []string { func NewFrame(width, height int) []string {
frame := make([]string, height) frame := make([]string, height)
for i := 0; i < height; i++ { for i := 0; i < height; i++ {