- sigChan := make(chan os.Signal, 1)
- done := make(chan struct{})
- defer close(done)
- termboxChan := new_tb_chan()
-
- signal.Notify(sigChan, syscall.SIGINT, syscall.SIGTERM)
-
- state.Setup(dbh)
- for !state.Finished() {
- select {
- case <-done:
- fmt.Println("exiting")
- state.SetFinished()
- case sig := <-sigChan:
- fmt.Println("Caught a signal", sig)
- done <- struct{}{}
- case <-wi.WaitNextPeriod():
- state.Collect()
- wi.CollectedNow()
- state.Display()
- case event := <-termboxChan:
- // switch on event type
- switch event.Type {
- case termbox.EventKey: // actions depend on key
- switch event.Key {
- case termbox.KeyCtrlZ, termbox.KeyCtrlC, termbox.KeyEsc:
- state.SetFinished()
- case termbox.KeyArrowLeft: // left arrow change to previous display mode
- state.DisplayPrevious()
- state.Display()
- case termbox.KeyTab, termbox.KeyArrowRight: // tab or right arrow - change to next display mode
- state.DisplayNext()
- state.Display()
- }
- switch event.Ch {
- case '-': // decrease the interval if > 1
- if wi.WaitInterval() > time.Second {
- wi.SetWaitInterval(wi.WaitInterval() - time.Second)
- }
- case '+': // increase interval by creating a new ticker
- wi.SetWaitInterval(wi.WaitInterval() + time.Second)
- case 'h', '?': // help
- state.SetHelp(!state.Help())
- case 'q': // quit
- state.SetFinished()
- case 't': // toggle between absolute/relative statistics
- state.SetWantRelativeStats(!state.WantRelativeStats())
- state.Display()
- case 'z': // reset the statistics to now by taking a query of current values
- state.ResetDBStatistics()
- state.Display()
- }
- case termbox.EventResize: // set sizes
- state.ScreenSetSize(event.Width, event.Height)
- state.Display()
- case termbox.EventError: // quit
- log.Fatalf("Quitting because of termbox error: \n%s\n", event.Err)
- }
- }
- }
- state.Cleanup()