Skip to content

Feature: Repeat Ctrl commands #613

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions parser/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ func (p *Parser) parseCommand() []Command {
case token.TYPE:
return []Command{p.parseType()}
case token.CTRL:
return []Command{p.parseCtrl()}
return p.parseCtrl()
case token.ALT:
return []Command{p.parseAlt()}
case token.SHIFT:
Expand Down Expand Up @@ -287,7 +287,7 @@ func (p *Parser) parseTime() string {
// E.g:
// Ctrl+Shift+O
// Ctrl+Alt+Shift+P
func (p *Parser) parseCtrl() Command {
func (p *Parser) parseCtrl() []Command {
var args []string

inModifierChain := true
Expand Down Expand Up @@ -339,7 +339,13 @@ func (p *Parser) parseCtrl() Command {
}

ctrlArgs := strings.Join(args, " ")
return Command{Type: token.CTRL, Args: ctrlArgs}
repeat, _ := strconv.Atoi(p.parseRepeat())

cmds := make([]Command, 0, repeat)
for range repeat {
cmds = append(cmds, Command{Type: token.CTRL, Args: ctrlArgs})
}
return cmds
}

// parseAlt parses an alt command.
Expand Down
24 changes: 16 additions & 8 deletions parser/parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -258,14 +258,20 @@ func TestParseCtrl(t *testing.T) {
wantArgs: []string{"Space"},
wantErr: false,
},
{
name: "Ctrl+w 2",
tape: "Ctrl+w 2",
wantArgs: []string{"w"},
wantErr: false,
},
}

for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
l := lexer.New(tc.tape)
p := New(l)

cmd := p.parseCtrl()
cmds := p.parseCtrl()
if tc.wantErr {
if len(p.errors) == 0 {
t.Errorf("Expected to parse with errors but was success")
Expand All @@ -277,14 +283,16 @@ func TestParseCtrl(t *testing.T) {
t.Errorf("Expected to parse with no errors but was failure")
}

args := strings.Split(cmd.Args, " ")
if len(tc.wantArgs) != len(args) {
t.Fatalf("Unable to parse args, expected args %d, got %d", len(tc.wantArgs), len(args))
}
for _, cmd := range cmds {
args := strings.Split(cmd.Args, " ")
if len(tc.wantArgs) != len(args) {
t.Fatalf("Unable to parse args, expected args %d, got %d", len(tc.wantArgs), len(args))
}

for i, arg := range args {
if tc.wantArgs[i] != arg {
t.Errorf("Arg %d is wrong, expected %s, got %s", i, tc.wantArgs[i], arg)
for i, arg := range args {
if tc.wantArgs[i] != arg {
t.Errorf("Arg %d is wrong, expected %s, got %s", i, tc.wantArgs[i], arg)
}
}
}
})
Expand Down
Loading