grid.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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. sinps := (func() []float64 {
  14. n := 400
  15. ps := make([]float64, n)
  16. for i := range ps {
  17. ps[i] = 1 + math.Sin(float64(i)/5)
  18. }
  19. return ps
  20. })()
  21. sinpsint := (func() []int {
  22. ps := make([]int, len(sinps))
  23. for i, v := range sinps {
  24. ps[i] = int(100*v + 10)
  25. }
  26. return ps
  27. })()
  28. spark := ui.Sparkline{}
  29. spark.Height = 8
  30. spdata := sinpsint
  31. spark.Data = spdata[:100]
  32. spark.LineColor = ui.ColorCyan
  33. spark.TitleColor = ui.ColorWhite
  34. sp := ui.NewSparklines(spark)
  35. sp.Height = 11
  36. sp.BorderLabel = "Sparkline"
  37. lc := ui.NewLineChart()
  38. lc.BorderLabel = "braille-mode Line Chart"
  39. lc.Data = sinps
  40. lc.Height = 11
  41. lc.AxesColor = ui.ColorWhite
  42. lc.LineColor = ui.ColorYellow | ui.AttrBold
  43. gs := make([]*ui.Gauge, 3)
  44. for i := range gs {
  45. gs[i] = ui.NewGauge()
  46. //gs[i].LabelAlign = ui.AlignCenter
  47. gs[i].Height = 2
  48. gs[i].Border = false
  49. gs[i].Percent = i * 10
  50. gs[i].PaddingBottom = 1
  51. gs[i].BarColor = ui.ColorRed
  52. }
  53. ls := ui.NewList()
  54. ls.Border = false
  55. ls.Items = []string{
  56. "[1] Downloading File 1",
  57. "", // == \newline
  58. "[2] Downloading File 2",
  59. "",
  60. "[3] Uploading File 3",
  61. }
  62. ls.Height = 5
  63. par := ui.NewPar("<> This row has 3 columns\n<- Widgets can be stacked up like left side\n<- Stacked widgets are treated as a single widget")
  64. par.Height = 5
  65. par.BorderLabel = "Demonstration"
  66. // build layout
  67. ui.Body.AddRows(
  68. ui.NewRow(
  69. ui.NewCol(6, 0, sp),
  70. ui.NewCol(6, 0, lc)),
  71. ui.NewRow(
  72. ui.NewCol(3, 0, ls),
  73. ui.NewCol(3, 0, gs[0], gs[1], gs[2]),
  74. ui.NewCol(6, 0, par)))
  75. // calculate layout
  76. ui.Body.Align()
  77. ui.Render(ui.Body)
  78. ui.Handle("/sys/kbd/q", func(ui.Event) {
  79. ui.StopLoop()
  80. })
  81. ui.Handle("/timer/1s", func(e ui.Event) {
  82. t := e.Data.(ui.EvtTimer)
  83. i := t.Count
  84. if i > 103 {
  85. ui.StopLoop()
  86. return
  87. }
  88. for _, g := range gs {
  89. g.Percent = (g.Percent + 3) % 100
  90. }
  91. sp.Lines[0].Data = spdata[:100+i]
  92. lc.Data = sinps[2*i:]
  93. ui.Render(ui.Body)
  94. })
  95. ui.Handle("/sys/wnd/resize", func(e ui.Event) {
  96. ui.Body.Width = ui.TermWidth()
  97. ui.Body.Align()
  98. ui.Clear()
  99. ui.Render(ui.Body)
  100. })
  101. ui.Loop()
  102. }