aboutsummaryrefslogtreecommitdiffstats
path: root/commands/send-keys.go
blob: a49f4f1cd81cbfaee2ec1ec6ed0942f2387af1c1 (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
52
53
54
55
56
57
package commands

import (
	"git.sr.ht/~rjarry/aerc/app"
	"git.sr.ht/~rjarry/aerc/config"
	"git.sr.ht/~rockorager/vaxis"
	"github.com/pkg/errors"
)

type SendKeys struct {
	Keys string `opt:"..."`
}

func init() {
	Register(SendKeys{})
}

func (SendKeys) Description() string {
	return "Send keystrokes to the currently visible terminal."
}

func (SendKeys) Context() CommandContext {
	return GLOBAL
}

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

func (s 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")
	}

	keys2send, err := config.ParseKeyStrokes(s.Keys)
	if err != nil {
		return errors.Wrapf(err, "Unable to parse keystroke: %q", s.Keys)
	}

	for _, key := range keys2send {
		ev := vaxis.Key{
			Keycode:   key.Key,
			Modifiers: key.Modifiers,
		}
		term.Event(ev)
	}

	term.Invalidate()

	return nil
}