Skip to content

Commit

Permalink
Add browser-specific implementation of TerminalCursor
Browse files Browse the repository at this point in the history
  • Loading branch information
ajalt committed Feb 21, 2024
1 parent 13fce2b commit bf4ac35
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 5 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

### Fixed
- Vertical layout now correctly pads non-text cells when `align` is set to `TextAlign.LEFT`
- Fixed exception when hiding the cursor on browsers on JS target.

## 2.3.0
### Added
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ private class JsAtomicRef<T>(override var value: T) : MppAtomicRef<T> {
}
}

private class JsAtomicInt(initial: Int) : MppAtomicInt{
private class JsAtomicInt(initial: Int) : MppAtomicInt {
private var backing = initial
override fun getAndIncrement(): Int {
return backing++
Expand All @@ -48,6 +48,7 @@ private interface JsMppImpls {
fun getTerminalSize(): Size?
fun printStderr(message: String, newline: Boolean)
fun readLineOrNull(): String?
fun makeTerminalCursor(terminal: Terminal): TerminalCursor
}

private object BrowserMppImpls : JsMppImpls {
Expand All @@ -63,6 +64,9 @@ private object BrowserMppImpls : JsMppImpls {

// readlnOrNull will just throw an exception on browsers
override fun readLineOrNull(): String? = readlnOrNull()
override fun makeTerminalCursor(terminal: Terminal): TerminalCursor {
return BrowserTerminalCursor(terminal)
}
}

private class NodeMppImpls(private val fs: dynamic) : JsMppImpls {
Expand All @@ -75,7 +79,7 @@ private class NodeMppImpls(private val fs: dynamic) : JsMppImpls {
// is false
if (process.stdout.getWindowSize == undefined) return null
val s = process.stdout.getWindowSize()
return Size(width = s[0] as Int, height = s[1] as Int)
return Size(width = s[0] as Int, height = s[1] as Int)
}

override fun printStderr(message: String, newline: Boolean) {
Expand All @@ -98,6 +102,10 @@ private class NodeMppImpls(private val fs: dynamic) : JsMppImpls {
null
}
}

override fun makeTerminalCursor(terminal: Terminal): TerminalCursor {
return NodeTerminalCursor(terminal)
}
}

private val impls: JsMppImpls = try {
Expand All @@ -112,7 +120,9 @@ internal actual fun getTerminalSize(): Size? = impls.getTerminalSize()
internal actual fun getEnv(key: String): String? = impls.readEnvvar(key)
internal actual fun stdoutInteractive(): Boolean = impls.stdoutInteractive()
internal actual fun stdinInteractive(): Boolean = impls.stdinInteractive()
internal actual fun printStderr(message: String, newline: Boolean) = impls.printStderr(message, newline)
internal actual fun printStderr(message: String, newline: Boolean) {
impls.printStderr(message, newline)
}

// hideInput is not currently implemented
internal actual fun readLineOrNullMpp(hideInput: Boolean): String? = impls.readLineOrNull()
Expand All @@ -125,9 +135,11 @@ internal actual fun codepointSequence(string: String): Sequence<Int> {
}
}

internal actual fun makePrintingTerminalCursor(terminal: Terminal): TerminalCursor = JsTerminalCursor(terminal)
internal actual fun makePrintingTerminalCursor(terminal: Terminal): TerminalCursor {
return impls.makeTerminalCursor(terminal)
}

private class JsTerminalCursor(terminal: Terminal) : PrintTerminalCursor(terminal) {
private class NodeTerminalCursor(terminal: Terminal) : PrintTerminalCursor(terminal) {
private var shutdownHook: (() -> Unit)? = null

override fun show() {
Expand All @@ -144,6 +156,9 @@ private class JsTerminalCursor(terminal: Terminal) : PrintTerminalCursor(termina
}
}

// There are no shutdown hooks on browsers, so we don't need to do anything here
private class BrowserTerminalCursor(terminal: Terminal) : PrintTerminalCursor(terminal)


internal actual fun sendInterceptedPrintRequest(
request: PrintRequest,
Expand Down

0 comments on commit bf4ac35

Please sign in to comment.