scoop:scoop.terminal:0.1.0
src/raw_scope.scoop
Selected production source · src/raw_scope.scoop
1
package scoop.terminal3
/**4
* Runs the block with raw mode enabled and restores the previous raw-mode state on normal or5
* exceptional exit.6
*/7
public fun <R> TerminalSession.withRawMode(block: () -> R): R {8
val scope = RawModeScope(this)9
scope.enter()10
return scope.use { block() }11
}13
private class RawModeScope(private val session: TerminalSession) : Closeable {14
private var changed: Boolean = false15
fun enter() {16
session.withTerminal { terminal ->17
if (!terminal.isRaw) {18
terminal.setRaw(true)19
changed = true20
}21
Unit22
}23
}24
public override fun close() {25
if (changed && !session.isClosed) session.disableRawMode()26
changed = false27
}28
}