-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
xterm.js keyboard input plus example
- Loading branch information
Showing
7 changed files
with
129 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
laika.navigationOrder = [ | ||
README.md | ||
examples.md | ||
jvm.md | ||
js.md | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
# Examples | ||
|
||
@:doodle("prompt", "Prompt.go") | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
package terminus.examples | ||
|
||
import terminus.* | ||
import scala.scalajs.js.annotation.* | ||
import scala.concurrent.Future | ||
import scala.concurrent.ExecutionContext.Implicits.global | ||
|
||
@JSExportTopLevel("Prompt") | ||
object Prompt { | ||
enum KeyCode { | ||
case Down | ||
case Up | ||
case Enter | ||
} | ||
|
||
// Clear the text we've written | ||
def clear(): Program[Unit] = { | ||
Terminal.cursor.move(1, -4) | ||
Terminal.erase.down() | ||
Terminal.cursor.column(1) | ||
} | ||
|
||
// Write an option the user can choose. The currently selected option is highlighted. | ||
def writeChoice(description: String, selected: Boolean): Program[Unit] = | ||
if selected then | ||
Terminal.display.bold(Terminal.write(s"> ${description}\r\n")) | ||
else Terminal.write(s" ${description}\r\n") | ||
|
||
// Write the UI | ||
def write(selected: Int): Program[Unit] = { | ||
Terminal.write("How cool is this?\r\n") | ||
writeChoice("Very cool", selected == 0) | ||
writeChoice("Way cool", selected == 1) | ||
writeChoice("So cool", selected == 2) | ||
Terminal.flush() | ||
} | ||
|
||
// Convert Javascript key to KeyCode | ||
// See https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent/key | ||
def read(): Program[Future[KeyCode]] = { | ||
Terminal.readKey().flatMap( keyCode => | ||
keyCode match { | ||
case "Enter" => Future.successful(KeyCode.Enter) | ||
case "ArrowDown" => Future.successful(KeyCode.Down) | ||
case "ArrowUp" => Future.successful(KeyCode.Up) | ||
case other => read() | ||
} | ||
) | ||
} | ||
|
||
def loop(idx: Int): Program[Future[Int]] = { | ||
write(idx) | ||
read().flatMap(keyCode => | ||
keyCode match { | ||
case KeyCode.Up => | ||
clear() | ||
loop(if idx == 0 then 2 else idx - 1) | ||
|
||
case KeyCode.Down => | ||
clear() | ||
loop(if idx == 2 then 0 else idx + 1) | ||
|
||
case KeyCode.Enter => Future.successful(idx) | ||
} | ||
) | ||
} | ||
@JSExport | ||
def go(id: String) = | ||
Terminal.run(id, rows = 16) { | ||
loop(0).map(idx => | ||
Terminal.write(s"You selected $idx") | ||
) | ||
} | ||
} |