56 lines
839 B
Go
56 lines
839 B
Go
package plays
|
|
|
|
import (
|
|
"fmt"
|
|
"time"
|
|
|
|
tea "github.com/charmbracelet/bubbletea"
|
|
)
|
|
|
|
type tickMsg time.Time
|
|
|
|
type Redirect struct {
|
|
Message string
|
|
|
|
secLeft int
|
|
}
|
|
|
|
func NewRedirect(msg string) *Redirect {
|
|
return &Redirect{
|
|
Message: msg,
|
|
|
|
secLeft: 3,
|
|
}
|
|
}
|
|
|
|
func tick() tea.Cmd {
|
|
return tea.Tick(time.Second, func(t time.Time) tea.Msg {
|
|
return tickMsg(t)
|
|
})
|
|
}
|
|
|
|
func (m *Redirect) Init() tea.Cmd {
|
|
return tick()
|
|
}
|
|
|
|
func (m *Redirect) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
|
|
switch msg := msg.(type) {
|
|
case tea.KeyMsg:
|
|
switch msg.String() {
|
|
case "q", "ctrl+c", "enter":
|
|
return m, tea.Quit
|
|
}
|
|
case tickMsg:
|
|
m.secLeft--
|
|
if m.secLeft <= 0 {
|
|
return m, tea.Quit
|
|
}
|
|
return m, tick()
|
|
}
|
|
return m, nil
|
|
}
|
|
|
|
func (m *Redirect) View() string {
|
|
return fmt.Sprintf("%s\n\nExit in %d seconds...", m.Message, m.secLeft)
|
|
}
|