initial commit

This commit is contained in:
Tordarus 2021-01-10 21:52:29 +01:00
commit c48904b98a
9 changed files with 235 additions and 0 deletions

13
draw_buffer.go Normal file
View File

@ -0,0 +1,13 @@
package tui
import (
"git.tordarus.net/tordarus/buf2d"
"github.com/gdamore/tcell"
)
func drawBuffer(scr tcell.Screen, buf *buf2d.Buffer) {
buf.Draw(func(x, y int, cn rune) {
scr.SetContent(x, y, cn, nil, tcell.StyleDefault)
})
scr.Show()
}

7
events.go Normal file
View File

@ -0,0 +1,7 @@
package tui
import "github.com/gdamore/tcell"
type Events interface {
OnKeyPressed(key tcell.Key)
}

73
screen.go Normal file
View File

@ -0,0 +1,73 @@
package tui
import (
"errors"
"fmt"
"git.tordarus.net/tordarus/buf2d"
"github.com/gdamore/tcell"
)
type Screen struct {
scr tcell.Screen
stopCh chan error
Root View
}
func NewScreen(root View) (*Screen, error) {
scr, err := tcell.NewScreen()
if err != nil {
return nil, err
}
s := &Screen{
Root: root,
scr: scr,
stopCh: make(chan error, 1),
}
go s.eventloop()
return s, nil
}
func (s *Screen) eventloop() {
for evt := s.scr.PollEvent(); evt != nil; evt = s.scr.PollEvent() {
switch event := evt.(type) {
case *tcell.EventResize:
go s.Redraw()
case *tcell.EventKey:
go func() {
s.Root.OnKeyPressed(event.Key())
s.Redraw()
}()
default:
s.StopWithError(errors.New(fmt.Sprintf("%#v", event)))
}
}
s.StopWithError(errors.New("unknown error occured"))
}
func (s *Screen) Start() error {
err := s.scr.Init()
if err != nil {
return err
}
s.Redraw()
return <-s.stopCh
}
func (s *Screen) Stop() {
s.StopWithError(nil)
}
func (s *Screen) StopWithError(err error) {
s.stopCh <- err
}
func (s *Screen) Redraw() {
w, h := s.scr.Size()
buf := buf2d.NewBuffer(w, h)
s.Root.Draw(buf)
drawBuffer(s.scr, buf)
}

27
tests/screen_test.go Normal file
View File

@ -0,0 +1,27 @@
package tui
import (
"errors"
"fmt"
"testing"
"tui"
"tui/views"
"github.com/gdamore/tcell"
)
func TestScreen(t *testing.T) {
eventView := views.NewEventView()
screen, err := tui.NewScreen(eventView)
if err != nil {
t.Error(err)
return
}
eventView.KeyPressed = func(key tcell.Key) {
screen.StopWithError(errors.New(fmt.Sprintf("%#v", key)))
}
err = screen.Start()
fmt.Println(err)
}

18
view.go Normal file
View File

@ -0,0 +1,18 @@
package tui
import (
"git.tordarus.net/tordarus/buf2d"
"github.com/gdamore/tcell"
)
type View interface {
Events
SetForeground(color tcell.Color)
Foreground() tcell.Color
SetBackground(color tcell.Color)
Background() tcell.Color
Draw(*buf2d.Buffer)
}

6
viewgroup.go Normal file
View File

@ -0,0 +1,6 @@
package tui
type ViewGroup interface {
View
Children() []*View
}

22
views/eventview.go Normal file
View File

@ -0,0 +1,22 @@
package views
import (
"tui"
"github.com/gdamore/tcell"
)
type EventView struct {
tui.ViewTmpl
KeyPressed func(key tcell.Key)
}
func (v *EventView) OnKeyPressed(key tcell.Key) {
if v.KeyPressed != nil {
v.KeyPressed(key)
}
}
func NewEventView() *EventView {
return &EventView{}
}

24
views/textview.go Normal file
View File

@ -0,0 +1,24 @@
package views
import (
"tui"
"git.tordarus.net/tordarus/buf2d"
)
type TextView struct {
tui.ViewTmpl
Text string
}
var _ tui.View = &TextView{}
func (v *TextView) Draw(buf *buf2d.Buffer) {
buf.WriteMultiLineString(v.Text, 0, 0)
}
func NewTextView(text string) *TextView {
return &TextView{
Text: text,
}
}

45
viewtmpl.go Normal file
View File

@ -0,0 +1,45 @@
package tui
import (
"git.tordarus.net/tordarus/buf2d"
"github.com/gdamore/tcell"
)
type ViewTmpl struct {
view View
foreground tcell.Color
background tcell.Color
}
var _ View = &ViewTmpl{}
func NewViewTmpl(v View) *ViewTmpl {
return &ViewTmpl{
view: v,
}
}
func (v *ViewTmpl) Draw(buf *buf2d.Buffer) {
buf.Fill(' ')
}
func (v *ViewTmpl) OnKeyPressed(key tcell.Key) {
}
func (v *ViewTmpl) Foreground() tcell.Color {
return v.foreground
}
func (v *ViewTmpl) SetForeground(color tcell.Color) {
v.foreground = color
}
func (v *ViewTmpl) Background() tcell.Color {
return v.background
}
func (v *ViewTmpl) SetBackground(color tcell.Color) {
v.background = color
}