-
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Implement and document bindings for xterm.js, providing most of the Terminus API plus some JS specific parts. * Use this implementation to add examples to the documentation
- Loading branch information
Showing
16 changed files
with
676 additions
and
134 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
/* | ||
* Copyright 2024 Creative Scala | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package terminus | ||
|
||
import org.scalajs.dom | ||
import org.scalajs.dom.HTMLElement | ||
|
||
import scala.collection.mutable | ||
import scala.concurrent.Future | ||
import scala.concurrent.Promise | ||
|
||
class Terminal(root: HTMLElement, options: XtermJsOptions) | ||
extends effect.Color[Terminal], | ||
effect.Cursor, | ||
effect.Display[Terminal], | ||
effect.Erase, | ||
effect.Writer { | ||
|
||
private val keyBuffer: mutable.ArrayDeque[Promise[String]] = | ||
new mutable.ArrayDeque[Promise[String]](8) | ||
|
||
private val terminal = new XtermJsTerminal(options) | ||
terminal.open(root) | ||
terminal.onKey { (event: XtermKeyEvent) => | ||
keyBuffer.removeHead().success(event.domEvent.key) | ||
() | ||
} | ||
|
||
/** Block reading a Javascript keycode */ | ||
def readKey(): Future[String] = { | ||
val promise = Promise[String]() | ||
keyBuffer.append(promise) | ||
|
||
promise.future | ||
} | ||
|
||
def flush(): Unit = () | ||
|
||
def write(string: String): Unit = | ||
terminal.write(string) | ||
|
||
def write(char: Char): Unit = | ||
terminal.write(char.toString()) | ||
} | ||
type Program[A] = Terminal ?=> A | ||
|
||
object Terminal extends Color, Cursor, Display, Erase, Writer { | ||
def readKey(): Program[Future[String]] = | ||
terminal ?=> terminal.readKey() | ||
|
||
def run[A](id: String, rows: Int = 24, cols: Int = 80)(f: Program[A]): A = { | ||
val options = XtermJsOptions(rows, cols) | ||
run(dom.document.getElementById(id).asInstanceOf[HTMLElement], options)(f) | ||
} | ||
|
||
def run[A](element: HTMLElement, options: XtermJsOptions)( | ||
f: Program[A] | ||
): A = { | ||
val terminal = Terminal(element, options) | ||
f(using terminal) | ||
} | ||
} |
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,29 @@ | ||
/* | ||
* Copyright 2024 Creative Scala | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package terminus | ||
|
||
import scala.scalajs.js | ||
|
||
@js.native | ||
trait XtermJsOptions extends js.Object { | ||
val cols: Int = js.native | ||
val rows: Int = js.native | ||
} | ||
object XtermJsOptions { | ||
def apply(rows: Int = 80, cols: Int = 24): XtermJsOptions = | ||
js.Dynamic.literal(rows = rows, cols = cols).asInstanceOf[XtermJsOptions] | ||
} |
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,39 @@ | ||
/* | ||
* Copyright 2024 Creative Scala | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package terminus | ||
|
||
import org.scalajs.dom | ||
|
||
import scala.annotation.unused | ||
import scala.scalajs.js | ||
import scala.scalajs.js.annotation.JSGlobal | ||
import org.scalajs.dom.KeyboardEvent | ||
|
||
@js.native | ||
trait XtermKeyEvent extends js.Object { | ||
val key: String = js.native | ||
val domEvent: KeyboardEvent = js.native | ||
} | ||
|
||
@js.native | ||
@JSGlobal("Terminal") | ||
/** Minimal definition of the Terminal type from xterm.js */ | ||
class XtermJsTerminal(@unused options: XtermJsOptions) extends js.Object { | ||
val onKey: js.Function1[js.Function1[XtermKeyEvent, Unit], Unit] = js.native | ||
def open(element: dom.HTMLElement): Unit = js.native | ||
def write(data: String): Unit = js.native | ||
} |
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
Oops, something went wrong.