Skip to content

Commit

Permalink
feat: add insert and delete key
Browse files Browse the repository at this point in the history
  • Loading branch information
mbechto authored and maaslalani committed Nov 14, 2023
1 parent 3ed6a87 commit 1de813f
Show file tree
Hide file tree
Showing 9 changed files with 45 additions and 3 deletions.
4 changes: 4 additions & 0 deletions command.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ type CommandType TokenType
// CommandTypes is a list of the available commands that can be executed.
var CommandTypes = []CommandType{ //nolint: deadcode
BACKSPACE,
DELETE,
INSERT,
CTRL,
ALT,
DOWN,
Expand Down Expand Up @@ -63,6 +65,8 @@ type CommandFunc func(c Command, v *VHS)
// CommandFuncs maps command types to their executable functions.
var CommandFuncs = map[CommandType]CommandFunc{
BACKSPACE: ExecuteKey(input.Backspace),
DELETE: ExecuteKey(input.Delete),
INSERT: ExecuteKey(input.Insert),
DOWN: ExecuteKey(input.ArrowDown),
ENTER: ExecuteKey(input.Enter),
LEFT: ExecuteKey(input.ArrowLeft),
Expand Down
4 changes: 2 additions & 2 deletions command_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@ import (
)

func TestCommand(t *testing.T) {
const numberOfCommands = 25
const numberOfCommands = 27
if len(CommandTypes) != numberOfCommands {
t.Errorf("Expected %d commands, got %d", numberOfCommands, len(CommandTypes))
}

const numberOfCommandFuncs = 25
const numberOfCommandFuncs = 27
if len(CommandFuncs) != numberOfCommandFuncs {
t.Errorf("Expected %d commands, got %d", numberOfCommandFuncs, len(CommandFuncs))
}
Expand Down
2 changes: 2 additions & 0 deletions examples/demo.tape
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@
# Keys:
# Escape[@<time>] [number] Press the Escape key
# Backspace[@<time>] [number] Press the Backspace key
# Delete[@<time>] [number] Press the Delete key
# Insert[@<time>] [number] Press the Insert key
# Down[@<time>] [number] Press the Down key
# Enter[@<time>] [number] Press the Enter key
# Space[@<time>] [number] Press the Space key
Expand Down
8 changes: 8 additions & 0 deletions examples/fixtures/all.tape
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,14 @@ Backspace
Backspace 2
Backspace@1 3
Delete
Delete 2
Delete@1 3
Insert
Insert 2
Insert@1 3
Down
Down 2
Down@1 3
Expand Down
14 changes: 14 additions & 0 deletions lexer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,20 @@ func TestLexTapeFile(t *testing.T) {
{AT, "@"},
{NUMBER, "1"},
{NUMBER, "3"},
{DELETE, "Delete"},
{DELETE, "Delete"},
{NUMBER, "2"},
{DELETE, "Delete"},
{AT, "@"},
{NUMBER, "1"},
{NUMBER, "3"},
{INSERT, "Insert"},
{INSERT, "Insert"},
{NUMBER, "2"},
{INSERT, "Insert"},
{AT, "@"},
{NUMBER, "1"},
{NUMBER, "3"},
{DOWN, "Down"},
{DOWN, "Down"},
{NUMBER, "2"},
Expand Down
2 changes: 2 additions & 0 deletions man.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ The following is a list of all possible commands in VHS:
* %Type% "<string>"
* %Ctrl% [+Alt][+Shift]+<char>
* %Backspace% [repeat]
* %Delete% [repeat]
* %Insert% [repeat]
* %Down% [repeat]
* %Enter% [repeat]
* %Left% [repeat]
Expand Down
2 changes: 1 addition & 1 deletion parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ func (p *Parser) Parse() []Command {
// parseCommand parses a command.
func (p *Parser) parseCommand() Command {
switch p.cur.Type {
case SPACE, BACKSPACE, ENTER, ESCAPE, TAB, DOWN, LEFT, RIGHT, UP, PAGEUP, PAGEDOWN:
case SPACE, BACKSPACE, DELETE, INSERT, ENTER, ESCAPE, TAB, DOWN, LEFT, RIGHT, UP, PAGEUP, PAGEDOWN:
return p.parseKeypress(p.cur.Type)
case SET:
return p.parseSet()
Expand Down
10 changes: 10 additions & 0 deletions parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ [email protected] 5
[email protected] 5
Backspace@1 5
Backspace@100ms 5
Delete 2
Insert 2
Right 3
Left 3
Up@50ms
Expand All @@ -33,6 +35,8 @@ Sleep 3`
{Type: BACKSPACE, Options: ".1s", Args: "5"},
{Type: BACKSPACE, Options: "1s", Args: "5"},
{Type: BACKSPACE, Options: "100ms", Args: "5"},
{Type: DELETE, Options: "", Args: "2"},
{Type: INSERT, Options: "", Args: "2"},
{Type: RIGHT, Options: "", Args: "3"},
{Type: LEFT, Options: "", Args: "3"},
{Type: UP, Options: "50ms", Args: "1"},
Expand Down Expand Up @@ -134,6 +138,12 @@ func TestParseTapeFile(t *testing.T) {
{Type: BACKSPACE, Options: "", Args: "1"},
{Type: BACKSPACE, Options: "", Args: "2"},
{Type: BACKSPACE, Options: "1s", Args: "3"},
{Type: DELETE, Options: "", Args: "1"},
{Type: DELETE, Options: "", Args: "2"},
{Type: DELETE, Options: "1s", Args: "3"},
{Type: INSERT, Options: "", Args: "1"},
{Type: INSERT, Options: "", Args: "2"},
{Type: INSERT, Options: "1s", Args: "3"},
{Type: DOWN, Options: "", Args: "1"},
{Type: DOWN, Options: "", Args: "2"},
{Type: DOWN, Options: "1s", Args: "3"},
Expand Down
2 changes: 2 additions & 0 deletions token.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,8 @@ var keywords = map[string]TokenType{
"Enter": ENTER,
"Space": SPACE,
"Backspace": BACKSPACE,
"Delete": DELETE,
"Insert": INSERT,
"Ctrl": CTRL,
"Alt": ALT,
"Shift": SHIFT,
Expand Down

0 comments on commit 1de813f

Please sign in to comment.