yankable.go

 1package yankable
 2
 3import (
 4	"time"
 5
 6	"github.com/charmbracelet/bubbles/timer"
 7	tea "github.com/charmbracelet/bubbletea"
 8	"github.com/charmbracelet/lipgloss"
 9)
10
11type Yankable struct {
12	YankStyle lipgloss.Style
13	Style     lipgloss.Style
14	Text      string
15	timer     timer.Model
16	clicked   bool
17}
18
19func (y *Yankable) Init() tea.Cmd {
20	y.timer = timer.New(3 * time.Second)
21	return nil
22}
23
24func (y *Yankable) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
25	cmds := make([]tea.Cmd, 0)
26	switch msg := msg.(type) {
27	case tea.MouseMsg:
28		switch msg.Type {
29		case tea.MouseRight:
30			y.clicked = true
31			cmds = append(cmds, y.timer.Init())
32		}
33	case timer.TimeoutMsg:
34		y.clicked = false
35	}
36	return y, tea.Batch(cmds...)
37}
38
39func (y *Yankable) View() string {
40	if y.clicked {
41		return y.YankStyle.Render(y.Text)
42	}
43	return y.Style.Render(y.Text)
44}