| 1 |
package commands |
| 2 |
|
| 3 |
import ( |
| 4 |
"git.sr.ht/~rjarry/aerc/app" |
| 5 |
"git.sr.ht/~rjarry/aerc/config" |
| 6 |
"github.com/pkg/errors" |
| 7 |
"go.rockorager.dev/vaxis" |
| 8 |
) |
| 9 |
|
| 10 |
type SendKeys struct { |
| 11 |
Keys string `opt:"..."` |
| 12 |
} |
| 13 |
|
| 14 |
func init() { |
| 15 |
Register(SendKeys{}) |
| 16 |
} |
| 17 |
|
| 18 |
func (SendKeys) Description() string { |
| 19 |
return "Send keystrokes to the currently visible terminal." |
| 20 |
} |
| 21 |
|
| 22 |
func (SendKeys) Context() CommandContext { |
| 23 |
return GLOBAL |
| 24 |
} |
| 25 |
|
| 26 |
func (SendKeys) Aliases() []string { |
| 27 |
return []string{"send-keys"} |
| 28 |
} |
| 29 |
|
| 30 |
func (s SendKeys) Execute(args []string) error { |
| 31 |
tab, ok := app.SelectedTabContent().(app.HasTerminal) |
| 32 |
if !ok { |
| 33 |
return errors.New("There is no terminal here") |
| 34 |
} |
| 35 |
|
| 36 |
term := tab.Terminal() |
| 37 |
if term == nil { |
| 38 |
return errors.New("The terminal is not active") |
| 39 |
} |
| 40 |
|
| 41 |
keys2send, err := config.ParseKeyStrokes(s.Keys) |
| 42 |
if err != nil { |
| 43 |
return errors.Wrapf(err, "Unable to parse keystroke: %q", s.Keys) |
| 44 |
} |
| 45 |
|
| 46 |
for _, key := range keys2send { |
| 47 |
ev := vaxis.Key{ |
| 48 |
Keycode: key.Key, |
| 49 |
Modifiers: key.Modifiers, |
| 50 |
} |
| 51 |
term.Event(ev) |
| 52 |
} |
| 53 |
|
| 54 |
term.Invalidate() |
| 55 |
|
| 56 |
return nil |
| 57 |
} |