1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
|
package app
import (
"strings"
"testing"
"github.com/gdamore/tcell/v2"
)
// pasteKeys sends text as a terminal's bracketed paste would: a start mark,
// the characters as keys with Enter for each newline, an end mark.
func pasteKeys(a *App, text string) {
a.handle(tcell.NewEventPaste(true))
for _, r := range text {
if r == '\n' {
a.handle(tcell.NewEventKey(tcell.KeyEnter, 0, tcell.ModNone))
continue
}
a.handle(tcell.NewEventKey(tcell.KeyRune, r, tcell.ModNone))
}
a.handle(tcell.NewEventPaste(false))
}
func TestABracketedPasteReachesAnAgentWindowWhole(t *testing.T) {
// Key by key, the two Enters would each have sent a prompt; whole, the
// three lines are one paste, kept aside as a token.
a, _ := newAgentsApp(t, "[[agent]]\nname=\"Echo\"\ncommand=\"cat\"\n")
a.NewAgent("Echo")
agent := a.activeAgent()
if agent == nil {
t.Fatal("no agent window is in front")
}
pasteKeys(a, "one\ntwo\nthree")
if got := agent.view.Input(); got != "[Pasted #1 · 3 lines · 13 chars]" {
t.Errorf("the box holds %q", got)
}
if entries := agent.session.Entries(); len(entries) != 0 {
t.Errorf("%d prompt(s) were sent by the newlines inside the paste", len(entries))
}
}
func TestABracketedPasteIntoAFileIsTypedAsBefore(t *testing.T) {
a, _ := newTestApp(t)
a.NewFile()
pasteKeys(a, "ab")
if got := a.activeView().Buffer().Text(); got != "ab" {
t.Errorf("the file holds %q after a paste, want %q", got, "ab")
}
}
func TestKeysAreNotLostWhenAPasteIsCancelledByAnotherWindow(t *testing.T) {
// The paste starts in an agent window; nothing else in the editor sees
// its keys until it ends — so a key layer cannot half-act on them.
a, _ := newAgentsApp(t, "[[agent]]\nname=\"Echo\"\ncommand=\"cat\"\n")
a.NewAgent("Echo")
a.handle(tcell.NewEventPaste(true))
a.handle(tcell.NewEventKey(tcell.KeyF4, 0, tcell.ModNone)) // New file, were it a keystroke
a.handle(tcell.NewEventPaste(false))
if len(a.desktop.Windows()) != 1 {
t.Errorf("%d windows are open: a key inside a paste acted as a shortcut", len(a.desktop.Windows()))
}
}
func TestEditPasteGoesIntoTheAgentBox(t *testing.T) {
a, _ := newAgentsApp(t, "[[agent]]\nname=\"Echo\"\ncommand=\"cat\"\n")
a.NewAgent("Echo")
a.clipboard.SetText("from a file\nin the editor\n")
a.Paste()
if got := a.activeAgent().view.Input(); !strings.HasPrefix(got, "[Pasted #1 · 2 lines") {
t.Errorf("the box holds %q after Edit ▸ Paste", got)
}
}
func TestCtrlVInAnAgentWindowPastesTheEditorsClipboard(t *testing.T) {
a, _ := newAgentsApp(t, "[[agent]]\nname=\"Echo\"\ncommand=\"cat\"\n")
a.NewAgent("Echo")
a.clipboard.SetText("buildMenus")
press(a, tcell.KeyCtrlV, 0, tcell.ModNone)
if got := a.activeAgent().view.Input(); got != "buildMenus" {
t.Errorf("the box holds %q after Ctrl-V", got)
}
}
|