dashboard.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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. // +build ignore
  5. package main
  6. import ui "notabug.org/themusicgod1/termui"
  7. import "math"
  8. func main() {
  9. if err := ui.Init(); err != nil {
  10. panic(err)
  11. }
  12. defer ui.Close()
  13. p := ui.NewPar(":PRESS q TO QUIT DEMO")
  14. p.Height = 3
  15. p.Width = 50
  16. p.TextFgColor = ui.ColorWhite
  17. p.BorderLabel = "Text Box"
  18. p.BorderFg = ui.ColorCyan
  19. p.Handle("/timer/1s", func(e ui.Event) {
  20. cnt := e.Data.(ui.EvtTimer)
  21. if cnt.Count%2 == 0 {
  22. p.TextFgColor = ui.ColorRed
  23. } else {
  24. p.TextFgColor = ui.ColorWhite
  25. }
  26. })
  27. strs := []string{"[0] themusicgod1/termui", "[1] editbox.go", "[2] interrupt.go", "[3] keyboard.go", "[4] output.go", "[5] random_out.go", "[6] dashboard.go", "[7] nsf/termbox-go"}
  28. list := ui.NewList()
  29. list.Items = strs
  30. list.ItemFgColor = ui.ColorYellow
  31. list.BorderLabel = "List"
  32. list.Height = 7
  33. list.Width = 25
  34. list.Y = 4
  35. g := ui.NewGauge()
  36. g.Percent = 50
  37. g.Width = 50
  38. g.Height = 3
  39. g.Y = 11
  40. g.BorderLabel = "Gauge"
  41. g.BarColor = ui.ColorRed
  42. g.BorderFg = ui.ColorWhite
  43. g.BorderLabelFg = ui.ColorCyan
  44. spark := ui.Sparkline{}
  45. spark.Height = 1
  46. spark.Title = "srv 0:"
  47. spdata := []int{4, 2, 1, 6, 3, 9, 1, 4, 2, 15, 14, 9, 8, 6, 10, 13, 15, 12, 10, 5, 3, 6, 1, 7, 10, 10, 14, 13, 6, 4, 2, 1, 6, 3, 9, 1, 4, 2, 15, 14, 9, 8, 6, 10, 13, 15, 12, 10, 5, 3, 6, 1, 7, 10, 10, 14, 13, 6, 4, 2, 1, 6, 3, 9, 1, 4, 2, 15, 14, 9, 8, 6, 10, 13, 15, 12, 10, 5, 3, 6, 1, 7, 10, 10, 14, 13, 6, 4, 2, 1, 6, 3, 9, 1, 4, 2, 15, 14, 9, 8, 6, 10, 13, 15, 12, 10, 5, 3, 6, 1, 7, 10, 10, 14, 13, 6}
  48. spark.Data = spdata
  49. spark.LineColor = ui.ColorCyan
  50. spark.TitleColor = ui.ColorWhite
  51. spark1 := ui.Sparkline{}
  52. spark1.Height = 1
  53. spark1.Title = "srv 1:"
  54. spark1.Data = spdata
  55. spark1.TitleColor = ui.ColorWhite
  56. spark1.LineColor = ui.ColorRed
  57. sp := ui.NewSparklines(spark, spark1)
  58. sp.Width = 25
  59. sp.Height = 7
  60. sp.BorderLabel = "Sparkline"
  61. sp.Y = 4
  62. sp.X = 25
  63. sinps := (func() []float64 {
  64. n := 220
  65. ps := make([]float64, n)
  66. for i := range ps {
  67. ps[i] = 1 + math.Sin(float64(i)/5)
  68. }
  69. return ps
  70. })()
  71. lc := ui.NewLineChart()
  72. lc.BorderLabel = "dot-mode Line Chart"
  73. lc.Data = sinps
  74. lc.Width = 50
  75. lc.Height = 11
  76. lc.X = 0
  77. lc.Y = 14
  78. lc.AxesColor = ui.ColorWhite
  79. lc.LineColor = ui.ColorRed | ui.AttrBold
  80. lc.Mode = "dot"
  81. bc := ui.NewBarChart()
  82. bcdata := []int{3, 2, 5, 3, 9, 5, 3, 2, 5, 8, 3, 2, 4, 5, 3, 2, 5, 7, 5, 3, 2, 6, 7, 4, 6, 3, 6, 7, 8, 3, 6, 4, 5, 3, 2, 4, 6, 4, 8, 5, 9, 4, 3, 6, 5, 3, 6}
  83. bclabels := []string{"S0", "S1", "S2", "S3", "S4", "S5"}
  84. bc.BorderLabel = "Bar Chart"
  85. bc.Width = 26
  86. bc.Height = 10
  87. bc.X = 51
  88. bc.Y = 0
  89. bc.DataLabels = bclabels
  90. bc.BarColor = ui.ColorGreen
  91. bc.NumColor = ui.ColorBlack
  92. lc1 := ui.NewLineChart()
  93. lc1.BorderLabel = "braille-mode Line Chart"
  94. lc1.Data = sinps
  95. lc1.Width = 26
  96. lc1.Height = 11
  97. lc1.X = 51
  98. lc1.Y = 14
  99. lc1.AxesColor = ui.ColorWhite
  100. lc1.LineColor = ui.ColorYellow | ui.AttrBold
  101. p1 := ui.NewPar("Hey!\nI am a borderless block!")
  102. p1.Border = false
  103. p1.Width = 26
  104. p1.Height = 2
  105. p1.TextFgColor = ui.ColorMagenta
  106. p1.X = 52
  107. p1.Y = 11
  108. draw := func(t int) {
  109. g.Percent = t % 101
  110. list.Items = strs[t%9:]
  111. sp.Lines[0].Data = spdata[:30+t%50]
  112. sp.Lines[1].Data = spdata[:35+t%50]
  113. lc.Data = sinps[t/2%220:]
  114. lc1.Data = sinps[2*t%220:]
  115. bc.Data = bcdata[t/2%10:]
  116. ui.Render(p, list, g, sp, lc, bc, lc1, p1)
  117. }
  118. ui.Handle("/sys/kbd/q", func(ui.Event) {
  119. ui.StopLoop()
  120. })
  121. ui.Handle("/timer/1s", func(e ui.Event) {
  122. t := e.Data.(ui.EvtTimer)
  123. draw(int(t.Count))
  124. })
  125. ui.Loop()
  126. }