From 41176a555991962b99bfdb58de6ff74148a56562 Mon Sep 17 00:00:00 2001 From: Michael Lorant Date: Thu, 29 Dec 2022 21:35:31 +1100 Subject: [PATCH] Fix alt modifier 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 --- command.go | 13 +++++++------ record.go | 36 ++++++++++++++++++++++++++++++++++++ style.go | 1 + syntax.go | 2 ++ 4 files changed, 46 insertions(+), 6 deletions(-) diff --git a/command.go b/command.go index 0fea13ebc..9833f125d 100644 --- a/command.go +++ b/command.go @@ -9,6 +9,7 @@ import ( "strconv" "strings" "time" + "unicode" "github.com/atotto/clipboard" "github.com/charmbracelet/vhs/lexer" @@ -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 diff --git a/record.go b/record.go index 9fcf5c22a..cf7d3888e 100644 --- a/record.go +++ b/record.go @@ -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, } diff --git a/style.go b/style.go index d541e7d71..7de1796ac 100644 --- a/style.go +++ b/style.go @@ -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() diff --git a/syntax.go b/syntax.go index 981e0fd7c..cb74b6962 100644 --- a/syntax.go +++ b/syntax.go @@ -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: