tui/tests/screen_test.go
2022-05-04 15:17:26 +02:00

280 lines
7.2 KiB
Go

package tui
import (
"errors"
"fmt"
"log"
"math/rand"
"os"
"strconv"
"testing"
"git.tordarus.net/Tordarus/tui"
"git.tordarus.net/Tordarus/tui/views"
"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
}
screen.MouseEvent = func(event *tui.MouseEvent) (consumed bool) {
//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
} else if event.Button == tui.MouseButtonMiddle {
panic("hi")
}
return false
}
err = screen.Start()
fmt.Println(err)
}
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)
}
}
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
}
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)
}
}
func TestFlowLayout(t *testing.T) {
textView := views.NewTextView("hello world!")
textView.SetStyle(tui.StyleDefault.Background(tcell.ColorRed).Foreground(tcell.ColorBlack))
textView.MouseEvent = func(event *tui.MouseEvent) (consumed bool) {
textView.Text = "hi"
return true
}
//borderView := views.NewBorderView(textView)
textView2 := views.NewTextView("Hi!")
textView2.SetStyle(tui.StyleDefault.Background(tcell.ColorBlue).Foreground(tcell.ColorYellow))
growView := views.NewGrowView(nil)
growView.SetStyle(tui.StyleDefault.Background(tcell.ColorGreen))
growView2 := views.NewGrowView(nil)
growView2.SetStyle(tui.StyleDefault.Background(tcell.ColorYellow))
flowLayout := views.NewFlowLayout(tui.Vertical)
flowLayout.AppendViews(textView, growView, textView2)
screen, err := tui.NewScreen(flowLayout)
if err != nil {
t.Error(err)
return
}
err = screen.Start()
fmt.Println(err)
}
func TestSeparatorLayout(t *testing.T) {
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))
growView := views.NewGrowView(nil)
growView.SetStyle(tui.StyleDefault.Background(tcell.ColorGreen))
growView2 := views.NewGrowView(nil)
growView2.SetStyle(tui.StyleDefault.Background(tcell.ColorYellow))
separatorLayout := views.NewSeparatorLayout(tui.Vertical)
separatorLayout.AppendView(frameView, 1)
separatorLayout.AppendView(growView, 1)
separatorLayout.AppendView(textView2, 1)
screen, err := tui.NewScreen(separatorLayout)
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)
}
func TestBorderLayout(t *testing.T) {
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)
topView.SetStyle(tui.StyleDefault.Background(tcell.ColorBlue))
bottomView := views.NewConstrainView(nil, 10, 10)
bottomView.SetStyle(tui.StyleDefault.Background(tcell.ColorRed))
bottomView.MouseEvent = func(event *tui.MouseEvent) (consumed bool) {
if event.Button == tui.MouseButtonLeft {
bottomView.SetStyle(tui.StyleDefault.Background(tcell.ColorBlue))
return true
}
return false
}
leftView := views.NewConstrainView(nil, 10, 10)
leftView.SetStyle(tui.StyleDefault.Background(tcell.ColorYellow))
leftView.MouseEvent = func(event *tui.MouseEvent) (consumed bool) {
if event.Button == tui.MouseButtonLeft {
leftView.SetStyle(tui.StyleDefault.Background(tcell.ColorGreen))
return true
}
return false
}
rightView := views.NewConstrainView(nil, 10, 10)
rightView.SetStyle(tui.StyleDefault.Background(tcell.ColorGreen))
rightView.MouseEvent = func(event *tui.MouseEvent) (consumed bool) {
if event.Button == tui.MouseButtonLeft {
rightView.SetStyle(tui.StyleDefault.Background(tcell.ColorYellow))
return true
}
return false
}
centerView := views.NewConstrainView(nil, 10, 10)
centerView.SetStyle(tui.StyleDefault.Background(tcell.ColorPurple))
centerView.MouseEvent = func(event *tui.MouseEvent) (consumed bool) {
if event.Button == tui.MouseButtonLeft {
centerView.SetStyle(tui.StyleDefault.Background(tcell.ColorOrange))
return true
}
return false
}
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)
screen, err := tui.NewScreen(borderLayout)
if err != nil {
t.Error(err)
return
}
err = screen.Start()
fmt.Println(err)
}