Compare commits
2
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
9cd88b0350 | ||
|
|
d4f6c4104d |
+1
-1
@@ -33,7 +33,7 @@ func App() *ui.PaginatorComponent {
|
||||
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}}),
|
||||
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)")),
|
||||
),
|
||||
|
||||
@@ -2,9 +2,11 @@ package osui
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"strings"
|
||||
|
||||
"github.com/orus-dev/osui/colors"
|
||||
"golang.org/x/term"
|
||||
)
|
||||
|
||||
type ComponentData struct {
|
||||
@@ -24,19 +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() {
|
||||
width, height := GetTerminalSize()
|
||||
frame := NewFrame(width, height)
|
||||
data := s.component.GetComponentData()
|
||||
data := s.Component.GetComponentData()
|
||||
if data.Height == 0 {
|
||||
data.Height = height
|
||||
}
|
||||
@@ -46,21 +48,24 @@ func (s *Screen) Render() {
|
||||
data.Screen = s
|
||||
data.IsActive = true
|
||||
data.DefaultColor = colors.Reset
|
||||
RenderOnFrame(s.component, &frame)
|
||||
RenderOnFrame(s.Component, &frame)
|
||||
Clear()
|
||||
fmt.Print(strings.Join(frame, "\n"))
|
||||
fmt.Print(strings.Join(frame, ""))
|
||||
}
|
||||
|
||||
func (s *Screen) Run() {
|
||||
DisableEcho()
|
||||
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()
|
||||
EnableEcho()
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
@@ -58,7 +58,7 @@ 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-2) + colors.Reset + "\n" + strings.Join(frame, "\n") + "\n " + d.Style.Outline + strings.Repeat("‾", d.Data.Width-2) + colors.Reset
|
||||
|
||||
@@ -11,7 +11,6 @@ import (
|
||||
|
||||
"github.com/nathan-fiscaletti/consolesize-go"
|
||||
"github.com/orus-dev/osui/colors"
|
||||
"golang.org/x/term"
|
||||
)
|
||||
|
||||
var re = regexp.MustCompile(`(\x1b\[([0-9;]*)[a-zA-Z])+`)
|
||||
@@ -71,11 +70,6 @@ func RenderOnFrame(c Component, frame *[]string) {
|
||||
}
|
||||
|
||||
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 {
|
||||
@@ -84,24 +78,6 @@ func ReadKey() (string, error) {
|
||||
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 {
|
||||
frame := make([]string, height)
|
||||
for i := 0; i < height; i++ {
|
||||
|
||||
Reference in New Issue
Block a user