yankable.go

 1package yankable
 2
 3import (
 4	"io"
 5
 6	"github.com/aymanbagabas/go-osc52"
 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	clicked   bool
16	osc52     *osc52.Output
17}
18
19func New(w io.Writer, environ []string, style, yankStyle lipgloss.Style, text string) *Yankable {
20	return &Yankable{
21		yankStyle: yankStyle,
22		style:     style,
23		text:      text,
24		clicked:   false,
25		osc52:     osc52.NewOutput(w, environ),
26	}
27}
28
29func (y *Yankable) SetText(text string) {
30	y.text = text
31}
32
33func (y *Yankable) Init() tea.Cmd {
34	return nil
35}
36
37func (y *Yankable) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
38	switch msg := msg.(type) {
39	case tea.MouseMsg:
40		switch msg.Type {
41		case tea.MouseRight:
42			y.clicked = true
43			return y, y.copy()
44		}
45	default:
46		y.clicked = false
47	}
48	return y, nil
49}
50
51func (y *Yankable) View() string {
52	if y.clicked {
53		return y.yankStyle.String()
54	}
55	return y.style.Render(y.text)
56}
57
58func (y *Yankable) copy() tea.Cmd {
59	y.osc52.Copy(y.text)
60	return nil
61}