Skip to content

Commit

Permalink
Fix alt modifier
Browse files Browse the repository at this point in the history
The alt modifier was not handled correctly for both sending and
recording key presses.

To send the key press, the `ESC` sequences needs to be transmitted
before the key.

For recording, the escape sequences lookup table had to be updated with
all alphanumeric combinations.

Syntax highlights had also not been updated.

Signed-off-by: Michael Lorant <[email protected]>
  • Loading branch information
mikelorant committed Mar 8, 2024
1 parent 9950678 commit 41176a5
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 6 deletions.
13 changes: 7 additions & 6 deletions command.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"strconv"
"strings"
"time"
"unicode"

"github.com/atotto/clipboard"
"github.com/charmbracelet/vhs/lexer"
Expand Down Expand Up @@ -139,23 +140,23 @@ func ExecuteCtrl(c parser.Command, v *VHS) {
// ExecuteAlt is a CommandFunc that presses the argument key with the alt key
// held down on the running instance of vhs.
func ExecuteAlt(c parser.Command, v *VHS) {
_ = v.Page.Keyboard.Press(input.AltLeft)
const esc = '\x1b'

if k, ok := token.Keywords[c.Args]; ok {
_ = v.Page.Keyboard.Press(input.AltLeft)
switch k {
case token.ENTER:
_ = v.Page.Keyboard.Type(input.Enter)
case token.TAB:
_ = v.Page.Keyboard.Type(input.Tab)
}
_ = v.Page.Keyboard.Release(input.AltLeft)
} else {
for _, r := range c.Args {
if k, ok := keymap[r]; ok {
_ = v.Page.Keyboard.Type(k)
}
k := []rune{esc, unicode.ToLower(r)}
_ = v.Page.InsertText(string(k))
}
}

_ = v.Page.Keyboard.Release(input.AltLeft)
}

// ExecuteShift is a CommandFunc that presses the argument key with the shift
Expand Down
36 changes: 36 additions & 0 deletions record.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,42 @@ var EscapeSequences = map[string]string{
"\x19": token.CTRL + "+Y",
"\x1a": token.CTRL + "+Z",
"\x1b": token.ESCAPE,
"\x1b1": token.ALT + "+1",
"\x1b2": token.ALT + "+2",
"\x1b3": token.ALT + "+3",
"\x1b4": token.ALT + "+4",
"\x1b5": token.ALT + "+5",
"\x1b6": token.ALT + "+6",
"\x1b7": token.ALT + "+7",
"\x1b8": token.ALT + "+8",
"\x1b9": token.ALT + "+9",
"\x1b0": token.ALT + "+0",
"\x1ba": token.ALT + "+A",
"\x1bb": token.ALT + "+B",
"\x1bc": token.ALT + "+C",
"\x1bd": token.ALT + "+D",
"\x1be": token.ALT + "+E",
"\x1bf": token.ALT + "+F",
"\x1bg": token.ALT + "+G",
"\x1bh": token.ALT + "+H",
"\x1bi": token.ALT + "+I",
"\x1bj": token.ALT + "+J",
"\x1bk": token.ALT + "+K",
"\x1bl": token.ALT + "+L",
"\x1bm": token.ALT + "+M",
"\x1bn": token.ALT + "+N",
"\x1bo": token.ALT + "+O",
"\x1bp": token.ALT + "+P",
"\x1bq": token.ALT + "+Q",
"\x1br": token.ALT + "+R",
"\x1bs": token.ALT + "+S",
"\x1bt": token.ALT + "+T",
"\x1bu": token.ALT + "+U",
"\x1bv": token.ALT + "+V",
"\x1bw": token.ALT + "+W",
"\x1bx": token.ALT + "+X",
"\x1by": token.ALT + "+Y",
"\x1bz": token.ALT + "+Z",
"\x7f": token.BACKSPACE,
}

Expand Down
1 change: 1 addition & 0 deletions style.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ const (
// Styles for syntax highlighting
var (
CommandStyle = lipgloss.NewStyle().Foreground(lipgloss.Color("12"))
AltStyle = lipgloss.NewStyle().Foreground(lipgloss.Color("12"))
FaintStyle = lipgloss.NewStyle().Foreground(lipgloss.AdaptiveColor{Light: "242", Dark: "238"})
NoneStyle = lipgloss.NewStyle()
KeywordStyle = lipgloss.NewStyle()
Expand Down
2 changes: 2 additions & 0 deletions syntax.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ func Highlight(c parser.Command, faint bool) string {
argsStyle = StringStyle
case token.CTRL:
argsStyle = CommandStyle
case token.ALT:
argsStyle = AltStyle
case token.SLEEP:
argsStyle = TimeStyle
case token.TYPE:
Expand Down

0 comments on commit 41176a5

Please sign in to comment.