aboutsummaryrefslogtreecommitdiffstats
path: root/commands/send-keys.go
blob: f9cae392d3b322d1381f8a662f24424ea8c496e2 (plain) (blame)
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
package commands

import (
	"strings"

	"git.sr.ht/~rjarry/aerc/app"
	"git.sr.ht/~rjarry/aerc/config"
	"github.com/gdamore/tcell/v2"
	"github.com/pkg/errors"
)

type SendKeys struct{}

func init() {
	register(SendKeys{})
}

func (SendKeys) Aliases() []string {
	return []string{"send-keys"}
}

func (SendKeys) Complete(args []string) []string {
	return nil
}

func (SendKeys) Execute(args []string) error {
	tab, ok := app.SelectedTabContent().(app.HasTerminal)
	if !ok {
		return errors.New("There is no terminal here")
	}

	term := tab.Terminal()
	if term == nil {
		return errors.New("The terminal is not active")
	}

	text2send := strings.Join(args[1:], "")
	keys2send, err := config.ParseKeyStrokes(text2send)
	if err != nil {
		return errors.Wrapf(err, "Unable to parse keystroke: '%s'", text2send)
	}

	for _, key := range keys2send {
		ev := tcell.NewEventKey(key.Key, key.Rune, key.Modifiers)
		term.Event(ev)
	}

	term.Invalidate()

	return nil
}