tui/tests/screen_test.go

315 lines
8.2 KiB
Go
Raw Normal View History

2021-01-10 21:52:29 +01:00
package tui
import (
"errors"
"fmt"
"log"
"math/rand"
"os"
"strconv"
2022-05-04 14:16:08 +02:00
"strings"
2021-01-10 21:52:29 +01:00
"testing"
2022-04-01 20:10:51 +02:00
"git.tordarus.net/Tordarus/tui"
"git.tordarus.net/Tordarus/tui/views"
2021-01-10 21:52:29 +01:00
"github.com/gdamore/tcell"
)
var logger *log.Logger
func initDebugLogger() func() {
out, err := os.Create("output.log")
if err != nil {
panic(err)
}
logger = log.New(out, "", log.LstdFlags|log.Lmicroseconds)
return func() {
out.Close()
}
}
func TestScrollView(t *testing.T) {
//defer initDebugLogger()()
textViews := make([]tui.View, 0, 50)
for i := 0; i < cap(textViews); i++ {
textViews = append(textViews, views.NewTextView(strconv.Itoa(i)))
textViews[i].SetStyle(textViews[i].Style().Foreground(tcell.ColorBlack).Background(tcell.Color(rand.Intn(int(tcell.ColorYellowGreen)))))
}
flowLayout := views.NewFlowLayout(tui.Vertical)
flowLayout.AppendViews(textViews...)
scrollView := views.NewScrollView(flowLayout)
screen, err := tui.NewScreen(scrollView)
if err != nil {
t.Error(err)
return
}
screen.KeyPressed = func(event *tui.KeyEvent) (consumed bool) {
switch event.Key() {
case tcell.KeyCtrlC:
screen.StopWithError(errors.New(fmt.Sprintf("key: %#v | rune: %s", event.Key(), string(event.Rune()))))
case tcell.KeyPgDn:
scrollView.Scroll(10, 0)
case tcell.KeyPgUp:
scrollView.Scroll(-10, 0)
}
return true
}
2022-05-04 14:16:08 +02:00
screen.MouseEvent = func(event *tui.MouseEvent) (consumed bool) {
2022-04-03 17:31:46 +02:00
//textViews[0].(*views.TextView).Text = fmt.Sprintf("mouse position: %d | %d", event.X, event.Y)
//textViews[1].(*views.TextView).Text = fmt.Sprintf("mouse button: %d", event.Button)
if event.Button == tui.MouseWheelUp {
scrollView.Scroll(-1, 0)
return true
} else if event.Button == tui.MouseWheelDown {
scrollView.Scroll(1, 0)
return true
2022-04-04 15:05:35 +02:00
} else if event.Button == tui.MouseButtonMiddle {
panic("hi")
}
return false
}
err = screen.Start()
fmt.Println(err)
}
2022-05-04 10:17:16 +02:00
func TestBorderView(t *testing.T) {
textView := views.NewTextView("hello world!")
borderView := views.NewBorderView(textView)
//borderView2 := views.NewBorderView(borderView)
screen, err := tui.NewScreen(borderView)
if err != nil {
t.Error(err)
return
}
if err := screen.Start(); err != nil {
fmt.Println(err)
}
}
2022-05-04 11:13:18 +02:00
func TestGrowView(t *testing.T) {
textView := views.NewTextView("hello world")
textView.SetStyle(textView.Style().Background(tcell.ColorYellow).Foreground(tcell.ColorBlack))
growView := views.NewGrowView(textView)
screen, err := tui.NewScreen(growView)
if err != nil {
t.Error(err)
return
}
if err := screen.Start(); err != nil {
fmt.Println(err)
}
}
func TestMousePosition(t *testing.T) {
textView := views.NewTextView("hello world")
screen, err := tui.NewScreen(textView)
if err != nil {
t.Error(err)
return
}
2022-05-04 14:16:08 +02:00
screen.MouseEvent = func(event *tui.MouseEvent) (consumed bool) {
textView.Text = fmt.Sprintf("position: %s", event.Position.String())
return true
}
if err := screen.Start(); err != nil {
fmt.Println(err)
}
}
2022-04-02 15:21:17 +02:00
func TestFlowLayout(t *testing.T) {
2022-04-02 13:01:41 +02:00
textView := views.NewTextView("hello world!")
textView.SetStyle(tui.StyleDefault.Background(tcell.ColorRed).Foreground(tcell.ColorBlack))
marginView := views.NewMarginView(textView, 3, 1, 1, 0)
2022-04-02 13:01:41 +02:00
//borderView := views.NewBorderView(textView)
textView2 := views.NewTextView("Hi!")
textView2.SetStyle(tui.StyleDefault.Background(tcell.ColorBlue).Foreground(tcell.ColorYellow))
2022-05-04 11:13:18 +02:00
growView := views.NewGrowView(nil)
2022-04-02 13:01:41 +02:00
growView.SetStyle(tui.StyleDefault.Background(tcell.ColorGreen))
2022-05-04 11:13:18 +02:00
growView2 := views.NewGrowView(nil)
2022-04-02 13:01:41 +02:00
growView2.SetStyle(tui.StyleDefault.Background(tcell.ColorYellow))
2022-04-02 15:21:17 +02:00
flowLayout := views.NewFlowLayout(tui.Vertical)
flowLayout.AppendViews(marginView, growView, textView2)
2022-04-02 13:01:41 +02:00
2022-05-04 11:13:18 +02:00
constrainView := views.NewConstrainView(flowLayout, -1, -1)
2022-04-02 13:01:41 +02:00
constrainView.SetStyle(tui.StyleDefault.Background(tcell.ColorPurple))
screen, err := tui.NewScreen(constrainView)
2021-01-10 21:52:29 +01:00
if err != nil {
t.Error(err)
return
}
2022-04-02 13:01:41 +02:00
screen.KeyPressed = func(event *tui.KeyEvent) (consumed bool) {
textView.Text = event.When().String()
2022-04-01 20:10:51 +02:00
if event.Key() == tcell.KeyCtrlC {
screen.StopWithError(errors.New(fmt.Sprintf("key: %#v | rune: %s", event.Key(), string(event.Rune()))))
}
2022-04-02 13:01:41 +02:00
return true
}
err = screen.Start()
fmt.Println(err)
}
2022-04-02 15:21:17 +02:00
func TestSeparatorLayout(t *testing.T) {
2022-04-02 15:09:52 +02:00
textView := views.NewTextView("hello world!")
textView.SetStyle(tui.StyleDefault.Background(tcell.ColorRed).Foreground(tcell.ColorBlack))
frameView := views.NewFrameView(textView)
textView2 := views.NewTextView("Hi!")
textView2.SetStyle(tui.StyleDefault.Background(tcell.ColorBlue).Foreground(tcell.ColorYellow))
2022-05-04 11:13:18 +02:00
growView := views.NewGrowView(nil)
2022-04-02 15:09:52 +02:00
growView.SetStyle(tui.StyleDefault.Background(tcell.ColorGreen))
2022-05-04 11:13:18 +02:00
growView2 := views.NewGrowView(nil)
2022-04-02 15:09:52 +02:00
growView2.SetStyle(tui.StyleDefault.Background(tcell.ColorYellow))
2022-04-02 15:21:17 +02:00
separatorLayout := views.NewSeparatorLayout(tui.Vertical)
separatorLayout.AppendView(frameView, 1)
separatorLayout.AppendView(growView, 1)
separatorLayout.AppendView(textView2, 1)
2022-04-02 15:09:52 +02:00
2022-04-02 15:21:17 +02:00
screen, err := tui.NewScreen(separatorLayout)
2022-04-02 15:09:52 +02:00
if err != nil {
t.Error(err)
return
}
screen.KeyPressed = func(event *tui.KeyEvent) (consumed bool) {
textView.Text = event.When().String()
if event.Key() == tcell.KeyCtrlC {
screen.StopWithError(errors.New(fmt.Sprintf("key: %#v | rune: %s", event.Key(), string(event.Rune()))))
}
return true
}
err = screen.Start()
fmt.Println(err)
}
2022-04-02 15:21:17 +02:00
func TestBorderLayout(t *testing.T) {
2022-05-04 14:16:08 +02:00
textView := views.NewTextView("")
textView.SetStyle(tui.StyleDefault.Background(tcell.ColorBlue))
textView.MouseEvent = func(event *tui.MouseEvent) (consumed bool) {
if event.Button == tui.MouseButtonLeft {
textView.SetStyle(tui.StyleDefault.Background(tcell.ColorRed))
return true
}
return false
}
topView := views.NewConstrainView(textView, -1, -1)
2022-04-02 13:01:41 +02:00
topView.SetStyle(tui.StyleDefault.Background(tcell.ColorBlue))
2022-05-04 11:13:18 +02:00
bottomView := views.NewConstrainView(nil, 10, 10)
2022-04-02 13:01:41 +02:00
bottomView.SetStyle(tui.StyleDefault.Background(tcell.ColorRed))
2022-05-04 14:16:08 +02:00
bottomView.MouseEvent = func(event *tui.MouseEvent) (consumed bool) {
if event.Button == tui.MouseButtonLeft {
bottomView.SetStyle(tui.StyleDefault.Background(tcell.ColorBlue))
return true
}
return false
}
2022-04-02 13:01:41 +02:00
2022-05-04 11:13:18 +02:00
leftView := views.NewConstrainView(nil, 10, 10)
2022-04-02 13:01:41 +02:00
leftView.SetStyle(tui.StyleDefault.Background(tcell.ColorYellow))
2022-05-04 14:16:08 +02:00
leftView.MouseEvent = func(event *tui.MouseEvent) (consumed bool) {
if event.Button == tui.MouseButtonLeft {
leftView.SetStyle(tui.StyleDefault.Background(tcell.ColorGreen))
return true
}
return false
}
2022-04-02 13:01:41 +02:00
2022-05-04 11:13:18 +02:00
rightView := views.NewConstrainView(nil, 10, 10)
2022-04-02 13:01:41 +02:00
rightView.SetStyle(tui.StyleDefault.Background(tcell.ColorGreen))
2022-05-04 14:16:08 +02:00
rightView.MouseEvent = func(event *tui.MouseEvent) (consumed bool) {
if event.Button == tui.MouseButtonLeft {
rightView.SetStyle(tui.StyleDefault.Background(tcell.ColorYellow))
return true
}
return false
}
2022-04-02 13:01:41 +02:00
2022-05-04 11:13:18 +02:00
centerView := views.NewConstrainView(nil, 10, 10)
2022-04-02 13:01:41 +02:00
centerView.SetStyle(tui.StyleDefault.Background(tcell.ColorPurple))
2022-05-04 14:16:08 +02:00
centerView.MouseEvent = func(event *tui.MouseEvent) (consumed bool) {
if event.Button == tui.MouseButtonLeft {
centerView.SetStyle(tui.StyleDefault.Background(tcell.ColorOrange))
return true
}
return false
}
2022-04-02 13:01:41 +02:00
2022-04-02 15:21:17 +02:00
borderLayout := views.NewBorderLayout()
borderLayout.SetStyle(tui.StyleDefault.Background(tcell.ColorPurple))
borderLayout.SetView(topView, views.Top)
borderLayout.SetView(bottomView, views.Bottom)
borderLayout.SetView(leftView, views.Left)
borderLayout.SetView(rightView, views.Right)
borderLayout.SetView(centerView, views.Center)
2022-04-02 13:01:41 +02:00
2022-04-02 15:21:17 +02:00
screen, err := tui.NewScreen(borderLayout)
2022-04-02 13:01:41 +02:00
if err != nil {
t.Error(err)
return
}
screen.KeyPressed = func(event *tui.KeyEvent) (consumed bool) {
if event.Key() == tcell.KeyCtrlC {
screen.StopWithError(errors.New(fmt.Sprintf("key: %#v | rune: %s", event.Key(), string(event.Rune()))))
}
2022-04-01 20:10:51 +02:00
return true
2021-01-10 21:52:29 +01:00
}
2022-05-04 14:16:08 +02:00
screen.MouseEvent = func(event *tui.MouseEvent) (consumed bool) {
if event.Button == tui.MouseButtonLeft {
b := new(strings.Builder)
b.WriteString(textView.Text)
for slot, dim := range borderLayout.ViewDimensions {
if event.Position.In(dim) {
b.WriteString(fmt.Sprintf("%s: %s\n", slot, dim))
}
}
textView.Text = b.String()
}
return false
}
2021-01-10 21:52:29 +01:00
err = screen.Start()
fmt.Println(err)
}