theme.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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. err := ui.Init()
  10. if err != nil {
  11. panic(err)
  12. }
  13. defer ui.Close()
  14. // Deprecated
  15. //ui.UseTheme("helloworld")
  16. ui.ColorMap = map[string]ui.Attribute{
  17. "fg": ui.ColorWhite,
  18. "bg": ui.ColorDefault,
  19. "border.fg": ui.ColorYellow,
  20. "label.fg": ui.ColorGreen,
  21. "par.fg": ui.ColorYellow,
  22. "par.label.bg": ui.ColorWhite,
  23. "gauge.bar.bg": ui.ColorCyan,
  24. "gauge.percent.fg": ui.ColorBlue,
  25. "barchart.bar.bg": ui.ColorRed,
  26. }
  27. p := ui.NewPar(":PRESS q TO QUIT DEMO")
  28. p.Height = 3
  29. p.Width = 50
  30. p.BorderLabel = "Text Box"
  31. 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"}
  32. list := ui.NewList()
  33. list.Items = strs
  34. list.BorderLabel = "List"
  35. list.Height = 7
  36. list.Width = 25
  37. list.Y = 4
  38. g := ui.NewGauge()
  39. g.Percent = 50
  40. g.Width = 50
  41. g.Height = 3
  42. g.Y = 11
  43. g.BorderLabel = "Gauge"
  44. spark := ui.NewSparkline()
  45. spark.Title = "srv 0:"
  46. 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}
  47. spark.Data = spdata
  48. spark1 := ui.NewSparkline()
  49. spark1.Title = "srv 1:"
  50. spark1.Data = spdata
  51. sp := ui.NewSparklines(spark, spark1)
  52. sp.Width = 25
  53. sp.Height = 7
  54. sp.BorderLabel = "Sparkline"
  55. sp.Y = 4
  56. sp.X = 25
  57. lc := ui.NewLineChart()
  58. sinps := (func() []float64 {
  59. n := 100
  60. ps := make([]float64, n)
  61. for i := range ps {
  62. ps[i] = 1 + math.Sin(float64(i)/4)
  63. }
  64. return ps
  65. })()
  66. lc.BorderLabel = "Line Chart"
  67. lc.Data = sinps
  68. lc.Width = 50
  69. lc.Height = 11
  70. lc.X = 0
  71. lc.Y = 14
  72. lc.Mode = "dot"
  73. bc := ui.NewBarChart()
  74. 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}
  75. bclabels := []string{"S0", "S1", "S2", "S3", "S4", "S5"}
  76. bc.BorderLabel = "Bar Chart"
  77. bc.Width = 26
  78. bc.Height = 10
  79. bc.X = 51
  80. bc.Y = 0
  81. bc.DataLabels = bclabels
  82. lc1 := ui.NewLineChart()
  83. lc1.BorderLabel = "Line Chart"
  84. rndwalk := (func() []float64 {
  85. n := 150
  86. d := make([]float64, n)
  87. for i := 1; i < n; i++ {
  88. if i < 20 {
  89. d[i] = d[i-1] + 0.01
  90. }
  91. if i > 20 {
  92. d[i] = d[i-1] - 0.05
  93. }
  94. }
  95. return d
  96. })()
  97. lc1.Data = rndwalk
  98. lc1.Width = 26
  99. lc1.Height = 11
  100. lc1.X = 51
  101. lc1.Y = 14
  102. p1 := ui.NewPar("Hey!\nI am a borderless block!")
  103. p1.Border = false
  104. p1.Width = 26
  105. p1.Height = 2
  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[t%10:]
  112. sp.Lines[1].Data = spdata[t/2%10:]
  113. lc.Data = sinps[t/2:]
  114. lc1.Data = rndwalk[t:]
  115. bc.Data = bcdata[t/2%10:]
  116. ui.Render(p, list, g, sp, lc, bc, lc1, p1)
  117. }
  118. ui.Render(p, list, g, sp, lc, bc, lc1, p1)
  119. ui.Handle("/sys/kbd/q", func(ui.Event) {
  120. ui.StopLoop()
  121. })
  122. ui.Handle("/timer/1s", func(e ui.Event) {
  123. t := e.Data.(ui.EvtTimer)
  124. draw(int(t.Count))
  125. })
  126. ui.Loop()
  127. }