//autorefresh view that autorefreshes its content every second func autorefresh(dry *app.Dry, screen *ui.Screen, keyboardQueue chan termbox.Event, done chan<- struct{}, doneStats chan<- bool, errC <-chan error) { screen.Clear() v := ui.NewMarkupView("", 0, 0, screen.Width, screen.Height, false) //used to coordinate rendering betwen the ticker //and the exit event var mutex = &sync.Mutex{} app.Write(dry, v) err := v.Render() if err != nil { ui.ShowErrorMessage(screen, keyboardQueue, err) } screen.Flush() //the ticker is created after the first render timestampQueue := time.NewTicker(1000 * time.Millisecond) loop: for { select { case <-errC: { mutex.Lock() timestampQueue.Stop() break loop } case event := <-keyboardQueue: switch event.Type { case termbox.EventKey: if event.Key == termbox.KeyEsc { //the lock is acquired and the time-based refresh queue is stopped //before breaking the loop mutex.Lock() timestampQueue.Stop() break loop } } case <-timestampQueue.C: { mutex.Lock() v.Clear() app.Write(dry, v) v.Render() screen.Flush() mutex.Unlock() } } } //cleanup before exiting, the screen is cleared and the lock released termbox.HideCursor() screen.Clear() screen.Sync() mutex.Unlock() doneStats <- true done <- struct{}{} }
//less shows dry output in a "less" emulator func less(dry *app.Dry, screen *ui.Screen, keyboardQueue chan termbox.Event, done chan struct{}) { screen.Clear() v := ui.NewLess() v.MarkupSupport() go app.Write(dry, v) if err := v.Focus(keyboardQueue); err != nil { ui.ShowErrorMessage(screen, keyboardQueue, err) } termbox.HideCursor() screen.Clear() screen.Sync() done <- struct{}{} }