Displaying Historical Data to the Users
Use historical data to display a summary of activities to the users.
Now, let’s update the application interface to display the two new widgets.
Updating the summaryWidgets.go file
Let’s update the file called summaryWidgets.go to define the new widgets.
We add the package definition and import list.
We’ll use:
- The
contextpackage to define a cancellation context to close the widgets. - The
mathpackage to use some mathematical functions. - The
timepackage to deal with times and dates. - The
cell,widgets/barchart, andwidgets/linechartpackages to create the required widgets. - Our
pomodoropackage to access the repository.
package appimport ("context""math""time""github.com/mum4k/termdash/cell""github.com/mum4k/termdash/widgets/barchart""github.com/mum4k/termdash/widgets/linechart""pragprog.com/rggo/interactiveTools/pomo/pomodoro")
We create a custom type summary as a collection of summary widgets. We’ll use this type to connect the widgets to the app and update them:
type summary struct {bcDay *barchart.BarChartlcWeekly *linechart.LineChartupdateDaily chan boolupdateWeekly chan bool}
We define an update() method for the summary type, which will update all summary
widgets by sending a value to the update channels. Each widget will have a goroutine waiting for data coming from this channel to update. This is the
same approach we used for all the previous widgets:
func (s *summary) update(redrawCh chan<- bool) {s.updateDaily <- trues.updateWeekly <- trueredrawCh <- true}
Defining the newSummary() function
Now we define a newSummary() function that initializes both widgets and returns
an instance of summary with them. ...