github.com/wolfi-dev/wolfictl@v0.16.11/pkg/cli/components/keytocontinue/key_to_continue.go (about)

     1  package keytocontinue
     2  
     3  import (
     4  	"fmt"
     5  
     6  	tea "github.com/charmbracelet/bubbletea"
     7  	"github.com/charmbracelet/lipgloss"
     8  	"github.com/wolfi-dev/wolfictl/pkg/cli/components/breather"
     9  	"github.com/wolfi-dev/wolfictl/pkg/cli/components/ctrlcwrapper"
    10  )
    11  
    12  var _ tea.Model = (*Model)(nil)
    13  
    14  type Model struct {
    15  	// key is the key that the user must press to continue. It must be a valid
    16  	// tea.KeyMsg string.
    17  	key string
    18  
    19  	// purpose is a string that describes the purpose of the key press. It should
    20  	// typically start with "to" and end with "...", e.g. "to continue...".
    21  	purpose string
    22  
    23  	programAboutToEnd bool
    24  	breather          breather.Model
    25  }
    26  
    27  func New(key, purpose string) Model {
    28  	return Model{
    29  		key:      key,
    30  		purpose:  purpose,
    31  		breather: breather.New(">"),
    32  	}
    33  }
    34  
    35  func (m Model) Init() tea.Cmd {
    36  	return m.breather.Init()
    37  }
    38  
    39  func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
    40  	switch msg := msg.(type) {
    41  	case tea.KeyMsg:
    42  		if msg.String() == m.key {
    43  			m.programAboutToEnd = true
    44  			return m, tea.Quit
    45  		}
    46  
    47  	case ctrlcwrapper.AboutToExitMsg:
    48  		m.programAboutToEnd = true
    49  		return m, ctrlcwrapper.InnerIsReady
    50  
    51  	case breather.TickMsg:
    52  		var cmd tea.Cmd
    53  		m.breather, cmd = m.breather.Update(msg)
    54  		return m, cmd
    55  	}
    56  
    57  	return m, nil
    58  }
    59  
    60  func (m Model) View() string {
    61  	if m.programAboutToEnd {
    62  		return ""
    63  	}
    64  
    65  	return fmt.Sprintf(
    66  		"%s Press %s %s\n",
    67  		m.breather.View(),
    68  		keyStyle.Render(m.key),
    69  		m.purpose,
    70  	)
    71  }
    72  
    73  var keyStyle = lipgloss.NewStyle().Bold(true)