Skip to content

Commit

Permalink
add support for function keys
Browse files Browse the repository at this point in the history
  • Loading branch information
pyaillet committed Dec 27, 2023
1 parent 7dd24a2 commit d0877f9
Show file tree
Hide file tree
Showing 7 changed files with 80 additions and 3 deletions.
24 changes: 24 additions & 0 deletions command.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,18 @@ var CommandTypes = []CommandType{ //nolint: deadcode
LEFT,
PAGEUP,
PAGEDOWN,
F1,
F2,
F3,
F4,
F5,
F6,
F7,
F8,
F9,
F10,
F11,
F12,
RIGHT,
SET,
OUTPUT,
Expand Down Expand Up @@ -77,6 +89,18 @@ var CommandFuncs = map[CommandType]CommandFunc{
ESCAPE: ExecuteKey(input.Escape),
PAGEUP: ExecuteKey(input.PageUp),
PAGEDOWN: ExecuteKey(input.PageDown),
F1: ExecuteKey(input.F1),
F2: ExecuteKey(input.F2),
F3: ExecuteKey(input.F3),
F4: ExecuteKey(input.F4),
F5: ExecuteKey(input.F5),
F6: ExecuteKey(input.F6),
F7: ExecuteKey(input.F7),
F8: ExecuteKey(input.F8),
F9: ExecuteKey(input.F9),
F10: ExecuteKey(input.F10),
F11: ExecuteKey(input.F11),
F12: ExecuteKey(input.F12),
HIDE: ExecuteHide,
REQUIRE: ExecuteRequire,
SHOW: ExecuteShow,
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 = 27
const numberOfCommands = 39
if len(CommandTypes) != numberOfCommands {
t.Errorf("Expected %d commands, got %d", numberOfCommands, len(CommandTypes))
}

const numberOfCommandFuncs = 27
const numberOfCommandFuncs = 39
if len(CommandFuncs) != numberOfCommandFuncs {
t.Errorf("Expected %d commands, got %d", numberOfCommandFuncs, len(CommandFuncs))
}
Expand Down
8 changes: 8 additions & 0 deletions examples/fixtures/all.tape
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,14 @@ PageDown
PageDown 2
PageDown@1 3

F1
F1 2
F1@1 3

F12
F12 2
F12@1 3

Enter
Enter 2
Enter@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 @@ -216,6 +216,20 @@ func TestLexTapeFile(t *testing.T) {
{AT, "@"},
{NUMBER, "1"},
{NUMBER, "3"},
{F1, "F1"},
{F1, "F1"},
{NUMBER, "2"},
{F1, "F1"},
{AT, "@"},
{NUMBER, "1"},
{NUMBER, "3"},
{F12, "F12"},
{F12, "F12"},
{NUMBER, "2"},
{F12, "F12"},
{AT, "@"},
{NUMBER, "1"},
{NUMBER, "3"},
{ENTER, "Enter"},
{ENTER, "Enter"},
{NUMBER, "2"},
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, DELETE, INSERT, ENTER, ESCAPE, TAB, DOWN, LEFT, RIGHT, UP, PAGEUP, PAGEDOWN:
case SPACE, BACKSPACE, DELETE, INSERT, ENTER, ESCAPE, TAB, DOWN, LEFT, RIGHT, UP, PAGEUP, PAGEDOWN, F1, F2, F3, F4, F5, F6, F7, F8, F9, F10, F11, F12:
return p.parseKeypress(p.cur.Type)
case SET:
return p.parseSet()
Expand Down
6 changes: 6 additions & 0 deletions parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,12 @@ func TestParseTapeFile(t *testing.T) {
{Type: PAGEDOWN, Options: "", Args: "1"},
{Type: PAGEDOWN, Options: "", Args: "2"},
{Type: PAGEDOWN, Options: "1s", Args: "3"},
{Type: F1, Options: "", Args: "1"},
{Type: F1, Options: "", Args: "2"},
{Type: F1, Options: "1s", Args: "3"},
{Type: F12, Options: "", Args: "1"},
{Type: F12, Options: "", Args: "2"},
{Type: F12, Options: "1s", Args: "3"},
{Type: ENTER, Options: "", Args: "1"},
{Type: ENTER, Options: "", Args: "2"},
{Type: ENTER, Options: "1s", Args: "3"},
Expand Down
25 changes: 25 additions & 0 deletions token.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,18 @@ const (
HOME = "HOME"
INSERT = "INSERT"
PAGEDOWN = "PAGEDOWN"
F1 = "F1"
F2 = "F2"
F3 = "F3"
F4 = "F4"
F5 = "F5"
F6 = "F6"
F7 = "F7"
F8 = "F8"
F9 = "F9"
F10 = "F10"
F11 = "F11"
F12 = "F12"
PAGEUP = "PAGEUP"
SLEEP = "SLEEP"
SPACE = "SPACE"
Expand Down Expand Up @@ -115,6 +127,18 @@ var keywords = map[string]TokenType{
"Up": UP,
"PageUp": PAGEUP,
"PageDown": PAGEDOWN,
"F1": F1,
"F2": F2,
"F3": F3,
"F4": F4,
"F5": F5,
"F6": F6,
"F7": F7,
"F8": F8,
"F9": F9,
"F10": F10,
"F11": F11,
"F12": F12,
"Tab": TAB,
"Escape": ESCAPE,
"End": END,
Expand Down Expand Up @@ -167,6 +191,7 @@ func IsCommand(t TokenType) bool {
switch t {
case TYPE, SLEEP,
UP, DOWN, RIGHT, LEFT, PAGEUP, PAGEDOWN,
F1, F2, F3, F4, F5, F6, F7, F8, F9, F10, F11, F12,
ENTER, BACKSPACE, DELETE, TAB,
ESCAPE, HOME, INSERT, END, CTRL, SOURCE, SCREENSHOT, COPY, PASTE:
return true
Expand Down

0 comments on commit d0877f9

Please sign in to comment.