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(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.WithPosition((w-30)/2, (h-4)/2,
|
||||||
ui.WithSize(32, 7,
|
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)")),
|
keybindings.Class(ui.Text("Navigate: Ctrl+(W/A/S/D)")),
|
||||||
),
|
),
|
||||||
|
|||||||
@@ -2,9 +2,11 @@ package osui
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"os"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/orus-dev/osui/colors"
|
"github.com/orus-dev/osui/colors"
|
||||||
|
"golang.org/x/term"
|
||||||
)
|
)
|
||||||
|
|
||||||
type ComponentData struct {
|
type ComponentData struct {
|
||||||
@@ -24,19 +26,19 @@ type Component interface {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type Screen struct {
|
type Screen struct {
|
||||||
component Component
|
Component Component
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewScreen(c Component) *Screen {
|
func NewScreen(c Component) *Screen {
|
||||||
HideCursor()
|
HideCursor()
|
||||||
s := &Screen{component: c}
|
s := &Screen{Component: c}
|
||||||
return s
|
return s
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Screen) Render() {
|
func (s *Screen) Render() {
|
||||||
width, height := GetTerminalSize()
|
width, height := GetTerminalSize()
|
||||||
frame := NewFrame(width, height)
|
frame := NewFrame(width, height)
|
||||||
data := s.component.GetComponentData()
|
data := s.Component.GetComponentData()
|
||||||
if data.Height == 0 {
|
if data.Height == 0 {
|
||||||
data.Height = height
|
data.Height = height
|
||||||
}
|
}
|
||||||
@@ -46,21 +48,24 @@ func (s *Screen) Render() {
|
|||||||
data.Screen = s
|
data.Screen = s
|
||||||
data.IsActive = true
|
data.IsActive = true
|
||||||
data.DefaultColor = colors.Reset
|
data.DefaultColor = colors.Reset
|
||||||
RenderOnFrame(s.component, &frame)
|
RenderOnFrame(s.Component, &frame)
|
||||||
Clear()
|
Clear()
|
||||||
fmt.Print(strings.Join(frame, "\n"))
|
fmt.Print(strings.Join(frame, ""))
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Screen) Run() {
|
func (s *Screen) Run() {
|
||||||
DisableEcho()
|
oldState, err := term.MakeRaw(int(os.Stdin.Fd()))
|
||||||
data := s.component.GetComponentData()
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
defer term.Restore(int(os.Stdin.Fd()), oldState)
|
||||||
|
data := s.Component.GetComponentData()
|
||||||
data.Screen = s
|
data.Screen = s
|
||||||
for {
|
for {
|
||||||
s.Render()
|
s.Render()
|
||||||
k, _ := ReadKey()
|
k, _ := ReadKey()
|
||||||
if s.component.Update(k) {
|
if s.Component.Update(k) {
|
||||||
ShowCursor()
|
ShowCursor()
|
||||||
EnableEcho()
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -58,7 +58,7 @@ func (d *DivComponent) Render() string {
|
|||||||
return strings.Join(frame, "\n")
|
return strings.Join(frame, "\n")
|
||||||
} else {
|
} else {
|
||||||
for i, f := range frame {
|
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
|
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/nathan-fiscaletti/consolesize-go"
|
||||||
"github.com/orus-dev/osui/colors"
|
"github.com/orus-dev/osui/colors"
|
||||||
"golang.org/x/term"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
var re = regexp.MustCompile(`(\x1b\[([0-9;]*)[a-zA-Z])+`)
|
var re = regexp.MustCompile(`(\x1b\[([0-9;]*)[a-zA-Z])+`)
|
||||||
@@ -71,11 +70,6 @@ func RenderOnFrame(c Component, frame *[]string) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func ReadKey() (string, error) {
|
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
|
var b [3]byte
|
||||||
n, err := os.Stdin.Read(b[:])
|
n, err := os.Stdin.Read(b[:])
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -84,24 +78,6 @@ 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++ {
|
||||||
|
|||||||
Reference in New Issue
Block a user