tui/tests/screen_test.go

143 lines
3.9 KiB
Go
Raw Normal View History

2021-01-10 21:52:29 +01:00
package tui
import (
"errors"
"fmt"
"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"
)
2022-04-02 13:01:41 +02:00
func TestFlowGroup(t *testing.T) {
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))
flowGroup := views.NewFlowGroup(tui.Vertical)
flowGroup.AppendViews(marginView, growView, textView2)
constrainView := views.NewConstrainView(flowGroup)
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:09:52 +02:00
func TestSeparatorGroup(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()
growView.SetStyle(tui.StyleDefault.Background(tcell.ColorGreen))
growView2 := views.NewGrowView()
growView2.SetStyle(tui.StyleDefault.Background(tcell.ColorYellow))
separatorGroup := views.NewSeparatorGroup(tui.Vertical)
separatorGroup.AppendView(frameView, 1)
separatorGroup.AppendView(growView, 1)
separatorGroup.AppendView(textView2, 1)
screen, err := tui.NewScreen(separatorGroup)
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 13:01:41 +02:00
func TestBorderGroup(t *testing.T) {
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)
borderGroup := views.NewBorderGroup()
borderGroup.SetStyle(tui.StyleDefault.Background(tcell.ColorPurple))
borderGroup.SetView(topView, views.Top)
borderGroup.SetView(bottomView, views.Bottom)
borderGroup.SetView(leftView, views.Left)
borderGroup.SetView(rightView, views.Right)
borderGroup.SetView(centerView, views.Center)
screen, err := tui.NewScreen(borderGroup)
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)
}