tui/tests/screen_test.go

213 lines
5.6 KiB
Go
Raw Normal View History

2021-01-10 21:52:29 +01:00
package tui
import (
"errors"
"fmt"
"log"
"math/rand"
"os"
"strconv"
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
}
screen.MouseClicked = func(event *tui.MouseEvent) (consumed bool) {
2022-04-03 17:07:15 +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
}
return false
}
err = screen.Start()
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)
marginView.SetMargin(3, 1, 1, 0)
//borderView := views.NewBorderView(textView)
textView2 := views.NewTextView("Hi!")
textView2.SetStyle(tui.StyleDefault.Background(tcell.ColorBlue).Foreground(tcell.ColorYellow))
growView := views.NewGrowView()
growView.SetStyle(tui.StyleDefault.Background(tcell.ColorGreen))
growView2 := views.NewGrowView()
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-04-02 15:21:17 +02:00
constrainView := views.NewConstrainView(flowLayout)
2022-04-02 13:01:41 +02:00
constrainView.SetStyle(tui.StyleDefault.Background(tcell.ColorPurple))
constrainView.Constrain(-1, -1)
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))
growView := views.NewGrowView()
growView.SetStyle(tui.StyleDefault.Background(tcell.ColorGreen))
growView2 := views.NewGrowView()
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-04-02 13:01:41 +02:00
topView := views.NewConstrainView(nil)
topView.SetStyle(tui.StyleDefault.Background(tcell.ColorBlue))
topView.Constrain(10, 10)
bottomView := views.NewConstrainView(nil)
bottomView.SetStyle(tui.StyleDefault.Background(tcell.ColorRed))
bottomView.Constrain(10, 10)
leftView := views.NewConstrainView(nil)
leftView.SetStyle(tui.StyleDefault.Background(tcell.ColorYellow))
leftView.Constrain(10, 10)
rightView := views.NewConstrainView(nil)
rightView.SetStyle(tui.StyleDefault.Background(tcell.ColorGreen))
rightView.Constrain(10, 10)
centerView := views.NewConstrainView(nil)
centerView.SetStyle(tui.StyleDefault.Background(tcell.ColorPurple))
centerView.Constrain(10, 10)
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
}
err = screen.Start()
fmt.Println(err)
}