Search⌘ K

Organizing the Interface’s Layout

Explore how to organize the interface layout effectively in Go CLI applications by creating start and pause buttons. Understand implementing lightweight, nonblocking button callbacks using goroutines to control Pomodoro intervals, update UI elements, and handle errors asynchronously.

Now that we’ve added the main widgets, we can create the buttons to start and pause Pomodoro intervals. To make it easier to maintain and update the code, we’ll add the buttons in a different file. Save and close the widgets.go file and open a new file buttons.go for editing.

Creating the buttons

We start by adding the package definition and the import list. For this file, we’ll use the following packages:

  • context to carry cancellation signals.
  • fmt to format strings.
  • termdash/cell, which provides customization options for widgets.
  • termdash/widgets/button to define a button widget.
  • pomodoro, which we created with the business logic.
Go (1.6.2)
package app
import (
"context"
"fmt"
"github.com/mum4k/termdash/cell"
"github.com/mum4k/termdash/widgets/button"
"pragprog.com/rggo/interactiveTools/pomo/pomodoro"
)
Importing the required list

Then we define a new custom type, buttonSet, that includes the btStart and btPause fields of type button.Button, which represents a Termdash button:

Go (1.6.2)
type buttonSet struct {
btStart *button.Button
btPause *button.Button
}
Custom type struct

Creating the newButtonSet() function

Next, we add the function newButtonSet() to instantiate a buttonSet. This function takes the following inputs: a Context to carry cancellation signals, an instance of ...