func setContacts(g *gocui.Gui, contacts map[string]*contact) error { if jidBuffs == nil { jidBuffs = make(map[string][]byte) } g.View("contacts").Clear() viewJids = make([]string, 1) viewJids[0] = "Home" for _, c := range contacts { if !c.HasAxo() { continue } viewJids = append(viewJids, c.String()) } sort.Strings(viewJids[1:]) for _, c := range viewJids { fmt.Fprintln(g.View("contacts"), c) } // Cleanup buffers for k, _ := range jidBuffs { if _, ok := contacts[k]; !ok { delete(jidBuffs, k) } } g.Flush() return nil }
func updateMainView(g *gocui.Gui) { main_view, _ := g.View("main") for i := range main_output { main_view.Clear() fmt.Fprintln(main_view, i) g.Flush() } }
func updateAveragesView(g *gocui.Gui) { averages_view, _ := g.View("averages") for i := range averages_output { averages_view.Clear() fmt.Fprintln(averages_view, i) g.Flush() } }
func updateSparksView(g *gocui.Gui) { sparks_view, _ := g.View("sparks") for i := range sparks_output { sparks_view.Clear() fmt.Fprintln(sparks_view, i) g.Flush() } }
// the UI could be a lot smoother if there was less Clear() // and Flush() .. Flush() could run on an interval instead of // inside these update*View functions func updateStatusView(g *gocui.Gui) { status_view, _ := g.View("status") for i := range status_output { status_view.Clear() fmt.Fprintln(status_view, i) g.Flush() } }
func updateTimeView(g *gocui.Gui) { for { time.Sleep(1 * time.Second) time_view, _ := g.View("time") time_view.Clear() fmt.Fprintln(time_view, "", time.Now().Local()) if err := g.Flush(); err != nil { return } } }
func delProj(g *gocui.Gui, v *gocui.View) error { if l := current_name; l != "" { delete(projects, l) } g.ShowCursor = false delView(g, "del_proj") delView(g, "list") g.Flush() setView(g, "list") return nil }
func updateLogDumpView(g *gocui.Gui) { logdump_view, _ := g.View("logdump_view") flush := time.Tick(update_logdump_frequency * time.Millisecond) for { select { case log_data := <-logdump_output: fmt.Fprintln(logdump_view, log_data) case <-flush: g.Flush() } } }
func saveProj(g *gocui.Gui, v *gocui.View) error { if l := strings.TrimSpace(v.Buffer()); l != "" { init_t, _ := time.ParseDuration("0s") init_d, _ := time.ParseDuration("0s") projects[l] = Project{time.Now(), &init_d, &init_t} } g.ShowCursor = false delView(g, "save_proj") delView(g, "list") g.Flush() setView(g, "list") updateProjInfo(g, v) return nil }
func updateAlertsView(g *gocui.Gui) { alerts_view, _ := g.View("alerts") for { select { case alert_text := <-alerts_output: fmt.Fprintln(alerts_view, alert_text) g.Flush() case alert_state := <-alert_state_chan: if alert_state { alerts_view.BgColor = gocui.ColorRed } else { alerts_view.BgColor = gocui.ColorDefault } g.Flush() } } }
func display(g *gocui.Gui, to, message string) error { // Write in buffer buf, ok := jidBuffs[to] if !ok { jidBuffs[to] = make([]byte, 0) buf = jidBuffs[to] } buf = append(buf, message...) jidBuffs[to] = buf // Is it the current view ? contacts := g.View("contacts") _, cy := contacts.Cursor() contact, err := contacts.Line(cy) if err != nil { return err } // If yes, write message in main view if to == contact { fmt.Fprint(g.View("main"), message) } return g.Flush() }
func updateViews(g *gocui.Gui) { for { time.Sleep(1000 * time.Millisecond) if sv := g.View("status"); sv != nil { sv.Clear() fmt.Fprintln(sv, time.Now().Local()) } if sv := g.View("counters"); sv != nil { sv.Clear() indexedSlice := make([]string, len(counters)) i := 0 for k, _ := range counters { indexedSlice[i] = k i++ } sort.Strings(indexedSlice) for _, value := range indexedSlice { fmt.Fprintf(sv, "%s =\t%d\n", value, counters[value].value) var duration time.Duration = time.Now().Sub(counters[value].received) if duration.Seconds() > float64(*flushInterval) { delete(counters, value) } } } if sv := g.View("gauges"); sv != nil { sv.Clear() indexedSlice := make([]string, len(gauges)) i := 0 for k, _ := range gauges { indexedSlice[i] = k i++ } sort.Strings(indexedSlice) for _, value := range indexedSlice { fmt.Fprintf(sv, "%s =\t%d\n", value, gauges[value]) } } if sv := g.View("timers"); sv != nil { sv.Clear() indexedSlice := make([]string, len(timers)) i := 0 for k, _ := range timers { indexedSlice[i] = k i++ } sort.Strings(indexedSlice) for _, value := range indexedSlice { calculateAverageTime(sv, value, timers[value]) } } if err := g.Flush(); err != nil { return } } }
func debug(g *gocui.Gui, str string) error { fmt.Fprintln(g.View("main"), str) g.Flush() return nil }
func debugf(g *gocui.Gui, format string, args ...interface{}) error { fmt.Fprintf(g.View("main"), format, args) g.Flush() return nil }