Scoop API

scoop:scoop.terminal:0.1.0

src/raw_scope.scoop

Selected production source · src/raw_scope.scoop

1package scoop.terminal
3/**
4 * Runs the block with raw mode enabled and restores the previous raw-mode state on normal or
5 * exceptional exit.
6 */
7public fun <R> TerminalSession.withRawMode(block: () -> R): R {
8 val scope = RawModeScope(this)
9 scope.enter()
10 return scope.use { block() }
13private class RawModeScope(private val session: TerminalSession) : Closeable {
14 private var changed: Boolean = false
15 fun enter() {
16 session.withTerminal { terminal ->
17 if (!terminal.isRaw) {
18 terminal.setRaw(true)
19 changed = true
20 }
21 Unit
22 }
23 }
24 public override fun close() {
25 if (changed && !session.isClosed) session.disableRawMode()
26 changed = false
27 }