func NewView() *View { var view = View{} view.Header = ui.NewPar("Containers") view.Header.Border = false view.Header.Text = " Dockdash - Interactive realtime container inspector" view.Header.Height = 2 view.InfoBar = ui.NewPar("InfoBar") view.InfoBar.Border = false view.InfoBar.Text = "" view.InfoBar.Height = 2 view.NameList = createContainerList() view.NameList.BorderLabel = "Name" view.InfoList = createContainerList() view.InfoList.BorderLabel = "Image" view.CpuChart = ui.NewBarChart() view.CpuChart.Border = true view.CpuChart.BorderLabel = "%CPU" view.CpuChart.BorderFg = ui.ColorBlack view.CpuChart.Height = 8 view.MemChart = ui.NewBarChart() view.MemChart.Border = true view.MemChart.BorderLabel = "%MEM" view.MemChart.BorderFg = ui.ColorBlack view.MemChart.Height = 8 return &view }
func main() { err := termui.Init() if err != nil { panic(err) } defer termui.Close() termui.UseTheme("helloworld") bc := termui.NewBarChart() data := []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} bclabels := []string{"S0", "S1", "S2", "S3", "S4", "S5"} bc.Border.Label = "Bar Chart" bc.Data = data bc.Width = 26 bc.Height = 10 bc.DataLabels = bclabels bc.TextColor = termui.ColorGreen bc.BarColor = termui.ColorRed bc.NumColor = termui.ColorYellow termui.Render(bc) <-termui.EventCh() }
func main() { if err := termui.Init(); err != nil { panic(err) } defer termui.Close() bc := termui.NewBarChart() data := []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} bclabels := []string{"S0", "S1", "S2", "S3", "S4", "S5"} bc.BorderLabel = "Bar Chart" bc.Data = data bc.Width = 26 bc.Height = 10 bc.DataLabels = bclabels bc.TextColor = termui.ColorGreen bc.BarColor = termui.ColorRed bc.NumColor = termui.ColorYellow termui.Render(bc) termui.Handle("/sys/kbd/q", func(termui.Event) { termui.StopLoop() }) termui.Loop() }
func (summary *Summary) GetChart() *termui.BarChart { var bc *termui.BarChart keys := summary.getSortedKeys() data := summary.getData() if len(data) == 0 { return bc } var bcdata []int for _, key := range keys { bcdata = append(bcdata, data[key]) } bc = termui.NewBarChart() bc.DataLabels = keys bc.Data = bcdata bc.Border.Label = "cntbar" bc.Width = len(data) * 6 bc.Height = 10 bc.X = 3 bc.Y = 0 bc.BarColor = termui.ColorGreen bc.NumColor = termui.ColorBlack return bc }
func (u *UAAChart) Init(ui terminal.UI) { u.data = make([]int, 4) u.dataByIp = make([]map[string]int, 4) for i := 0; i < 4; i++ { u.dataByIp[i] = make(map[string]int) } u.cfUI = ui u.graph = termui.NewBarChart() u.graph.BorderLabel = "UAA Metrics" u.graph.Data = u.data u.graph.Width = 80 u.graph.Height = 20 u.graph.DataLabels = []string{"AuthSuccess", "AuthFailure", "PrnAuthFailure", "PwdFailure"} u.graph.TextColor = termui.ColorYellow u.graph.BarColor = termui.ColorCyan u.graph.NumColor = termui.ColorBlack u.graph.BarWidth = 17 // u.graph.BarGap = 5 u.validOrigins = []string{"uaa"} u.validMetricNames = []string{ "audit_service.user_authentication_count", "audit_service.user_authentication_failure_count", "audit_service.principal_authentication_failure_count", "audit_service.user_password_failures", } }
func (s *SinkTypeChart) Init(ui terminal.UI) { s.data = make([]int, 5) s.dataByIp = make([]map[string]int, 5) for i := 0; i < 5; i++ { s.dataByIp[i] = make(map[string]int) } s.cfUI = ui s.graph = termui.NewBarChart() s.graph.BorderLabel = "Number of Sinks" s.graph.Data = s.data s.graph.Width = 80 s.graph.Height = 20 s.graph.DataLabels = []string{"CntnrMetric", "SysLog", "Dump", "Websocket", "Firehose"} s.graph.TextColor = termui.ColorYellow s.graph.BarColor = termui.ColorYellow s.graph.NumColor = termui.ColorMagenta s.graph.BarWidth = 12 s.validOrigins = []string{"DopplerServer"} s.validMetricNames = []string{ "messageRouter.numberOfContainerMetricSinks", "messageRouter.numberOfSyslogSinks", "messageRouter.numberOfDumpSinks", "messageRouter.numberOfWebsocketSinks", "messageRouter.numberOfFirehoseSinks", } }
func newTerminalUI(appName string) error { if err := ui.Init(); err != nil { return err } ui.UseTheme("helloworld") // usage text usageText := fmt.Sprintf(`Show live statistics for [%s] :Press 'q' or 'ctrl-c' to exit :Press 'PageUp' to increase app instances :Press 'PageDown' to decrease app instances`, appName) usage := ui.NewPar(usageText) usage.Height = 12 usage.TextFgColor = ui.ColorWhite usage.Border.Label = "Usage" usage.Border.FgColor = ui.ColorCyan // summary text summary := ui.NewPar("") summary.Height = 12 summary.TextFgColor = ui.ColorRed summary.Border.Label = "Summary" summary.Border.FgColor = ui.ColorCyan // cpu sparklines data := [400]int{} line := ui.NewSparkline() line.Data = data[:] line.Height = 4 line.LineColor = colors[0] cpu := ui.NewSparklines(line) cpu.Height = 7 cpu.Border.Label = "CPU Usage" // memory gauges mem := make([]*ui.Gauge, 1) for i := range mem { mem[i] = ui.NewGauge() mem[i].Percent = 0 mem[i].Height = 5 } // disk bars disk := ui.NewBarChart() disk.Border.Label = "Disk Usage (in MB)" disk.Data = []int{0, 0, 0, 0, 0, 0, 0, 0} disk.Height = 12 disk.BarWidth = 10 disk.DataLabels = []string{"I: 0", "I: 1", "I: 2", "I: 3", "I: 4", "I: 5", "I: 6", "I: 7"} disk.TextColor = ui.ColorWhite disk.BarColor = ui.ColorYellow disk.NumColor = ui.ColorWhite term = &TerminalUI{ui.Body, usage, summary, cpu, mem, disk} return nil }
func main() { err := ui.Init() if err != nil { panic(err) } defer ui.Close() ui.UseTheme("helloworld") p := ui.NewPar(":PRESS q TO QUIT DEMO") p.Height = 3 p.Width = 50 p.Border.Label = "Text Box" strs := []string{"[0] gizak/termui", "[1] editbox.go", "[2] iterrupt.go", "[3] keyboard.go", "[4] output.go", "[5] random_out.go", "[6] dashboard.go", "[7] nsf/termbox-go"} list := ui.NewList() list.Items = strs list.Border.Label = "List" list.Height = 7 list.Width = 25 list.Y = 4 g := ui.NewGauge() g.Percent = 50 g.Width = 50 g.Height = 3 g.Y = 11 g.Border.Label = "Gauge" spark := ui.NewSparkline() spark.Title = "srv 0:" 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} spark.Data = spdata spark1 := ui.NewSparkline() spark1.Title = "srv 1:" spark1.Data = spdata sp := ui.NewSparklines(spark, spark1) sp.Width = 25 sp.Height = 7 sp.Border.Label = "Sparkline" sp.Y = 4 sp.X = 25 lc := ui.NewLineChart() sinps := (func() []float64 { n := 100 ps := make([]float64, n) for i := range ps { ps[i] = 1 + math.Sin(float64(i)/4) } return ps })() lc.Border.Label = "Line Chart" lc.Data = sinps lc.Width = 50 lc.Height = 11 lc.X = 0 lc.Y = 14 lc.Mode = "dot" bc := ui.NewBarChart() 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} bclabels := []string{"S0", "S1", "S2", "S3", "S4", "S5"} bc.Border.Label = "Bar Chart" bc.Width = 26 bc.Height = 10 bc.X = 51 bc.Y = 0 bc.DataLabels = bclabels lc1 := ui.NewLineChart() lc1.Border.Label = "Line Chart" rndwalk := (func() []float64 { n := 150 d := make([]float64, n) for i := 1; i < n; i++ { if i < 20 { d[i] = d[i-1] + 0.01 } if i > 20 { d[i] = d[i-1] - 0.05 } } return d })() lc1.Data = rndwalk lc1.Width = 26 lc1.Height = 11 lc1.X = 51 lc1.Y = 14 p1 := ui.NewPar("Hey!\nI am a borderless block!") p1.HasBorder = false p1.Width = 26 p1.Height = 2 p1.X = 52 p1.Y = 11 draw := func(t int) { g.Percent = t % 101 list.Items = strs[t%9:] sp.Lines[0].Data = spdata[t%10:] sp.Lines[1].Data = spdata[t/2%10:] lc.Data = sinps[t/2:] lc1.Data = rndwalk[t:] bc.Data = bcdata[t/2%10:] ui.Render(p, list, g, sp, lc, bc, lc1, p1) } evt := ui.EventCh() i := 0 for { select { case e := <-evt: if e.Type == ui.EventKey && e.Ch == 'q' { return } default: draw(i) i++ if i == 102 { return } time.Sleep(time.Second / 2) } } }
func main() { err := ui.Init() if err != nil { panic(err) } defer ui.Close() p := ui.NewPar(":PRESS q TO QUIT DEMO") p.Height = 3 p.Width = 50 p.TextFgColor = ui.ColorWhite p.Border.Label = "Text Box" p.Border.FgColor = ui.ColorCyan strs := []string{"[0] gizak/termui", "[1] editbox.go", "[2] iterrupt.go", "[3] keyboard.go", "[4] output.go", "[5] random_out.go", "[6] dashboard.go", "[7] nsf/termbox-go"} list := ui.NewList() list.Items = strs list.ItemFgColor = ui.ColorYellow list.Border.Label = "List" list.Height = 7 list.Width = 25 list.Y = 4 g := ui.NewGauge() g.Percent = 50 g.Width = 50 g.Height = 3 g.Y = 11 g.Border.Label = "Gauge" g.BarColor = ui.ColorRed g.Border.FgColor = ui.ColorWhite g.Border.LabelFgColor = ui.ColorCyan spark := ui.Sparkline{} spark.Height = 1 spark.Title = "srv 0:" 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} spark.Data = spdata spark.LineColor = ui.ColorCyan spark.TitleColor = ui.ColorWhite spark1 := ui.Sparkline{} spark1.Height = 1 spark1.Title = "srv 1:" spark1.Data = spdata spark1.TitleColor = ui.ColorWhite spark1.LineColor = ui.ColorRed sp := ui.NewSparklines(spark, spark1) sp.Width = 25 sp.Height = 7 sp.Border.Label = "Sparkline" sp.Y = 4 sp.X = 25 sinps := (func() []float64 { n := 220 ps := make([]float64, n) for i := range ps { ps[i] = 1 + math.Sin(float64(i)/5) } return ps })() lc := ui.NewLineChart() lc.Border.Label = "dot-mode Line Chart" lc.Data = sinps lc.Width = 50 lc.Height = 11 lc.X = 0 lc.Y = 14 lc.AxesColor = ui.ColorWhite lc.LineColor = ui.ColorRed | ui.AttrBold lc.Mode = "dot" bc := ui.NewBarChart() 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} bclabels := []string{"S0", "S1", "S2", "S3", "S4", "S5"} bc.Border.Label = "Bar Chart" bc.Width = 26 bc.Height = 10 bc.X = 51 bc.Y = 0 bc.DataLabels = bclabels bc.BarColor = ui.ColorGreen bc.NumColor = ui.ColorBlack lc1 := ui.NewLineChart() lc1.Border.Label = "braille-mode Line Chart" lc1.Data = sinps lc1.Width = 26 lc1.Height = 11 lc1.X = 51 lc1.Y = 14 lc1.AxesColor = ui.ColorWhite lc1.LineColor = ui.ColorYellow | ui.AttrBold p1 := ui.NewPar("Hey!\nI am a borderless block!") p1.HasBorder = false p1.Width = 26 p1.Height = 2 p1.TextFgColor = ui.ColorMagenta p1.X = 52 p1.Y = 11 draw := func(t int) { g.Percent = t % 101 list.Items = strs[t%9:] sp.Lines[0].Data = spdata[t%10:] sp.Lines[1].Data = spdata[t/2%10:] lc.Data = sinps[t/2:] lc1.Data = sinps[2*t:] bc.Data = bcdata[t/2%10:] ui.Render(p, list, g, sp, lc, bc, lc1, p1) } evt := ui.EventCh() i := 0 for { select { case e := <-evt: if e.Type == ui.EventKey && e.Ch == 'q' { return } default: draw(i) i++ if i == 102 { return } time.Sleep(time.Second / 2) } } }
func main() { if err := ui.Init(); err != nil { panic(err) } defer ui.Close() p := ui.NewPar(":PRESS q TO QUIT DEMO") p.Height = 3 p.Width = 50 p.TextFgColor = ui.ColorWhite p.BorderLabel = "Text Box" p.BorderFg = ui.ColorCyan p.Handle("/timer/1s", func(e ui.Event) { cnt := e.Data.(ui.EvtTimer) if cnt.Count%2 == 0 { p.TextFgColor = ui.ColorRed } else { p.TextFgColor = ui.ColorWhite } }) strs := []string{"[0] gizak/termui", "[1] editbox.go", "[2] iterrupt.go", "[3] keyboard.go", "[4] output.go", "[5] random_out.go", "[6] dashboard.go", "[7] nsf/termbox-go"} list := ui.NewList() list.Items = strs list.ItemFgColor = ui.ColorYellow list.BorderLabel = "List" list.Height = 7 list.Width = 25 list.Y = 4 g := ui.NewGauge() g.Percent = 50 g.Width = 50 g.Height = 3 g.Y = 11 g.BorderLabel = "Gauge" g.BarColor = ui.ColorRed g.BorderFg = ui.ColorWhite g.BorderLabelFg = ui.ColorCyan spark := ui.Sparkline{} spark.Height = 1 spark.Title = "srv 0:" 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} spark.Data = spdata spark.LineColor = ui.ColorCyan spark.TitleColor = ui.ColorWhite spark1 := ui.Sparkline{} spark1.Height = 1 spark1.Title = "srv 1:" spark1.Data = spdata spark1.TitleColor = ui.ColorWhite spark1.LineColor = ui.ColorRed sp := ui.NewSparklines(spark, spark1) sp.Width = 25 sp.Height = 7 sp.BorderLabel = "Sparkline" sp.Y = 4 sp.X = 25 sinps := (func() []float64 { n := 220 ps := make([]float64, n) for i := range ps { ps[i] = 1 + math.Sin(float64(i)/5) } return ps })() lc := ui.NewLineChart() lc.BorderLabel = "dot-mode Line Chart" lc.Data = sinps lc.Width = 50 lc.Height = 11 lc.X = 0 lc.Y = 14 lc.AxesColor = ui.ColorWhite lc.LineColor = ui.ColorRed | ui.AttrBold lc.Mode = "dot" bc := ui.NewBarChart() 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} bclabels := []string{"S0", "S1", "S2", "S3", "S4", "S5"} bc.BorderLabel = "Bar Chart" bc.Width = 26 bc.Height = 10 bc.X = 51 bc.Y = 0 bc.DataLabels = bclabels bc.BarColor = ui.ColorGreen bc.NumColor = ui.ColorBlack lc1 := ui.NewLineChart() lc1.BorderLabel = "braille-mode Line Chart" lc1.Data = sinps lc1.Width = 26 lc1.Height = 11 lc1.X = 51 lc1.Y = 14 lc1.AxesColor = ui.ColorWhite lc1.LineColor = ui.ColorYellow | ui.AttrBold p1 := ui.NewPar("Hey!\nI am a borderless block!") p1.Border = false p1.Width = 26 p1.Height = 2 p1.TextFgColor = ui.ColorMagenta p1.X = 52 p1.Y = 11 draw := func(t int) { g.Percent = t % 101 list.Items = strs[t%9:] sp.Lines[0].Data = spdata[:30+t%50] sp.Lines[1].Data = spdata[:35+t%50] lc.Data = sinps[t/2%220:] lc1.Data = sinps[2*t%220:] bc.Data = bcdata[t/2%10:] ui.Render(p, list, g, sp, lc, bc, lc1, p1) } ui.Handle("/sys/kbd/q", func(ui.Event) { ui.StopLoop() }) ui.Handle("/timer/1s", func(e ui.Event) { t := e.Data.(ui.EvtTimer) draw(int(t.Count)) }) ui.Loop() }
func main() { err := termui.Init() if err != nil { panic(err) } defer termui.Close() termui.UseTheme("helloworld") header := termui.NewPar("Press q to quit, Press j or k to switch tabs") header.Height = 1 header.Width = 50 header.HasBorder = false header.TextBgColor = termui.ColorBlue tab1 := extra.NewTab("pierwszy") par2 := termui.NewPar("Press q to quit\nPress j or k to switch tabs\n") par2.Height = 5 par2.Width = 37 par2.Y = 0 par2.Border.Label = "Keys" par2.Border.FgColor = termui.ColorYellow tab1.AddBlocks(par2) tab2 := extra.NewTab("drugi") bc := termui.NewBarChart() data := []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} bclabels := []string{"S0", "S1", "S2", "S3", "S4", "S5"} bc.Border.Label = "Bar Chart" bc.Data = data bc.Width = 26 bc.Height = 10 bc.DataLabels = bclabels bc.TextColor = termui.ColorGreen bc.BarColor = termui.ColorRed bc.NumColor = termui.ColorYellow tab2.AddBlocks(bc) tab3 := extra.NewTab("trzeci") tab4 := extra.NewTab("żółw") tab5 := extra.NewTab("four") tab6 := extra.NewTab("five") tabpane := extra.NewTabpane() tabpane.Y = 1 tabpane.Width = 30 tabpane.HasBorder = true tabpane.SetTabs(*tab1, *tab2, *tab3, *tab4, *tab5, *tab6) evt := termui.EventCh() termui.Render(header, tabpane) for { select { case e := <-evt: if e.Type == termui.EventKey { switch e.Ch { case 'q': return case 'j': tabpane.SetActiveLeft() termui.Render(header, tabpane) case 'k': tabpane.SetActiveRight() termui.Render(header, tabpane) } } } } }
func createUi(state *render.RenderState) { err := ui.Init() if err != nil { panic(err) } defer ui.Close() ui.UseTheme("helloworld") bc := ui.NewBarChart() data := []int{} bclabels := []string{} bc.Border.Label = "SumoCLI" bc.Data = data //bc.Width = 26 bc.Height = ui.TermHeight() - 10 bc.DataLabels = bclabels bc.TextColor = ui.ColorGreen bc.BarColor = ui.ColorRed bc.NumColor = ui.ColorYellow // build layout ui.Body.AddRows( ui.NewRow( ui.NewCol(12, 0, bc))) // calculate layout ui.Body.Align() done := make(chan bool) redraw := make(chan bool) update := func() error { //fmt.Println("updating") bars := render.Columns(*state.Messages) columns := render.ColumnNames(bars) query, ok := (*state.Meta)["_queryString"] if ok && query.(string) != bc.Border.Label { bc.Border.Label = query.(string) } dataCol := render.NumericColumn(columns) data := []int{} labels := []string{} extractor := render.LabelExtractor(columns) for _, msg := range *state.Messages { labels = append(labels, extractor(msg)) // go is the worst floatVal, _ := strconv.ParseFloat(fmt.Sprint(msg[dataCol]), 64) rowData := int(floatVal) data = append(data, rowData) } bc.Data = data var numCols int if len(data) > 0 { numCols = len(data) } else { numCols = 1 } newBarWidth := (ui.TermWidth() - 10) / numCols if int(newBarWidth) != int(bc.BarWidth) { bc.BarWidth = newBarWidth } if len(labels) != len(bc.DataLabels) { bc.DataLabels = labels } redraw <- true return nil } state.Flush = update evt := ui.EventCh() ui.Render(ui.Body) go update() for { select { case e := <-evt: if e.Type == ui.EventKey && e.Ch == 'q' { return } if e.Type == ui.EventResize { ui.Body.Width = ui.TermWidth() ui.Body.Align() go func() { redraw <- true }() } case <-done: return case <-redraw: ui.Render(ui.Body) } } }
func main() { options := Options{} parser := flags.NewParser(&options, flags.Default) args, err := parser.Parse() if err != nil { os.Exit(1) return } if options.Version { fmt.Println(version) os.Exit(0) } // Figure out the log level numVerbose := len(options.Verbose) if numVerbose > len(logLevels) { numVerbose = len(logLevels) - 1 } logLevel := logLevels[numVerbose] //logger = golog.New(os.Stderr, logLevel) _ = logLevel logger = log.NullLogger if len(args) < 1 { fail(1, "Missing arg: PATH") } if err := ui.Init(); err != nil { panic(err) } defer ui.Close() g := ui.NewBarChart() g.BorderLabel = "Womps" g.Width = 180 g.Height = 30 //g.SetMax(100) ui.Render(g) setter := func(v []int) { g.Data = v g.DataLabels = make([]string, len(v)) ui.Render(g) } vis := BasicVisualizer(setter) ui.Handle("/sys/kbd", func(ui.Event) { // Any key to quit ui.StopLoop() }) go ui.Loop() err = PlayPath(args[0], vis) if err != nil { fail(2, "Play error: %s", err) } logger.Info("Done.") os.Exit(0) }
func (w *RPSWidget) Init() { w.server = gen.MonitorServer(w.addr) bc := termui.NewBarChart() data := []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} bclabels := []string{"INS", "QRY", "UPD", "DEL", "GTM", "", "CPU", "MEM", "DSK"} bc.Border.Label = "> " + w.title + " <" bc.Border.FgColor = termui.ColorCyan bc.Data = data bc.Width = 30 bc.Height = 10 bc.SetMax(100) bc.DataLabels = bclabels // bc.BgColor = termui.ColorBlack bc.TextColor = termui.ColorYellow bc.BarColor = termui.ColorRed bc.NumColor = termui.ColorRed w.bc = bc go func() { for { // TODO: refresh data here via channel or something var data []int stats := w.server.GetStats() for i := 0; i < len(bclabels); i++ { if bclabels[i] != "" { switch bclabels[i] { case "CPU": c := stats.P_CPU if stats.OPS > 100 && c > 10 { c = int(float64(stats.OPS) * (float64(c) / 100.0)) } data = append(data, c) case "MEM": c := stats.P_MEM if stats.OPS > 100 { c = int(float64(stats.OPS) * (float64(c) / 100.0)) } data = append(data, c) case "INS": data = append(data, stats.OPS_insert) case "QRY": data = append(data, stats.OPS_query) case "UPD": data = append(data, stats.OPS_update) case "GTM": data = append(data, stats.OPS_getmore) case "DEL": data = append(data, stats.OPS_delete) case "DSK": c := (stats.P_DISK_dbs + stats.P_DISK_journal) / 2 if stats.OPS > 100 { c = int(float64(stats.OPS) * (float64(c) / 100.0)) } data = append(data, c) default: data = append(data, rand.Intn(100)) } } else { data = append(data, 0) } } w.bc.Data = data time.Sleep(1 * time.Second) } }() }
func main() { err := tui.Init() if err != nil { panic(err) } defer tui.Close() /////////// // // Create UI components // /////////// // Header pr_th := 3 pr_title := tui.NewPar("Text Console User Interfaces") pr_title.Width = tui.TermWidth() pr_title.Height = pr_th pr_title.BorderFg = tui.ColorBlue // Footer g_h := 5 g := tui.NewGauge() g.Percent = 1 g.Width = tui.TermWidth() g.Height = g_h g.Y = tui.TermHeight() - g_h g.BorderLabel = "Progress" g.Label = "{{percent}} - Start!" g.LabelAlign = tui.AlignRight g.BarColor = tui.ColorGreen g.BorderFg = tui.ColorBlue g.BorderLabelFg = tui.ColorWhite // Slide 1 txtlst1 := "Introduction\n\no Myself\n\no Interests in Go" se1_1 := tui.NewPar(txtlst1) se1_1.Width = tui.TermWidth() se1_1.Height = (tui.TermHeight() / 2) - (pr_th + g_h) se1_1.Y = pr_th se1_2 := tui.NewPar("") se1_2.Width = tui.TermWidth() se1_2.Height = (tui.TermHeight() / 2) - (pr_th + g_h) se1_2.Y = pr_th + se1_1.Height // Slide 2 txtlst2 := "The Termui Library\n\no Console library UI\n\n" txtlst2 += "o A widget library for dashboard building in the terminal\n\n" txtlst2 += "o Cross Platform\n\n o Runs on Linux, OSX, and Windows" se2_1 := tui.NewPar(txtlst2) se2_1.Width = tui.TermWidth() se2_1.Height = (tui.TermHeight() / 2) - (pr_th + g_h) se2_1.Y = pr_th // Slide 3 txtlst3 := "More Info\n\n" txtlst3 += "o Built on top of termbox library\n\n" txtlst3 += "o Inherits handlers, events, and cross platform compatibility" se3_1 := tui.NewPar(txtlst3) se3_1.Width = tui.TermWidth() se3_1.Height = (tui.TermHeight() / 2) - (pr_th + g_h) se3_1.Y = pr_th // Slide 4 txtlst4 := "Features\n\n" txtlst4 += "o Multiple widgets available\n\n" txtlst4 += "o Automatic grid layout\n\n" txtlst4 += "o 多言語可能 (multi-lang possible)" se4_1 := tui.NewPar(txtlst4) se4_1.Width = tui.TermWidth() se4_1.Height = (tui.TermHeight() / 2) - (pr_th + g_h) se4_1.Y = pr_th // Slide 5 txtlst5 := "Widget Features\n\n" txtlst5 += "o Can be surrounded by borders\n\n" txtlst5 += "o Can have labels associated with it\n\n" txtlst5 += "o Borders can also have labels\n\n" txtlst5 += "o Color" se5_1 := tui.NewPar(txtlst5) se5_1.Width = tui.TermWidth() se5_1.Height = (tui.TermHeight() / 2) - (pr_th + g_h) se5_1.Y = pr_th // Slide 6 txtlst6 := "Widgets - Par\n\no Par - aka Textbox\n\n" txtlst6 += "o Basic textbox widget\n\n" txtlst6 += " p := termui.NewPar(\"World\")\n" txtlst6 += " p.BorderLabel(\"Hello\")" se6_1 := tui.NewPar(txtlst6) se6_1.Width = tui.TermWidth() se6_1.Height = (tui.TermHeight() / 2) - (pr_th + g_h) se6_1.Y = pr_th se6_2 := tui.NewPar("World") se6_2.BorderLabel = "Hello" se6_2.BorderFg = tui.ColorYellow se6_2.BorderLabelFg = tui.ColorWhite se6_2.Width = tui.TermWidth() se6_2.Height = (tui.TermHeight() / 2) - (pr_th + g_h) se6_2.Y = pr_th + se6_1.Height // Slide 7 txtlst7 := "Widgets - Lists\n\no List - A text list\n\n" txtlst7 += "o Text Lists\n\n" txtlst7 += " tl := termui.NewList()\n" txtlst7 += " tl.Items = textlist\n" se7_2lst := []string{ "* List Elems", "* Are Just", "* Lists of", "* Strings", "* [and support](fg-blue)", "* [colors](fg-green,bg-black)"} se7_1 := tui.NewPar(txtlst7) se7_1.Width = tui.TermWidth() se7_1.Height = (tui.TermHeight() / 2) - (pr_th + g_h) se7_1.Y = pr_th se7_2 := tui.NewList() se7_2.Items = se7_2lst se7_2.BorderFg = tui.ColorYellow se7_2.BorderLabelFg = tui.ColorWhite se7_2.Width = tui.TermWidth() se7_2.Height = (tui.TermHeight() / 2) - (pr_th + g_h) se7_2.Y = pr_th + se7_1.Height // Slide 8 txtlst8 := "Widgets - Line Charts\n\n" txtlst8 += "o Draw linecharts\n\n" txtlst8 += " lc := termui.NewLineChart()\n" txtlst8 += " lc.Data = cosdata" se8_1 := tui.NewPar(txtlst8) se8_1.Width = tui.TermWidth() se8_1.Height = (tui.TermHeight() / 2) - (pr_th + g_h) se8_1.Y = pr_th se8_2 := tui.NewLineChart() se8_2.Data = cosData() se8_2.BorderFg = tui.ColorYellow se8_2.BorderLabelFg = tui.ColorWhite se8_2.Width = tui.TermWidth() se8_2.Height = (tui.TermHeight() / 2) - (pr_th + g_h) se8_2.Y = pr_th + se8_1.Height // Slide 9 txtlst9 := "Widgets - Bar Charts\n\n" txtlst9 += "o Draw bar charts\n\n" txtlst9 += " data := []int{4, 5, 6, 7, 8, 6, 5}\n" txtlst9 += " bc := termui.NewBarChart()\n" txtlst9 += " bc.Data = data" se9_1 := tui.NewPar(txtlst9) se9_1.Width = tui.TermWidth() se9_1.Height = (tui.TermHeight() / 2) - (pr_th + g_h) se9_1.Y = pr_th se9_2 := tui.NewBarChart() se9_2.Data = []int{4, 5, 6, 7, 8, 6, 5} se9_2.DataLabels = []string{"S0", "S1", "S2", "S3", "S4", "S5", "S6", "S7"} se9_2.BorderFg = tui.ColorYellow se9_2.BorderLabelFg = tui.ColorWhite se9_2.Width = tui.TermWidth() se9_2.Height = (tui.TermHeight() / 2) - (pr_th + g_h) se9_2.Y = pr_th + se9_1.Height // Slide 10 txtlst10 := "Widgets - Sparklines\n\n" txtlst10 += "o Draw sparklines\n\n" txtlst10 += " data := []int{4, 5, 6, 7, 8, 6, 5}\n" txtlst10 += " sp := termui.NewSparkline()\n" txtlst10 += " sp.Data = data\n" txtlst10 += " spl := termui.NewSparklines(sp)" se10_1 := tui.NewPar(txtlst10) se10_1.Width = tui.TermWidth() se10_1.Height = (tui.TermHeight() / 2) - (pr_th + g_h) se10_1.Y = pr_th sp10_2 := tui.NewSparkline() sp10_2.Data = []int{4, 5, 6, 7, 8, 6, 5} sp10_2.LineColor = tui.ColorRed se10_2 := tui.NewSparklines(sp10_2) se10_2.Width = tui.TermWidth() se10_2.Height = (tui.TermHeight() / 2) - (pr_th + g_h) se10_2.Y = pr_th + se10_1.Height // Slide 11 txtlst11 := "General Workflow\n\n" txtlst11 += "o Setup\n\no Create & Setup UI elems\n\n" txtlst11 += "o Setup handlers\n\nLoop" se11_1 := tui.NewPar(txtlst11) se11_1.Width = tui.TermWidth() se11_1.Height = (tui.TermHeight() / 2) - (pr_th + g_h) se11_1.Y = pr_th txtlst11_2 := " termui.Init()\n" txtlst11_2 += " p := termui.NewPar(\"Hello World\")\n" txtlst11_2 += " termui.Render(p)\n" txtlst11_2 += " termui.Handle(\"/sys/kbd/Q\", func(termui.Event) {\n" txtlst11_2 += " termui.StopLoop() })\n" txtlst11_2 += " termui.Loop()" se11_2 := tui.NewPar(txtlst11_2) se11_2.BorderFg = tui.ColorYellow se11_2.BorderLabelFg = tui.ColorWhite se11_2.Width = tui.TermWidth() se11_2.Height = (tui.TermHeight() / 2) - (pr_th + g_h) se11_2.Y = pr_th + se11_1.Height // Slide 12 txtlst12 := "Extra Notes\n\n" txtlst12 += "o V1 vs V2\n\n" txtlst12 += "o Timers\n\n" se12_1 := tui.NewPar(txtlst12) se12_1.Width = tui.TermWidth() se12_1.Height = (tui.TermHeight() / 2) - (pr_th + g_h) se12_1.Y = pr_th se12_2 := tui.NewPar("") se12_2.Border = false se12_2.Width = tui.TermWidth() se12_2.Height = (tui.TermHeight() / 2) - (pr_th + g_h) se12_2.Y = pr_th + se12_1.Height /////////// // // Build the slideshow // /////////// slides := NewSlideShow(pr_title, g) slides.AddSlide("start", []tui.Bufferer{}) slides.AddSlide("Intro", []tui.Bufferer{se1_1}) slides.AddSlide("Termui", []tui.Bufferer{se2_1}) slides.AddSlide("More Info", []tui.Bufferer{se3_1}) slides.AddSlide("Features", []tui.Bufferer{se4_1}) slides.AddSlide("WidgetFeatures", []tui.Bufferer{se5_1}) slides.AddSlide("Par()", []tui.Bufferer{se6_1, se6_2}) slides.AddSlide("List()", []tui.Bufferer{se7_1, se7_2}) slides.AddSlide("LineChart()", []tui.Bufferer{se8_1, se8_2}) slides.AddSlide("BarChart()", []tui.Bufferer{se9_1, se9_2}) slides.AddSlide("Sparkline()", []tui.Bufferer{se10_1, se10_2}) slides.AddSlide("quickdemo", []tui.Bufferer{se11_1, se11_2}) slides.AddSlide("gotchas", []tui.Bufferer{se12_1, se12_2}) slides_num := slides.Length() slides_idx := 0 maxttl := 360 ttl := maxttl draw := func() { tui.Render(slides.At(slides_idx).Widgets...) } tui.Render(pr_title, g) tui.Handle("/sys/kbd/Q", func(tui.Event) { tui.StopLoop() }) tui.Handle("/sys/kbd/<left>", func(tui.Event) { }) tui.Handle("/sys/kbd/<right>", func(tui.Event) { ttl = maxttl slides_idx++ if slides_idx > (slides_num - 1) { slides_idx = 0 } g.Percent = calcPercent(slides_idx, slides_num) lbl := "Progress - " + strconv.Itoa(g.Percent) + "%" + " TTL: " + strconv.Itoa(ttl) g.BorderLabel = lbl g.Label = "{{percent}} - " + slides.At(slides_idx).Title }) tui.Handle("/sys/kbd/<space>", func(tui.Event) { ttl = maxttl slides_idx++ if slides_idx > (slides_num - 1) { slides_idx = 0 } g.Percent = calcPercent(slides_idx, slides_num) lbl := "Progress - " + strconv.Itoa(g.Percent) + "%" + " TTL: " + strconv.Itoa(ttl) g.BorderLabel = lbl g.Label = "{{percent}} - " + slides.At(slides_idx).Title }) tui.Handle("/timer/1s", func(e tui.Event) { ttl-- if ttl <= 0 { if slides_idx < (slides_num - 1) { if slides_idx > slides.Length()-1 { slides_idx++ } } g.Percent = calcPercent(slides_idx, slides_num) ttl = maxttl } lbl := "Progress - " + strconv.Itoa(g.Percent) + "%" + " TTL: " + strconv.Itoa(ttl) g.BorderLabel = lbl draw() }) tui.Loop() }