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 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)
|
|
|
|
}
|