Skip to content

Commit

Permalink
Refactor command execution
Browse files Browse the repository at this point in the history
Implement Repl#clear & Repl#close methods.
  • Loading branch information
vladimyr committed Jun 9, 2020
1 parent 6320514 commit ec4e893
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 26 deletions.
37 changes: 24 additions & 13 deletions src/module/repl.wren
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import "os" for Platform
/// does not render.
class Repl {
construct new() {
_closed = false

_cursor = 0
_line = ""

Expand All @@ -24,11 +26,15 @@ class Repl {

while (true) {
var byte = Stdin.readByte()
if (handleChar(byte)) break
if (handleChar(byte) || _closed) break
refreshLine(true)
}
}

close() {
_closed = true
}

handleChar(byte) {
if (byte == Chars.ctrlC) {
System.print()
Expand Down Expand Up @@ -66,9 +72,7 @@ class Repl {
// TODO: Handle ESC 0 sequences.
}
} else if (byte == Chars.carriageReturn) {
var next = executeInput()
if (next == Chars.ctrlD) return true
if (next != null) handleChar(next)
executeInput()
} else if (byte == Chars.delete) {
deleteLeft()
} else if (byte >= Chars.space && byte <= Chars.tilde) {
Expand All @@ -90,6 +94,10 @@ class Repl {
return false
}

clear() {
// Stub overriden by subclasses.
}

/// Inserts the character with [byte] value at the current cursor position.
insertChar(byte) {
var char = String.fromCodePoint(byte)
Expand Down Expand Up @@ -215,18 +223,17 @@ class Repl {
}
}

executeCommand(command, arguments) {
executeCommand(command, argument) {
if (command == Command.clear) {
return Chars.ctrlL
return clear()
}

if (command == Command.exit) {
return Chars.ctrlD
return close()
}

if (command == Command.help) {
System.print(Command.help())
return
return System.print(Command.help())
}

if (command == Command.save) {
Expand Down Expand Up @@ -360,10 +367,7 @@ class AnsiRepl is Repl {
// Delete everything after the cursor.
line = line[0...cursor]
} else if (byte == Chars.ctrlL) {
// Clear the screen.
System.write("\x1b[2J")
// Move cursor to top left.
System.write("\x1b[H")
clear()
} else {
// TODO: Ctrl-T to swap chars.
// TODO: ESC H and F to move to beginning and end of line. (Both ESC
Expand All @@ -375,6 +379,13 @@ class AnsiRepl is Repl {
return false
}

clear() {
// Clear the screen.
System.write("\x1b[2J")
// Move cursor to top left.
System.write("\x1b[H")
}

handleEscapeBracket(byte) {
if (byte == EscapeBracket.left) {
cursorLeft()
Expand Down
37 changes: 24 additions & 13 deletions src/module/repl.wren.inc
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ static const char* replModuleSource =
"/// does not render.\n"
"class Repl {\n"
" construct new() {\n"
" _closed = false\n"
"\n"
" _cursor = 0\n"
" _line = \"\"\n"
"\n"
Expand All @@ -26,11 +28,15 @@ static const char* replModuleSource =
"\n"
" while (true) {\n"
" var byte = Stdin.readByte()\n"
" if (handleChar(byte)) break\n"
" if (handleChar(byte) || _closed) break\n"
" refreshLine(true)\n"
" }\n"
" }\n"
"\n"
" close() {\n"
" _closed = true\n"
" }\n"
"\n"
" handleChar(byte) {\n"
" if (byte == Chars.ctrlC) {\n"
" System.print()\n"
Expand Down Expand Up @@ -68,9 +74,7 @@ static const char* replModuleSource =
" // TODO: Handle ESC 0 sequences.\n"
" }\n"
" } else if (byte == Chars.carriageReturn) {\n"
" var next = executeInput()\n"
" if (next == Chars.ctrlD) return true\n"
" if (next != null) handleChar(next)\n"
" executeInput()\n"
" } else if (byte == Chars.delete) {\n"
" deleteLeft()\n"
" } else if (byte >= Chars.space && byte <= Chars.tilde) {\n"
Expand All @@ -92,6 +96,10 @@ static const char* replModuleSource =
" return false\n"
" }\n"
"\n"
" clear() {\n"
" // Stub overriden by subclasses.\n"
" }\n"
"\n"
" /// Inserts the character with [byte] value at the current cursor position.\n"
" insertChar(byte) {\n"
" var char = String.fromCodePoint(byte)\n"
Expand Down Expand Up @@ -217,18 +225,17 @@ static const char* replModuleSource =
" }\n"
" }\n"
"\n"
" executeCommand(command, arguments) {\n"
" executeCommand(command, argument) {\n"
" if (command == Command.clear) {\n"
" return Chars.ctrlL\n"
" return clear()\n"
" }\n"
"\n"
" if (command == Command.exit) {\n"
" return Chars.ctrlD\n"
" return close()\n"
" }\n"
"\n"
" if (command == Command.help) {\n"
" System.print(Command.help())\n"
" return\n"
" return System.print(Command.help())\n"
" }\n"
"\n"
" if (command == Command.save) {\n"
Expand Down Expand Up @@ -362,10 +369,7 @@ static const char* replModuleSource =
" // Delete everything after the cursor.\n"
" line = line[0...cursor]\n"
" } else if (byte == Chars.ctrlL) {\n"
" // Clear the screen.\n"
" System.write(\"\x1b[2J\")\n"
" // Move cursor to top left.\n"
" System.write(\"\x1b[H\")\n"
" clear()\n"
" } else {\n"
" // TODO: Ctrl-T to swap chars.\n"
" // TODO: ESC H and F to move to beginning and end of line. (Both ESC\n"
Expand All @@ -377,6 +381,13 @@ static const char* replModuleSource =
" return false\n"
" }\n"
"\n"
" clear() {\n"
" // Clear the screen.\n"
" System.write(\"\x1b[2J\")\n"
" // Move cursor to top left.\n"
" System.write(\"\x1b[H\")\n"
" }\n"
"\n"
" handleEscapeBracket(byte) {\n"
" if (byte == EscapeBracket.left) {\n"
" cursorLeft()\n"
Expand Down

0 comments on commit ec4e893

Please sign in to comment.