runtest.go 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. // Copyright 2017 Zack Guo <zack.y.guo@gmail.com>. All rights reserved.
  2. // Use of this source code is governed by a MIT license that can
  3. // be found in the LICENSE file.
  4. package main
  5. import (
  6. "fmt"
  7. "os"
  8. "notabug.org/themusicgod1/termui"
  9. "notabug.org/themusicgod1/termui/debug"
  10. )
  11. func main() {
  12. // run as client
  13. if len(os.Args) > 1 {
  14. fmt.Print(debug.ConnectAndListen())
  15. return
  16. }
  17. // run as server
  18. go func() { panic(debug.ListenAndServe()) }()
  19. if err := termui.Init(); err != nil {
  20. panic(err)
  21. }
  22. defer termui.Close()
  23. //termui.UseTheme("helloworld")
  24. b := termui.NewBlock()
  25. b.Width = 20
  26. b.Height = 20
  27. b.Float = termui.AlignCenter
  28. b.BorderLabel = "[HELLO](fg-red,bg-white) [WORLD](fg-blue,bg-green)"
  29. termui.Render(b)
  30. termui.Handle("/sys", func(e termui.Event) {
  31. k, ok := e.Data.(termui.EvtKbd)
  32. debug.Logf("->%v\n", e)
  33. if ok && k.KeyStr == "q" {
  34. termui.StopLoop()
  35. }
  36. })
  37. termui.Handle(("/usr"), func(e termui.Event) {
  38. debug.Logf("->%v\n", e)
  39. })
  40. termui.Handle("/timer/1s", func(e termui.Event) {
  41. t := e.Data.(termui.EvtTimer)
  42. termui.SendCustomEvt("/usr/t", t.Count)
  43. if t.Count%2 == 0 {
  44. b.BorderLabel = "[HELLO](fg-red,bg-green) [WORLD](fg-blue,bg-white)"
  45. } else {
  46. b.BorderLabel = "[HELLO](fg-blue,bg-white) [WORLD](fg-red,bg-green)"
  47. }
  48. termui.Render(b)
  49. })
  50. termui.Loop()
  51. }