Fix copying

This commit is contained in:
2024-08-17 15:10:43 +02:00
parent a33726cee7
commit c54c7f5089

17
main.go
View File

@@ -6,7 +6,6 @@ import (
"io" "io"
"log" "log"
"os" "os"
"strings"
"golang.design/x/clipboard" "golang.design/x/clipboard"
@@ -112,7 +111,7 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
m.out.Blur() m.out.Blur()
return m, tea.Quit return m, tea.Quit
case key.Matches(msg, m.keymap.copy): case key.Matches(msg, m.keymap.copy):
clipboard.Write(clipboard.FmtText, []byte(m.out.Value())) clipboard.Write(clipboard.FmtText, []byte(lines))
case key.Matches(msg, m.keymap.clear): case key.Matches(msg, m.keymap.clear):
m.out.SetValue("") m.out.SetValue("")
lines = lines[:0] lines = lines[:0]
@@ -134,7 +133,8 @@ func (m *model) sizeInputs() {
m.out.SetWidth(m.width - 5) m.out.SetWidth(m.width - 5)
} }
var lines []string var lines string
func (m model) View() string { func (m model) View() string {
help := m.help.ShortHelpView([]key.Binding{ help := m.help.ShortHelpView([]key.Binding{
m.keymap.copy, m.keymap.copy,
@@ -142,7 +142,7 @@ func (m model) View() string {
}) })
var views []string var views []string
m.out.SetValue(strings.Join(lines, "\n")) m.out.SetValue(lines)
views = append(views, m.out.View()) views = append(views, m.out.View())
return lipgloss.JoinHorizontal(lipgloss.Top, views...) + "\n" + help return lipgloss.JoinHorizontal(lipgloss.Top, views...) + "\n" + help
@@ -174,13 +174,14 @@ func main() {
if err != nil { if err != nil {
panic(err) panic(err)
} }
go func() { go func() {
ch := clipboard.Watch(context.TODO(), clipboard.FmtText) ch := clipboard.Watch(context.TODO(), clipboard.FmtText)
for data := range ch { for data := range ch {
// log.Printf("%#v", m.out.Value() + "\n" + string(data)) sdata := string(data)
// m.out.SetValue(m.out.Value() + "\n" + string(data)) if sdata != lines {
lines = append(lines, string(data)) lines += "\n" + string(sdata)
}
} }
}() }()