func printHosts(lastApiResponse *healthd.ApiResponseHosts, status *healthdStatus) { goterm.Clear() // Clear current screen goterm.MoveCursor(1, 1) defer goterm.Flush() goterm.Println("Current Time:", status.FmtNow(), " Status:", status.FmtStatus()) // if lastApiResponse == nil { goterm.Println("no data yet") return } columns := []string{ "Host:Port", "Status", "Last Checked", "Last Response Time", } for i, s := range columns { columns[i] = goterm.Bold(goterm.Color(s, goterm.BLACK)) } table := goterm.NewTable(0, goterm.Width()-1, 5, ' ', 0) fmt.Fprintf(table, "%s\n", strings.Join(columns, "\t")) for _, host := range lastApiResponse.Hosts { printHost(table, host) } goterm.Println(table) }
func (cmd *Command) overviewAction(backendId string, watch int, limit int) { var bk *engine.BackendKey if backendId != "" { bk = &engine.BackendKey{Id: backendId} } for { frontends, err := cmd.client.TopFrontends(bk, limit) if err != nil { cmd.printError(err) frontends = []engine.Frontend{} } servers, err := cmd.client.TopServers(bk, limit) if err != nil { cmd.printError(err) servers = []engine.Server{} } t := time.Now() if watch != 0 { goterm.Clear() goterm.MoveCursor(1, 1) goterm.Flush() fmt.Fprintf(cmd.out, "%s Every %d seconds. Top %d entries\n\n", t.Format("2006-01-02 15:04:05"), watch, limit) } cmd.printOverview(frontends, servers) if watch != 0 { goterm.Flush() } else { return } time.Sleep(time.Second * time.Duration(watch)) } }
func main() { tm.Clear() // Clear current screen started := 100 finished := 250 // Based on http://golang.org/pkg/text/tabwriter totals := tm.NewTable(0, 10, 5, ' ', 0) fmt.Fprintf(totals, "Time\tStarted\tActive\tFinished\n") fmt.Fprintf(totals, "%s\t%d\t%d\t%d\n", "All", started, started-finished, finished) tm.Println(totals) tm.Flush() }
func main() { tm.Clear() tm.MoveCursor(0, 0) chart := tm.NewLineChart(100, 20) data := new(tm.DataTable) data.AddColumn("Time") data.AddColumn("Sin(x)") data.AddColumn("Cos(x+1)") for i := 0.1; i < 10; i += 0.1 { data.AddRow(i, math.Sin(i), math.Cos(i+1)) } tm.Println(chart.Draw(data)) tm.Flush() }
func main() { f, err := os.Create("box.txt") if err != nil { panic("Unable to create box file!") } defer f.Close() // Tell tm to use the file we just opened, not stdout tm.Output = bufio.NewWriter(f) // More or less stolen from the box example tm.Clear() box := tm.NewBox(30|tm.PCT, 20, 0) fmt.Fprint(box, "Some box content") tm.Print(tm.MoveTo(box.String(), 40|tm.PCT, 40|tm.PCT)) tm.Flush() fmt.Println("Now view the contents of 'box.txt' in an ansi-capable terminal") }
func printJobs(lastApiResponse *healthd.ApiResponseJobs, status *healthdStatus) { goterm.Clear() // Clear current screen goterm.MoveCursor(1, 1) defer goterm.Flush() goterm.Println("Current Time:", status.FmtNow(), " Status:", status.FmtStatus()) if lastApiResponse == nil { goterm.Println("no data yet") return } columns := []string{ "Job", // "Jobs/Second", //minute? flag? "Total Count", "Success", "ValidationError", "Panic", "Error", "Junk", "Avg Response Time", "Stddev", "Min", "Max", "Total", } for i, s := range columns { columns[i] = goterm.Bold(goterm.Color(s, goterm.BLACK)) } table := goterm.NewTable(0, goterm.Width()-1, 5, ' ', 0) fmt.Fprintf(table, "%s\n", strings.Join(columns, "\t")) for _, job := range lastApiResponse.Jobs { printJob(table, job) } goterm.Println(table) }
func main() { var ( errCh = make(chan error, 16) receivedCh = make(chan int, 1024) sentCh = make(chan int, 1024) sent = 0 lastSent = 0 received = 0 lastReceived = 0 ) // read params (server address:port, qos settings, number of publishers, number of subscribers) flag.Parse() // generate topics in advance, so we can run the subscribers before starting to publish for i := 0; i < *numTop; i++ { newTopic() } // // subscribe to topics // // TODO: multiple subscribers. // initialise a timeout (if no messages are received in the given time since the last publish.) timeout := time.NewTimer(10 * time.Second) resetSubTimeout := func() { timeout.Reset(time.Duration(*subTimeout) * time.Second) } // discarding received messages messageHandler := func(client *mqtt.Client, m mqtt.Message) { if string(m.Payload()) == "hello world" { receivedCh <- 1 resetSubTimeout() // reset timeout } } // prepare filter from topics and qos filters := map[string]byte{} for _, topic := range getTopics() { fmt.Printf("Created topic %s with qos %v\n", topic, *qos) filters[topic] = byte(*qos) } // multisubscribers fmt.Println("Connecting subscribers...") for i := 0; i < *numSub; i++ { subscriber := newClient() if token := subscriber.client.Connect(); token.Wait() && token.Error() != nil { errCh <- token.Error() } defer subscriber.client.Disconnect(250) token := subscriber.client.SubscribeMultiple(filters, messageHandler) if token.Wait() && token.Error() != nil { errCh <- token.Error() } } // // Publish to topics // // set value for timeout timeout = time.NewTimer(time.Duration(*subTimeout) * time.Second) go func() { for { select { case <-timeout.C: errCh <- fmt.Errorf("Subscriber timeout.. no more messages?") } } }() // publishers for i := 0; i < *numPub; i++ { go func() { c := newClient() if token := c.client.Connect(); token.Wait() && token.Error() != nil { errCh <- token.Error() } defer c.client.Disconnect(100) // publish (sequential per client) topics := getTopics() for k := 0; k < *numMessages; k++ { topic := topics[k%len(topics)] token := c.client.Publish(topic, byte(*qos), *retained, "hello world") if token.Wait() && token.Error() != nil { errCh <- token.Error() } sentCh <- 1 time.Sleep(time.Duration(*pubDelay) * time.Millisecond) } }() } // creating fancy output // start := time.Now() redraw := time.NewTicker(100 * time.Millisecond) if *nograph { redraw = time.NewTicker(1 * time.Second) } row := 0.0 // table data := new(tm.DataTable) data.AddColumn("Time (sec)") data.AddColumn("Sent") data.AddColumn("Received") for { select { case <-redraw.C: if !*nograph { tm.Clear() tm.Printf("[Published : Received]: [%v : %v]\n\n", sent, received) row += 1.0 data.AddRow(row/10.0, float64(sent), float64(received)) tm.Print(tm.NewLineChart(tm.Width()-4, tm.Height()-6).Draw(data)) tm.Flush() } else { if sent != lastSent || received != lastReceived { fmt.Printf("[Published : Received]: [%v : %v]\n", sent, received) lastSent, lastReceived = sent, received } } case e := <-errCh: fmt.Println(e.Error()) return case s := <-sentCh: sent += s case r := <-receivedCh: received += r } } }
func Run(done chan bool) { if !verbose { tm.Clear() // Clear current screen } failed := false var failedUpdate builder.Update for { time.Sleep(time.Millisecond) if !verbose { tm.MoveCursor(1, 1) header := fmt.Sprintf("Building (%s)", nstime.NsReadable(time.Since(stated).Nanoseconds()), ) termPrintln(header) } for worker, update := range statuses { if !verbose { tm.MoveCursor(worker+2, 1) } switch update.Status { case builder.Pending: termPrintln("[ IDLE ]") case builder.Started: ts := time.Since(update.TimeStamp) pbr := ">" s := fmt.Sprintf("%s %s (%s)", pbr, update.Target, nstime.NsReadable(ts.Nanoseconds()), ) termPrintln(s) case builder.Fail: termPrintln("[ IDLE ]") exit = true failed = true failedUpdate = update case builder.Success: termPrintln("[ IDLE ]") break } } if !verbose { tm.Flush() // Call it every time at the end of rendering } if exit { if failed { if !verbose { tm.MoveCursor(worderCount+2, 1) failMessage(failedUpdate.Target) tm.Flush() } else { failMessage(failedUpdate.Target) } } done <- true } } }