ui.go 579 B

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. package ui
  2. import (
  3. "sync"
  4. )
  5. type Command struct {
  6. Code int
  7. Payload interface{}
  8. }
  9. const (
  10. QUIT = iota
  11. )
  12. type View interface {
  13. SetUi(*Ui)
  14. }
  15. type Ui struct {
  16. // the model always updates
  17. Updates *Broadcast
  18. // all views put their commands into this channel
  19. Cmds chan Command
  20. // all threads may add themself to this to wait for clean shutdown
  21. Wait *sync.WaitGroup
  22. }
  23. func NewUi(views ...View) *Ui {
  24. ui := &Ui{
  25. Updates: NewBroadcast(),
  26. Cmds: make(chan Command),
  27. Wait: new(sync.WaitGroup),
  28. }
  29. for _, v := range views {
  30. v.SetUi(ui)
  31. }
  32. return ui
  33. }