This repository has been archived by the owner on Oct 22, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 49
/
UnixConsoleManager.kt
78 lines (60 loc) · 2.64 KB
/
UnixConsoleManager.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
/*
Copyright 2017-2020 Charles Korn.
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 batect.os.unix
import batect.logging.Logger
import batect.os.ConsoleManager
import batect.os.ProcessRunner
import batect.ui.ConsoleInfo
class UnixConsoleManager(
private val consoleInfo: ConsoleInfo,
private val processRunner: ProcessRunner,
private val logger: Logger
) : ConsoleManager {
override fun enableConsoleEscapeSequences() {
// Nothing to do on Linux or macOS.
}
override fun enterRawMode(): AutoCloseable {
if (!consoleInfo.stdinIsTTY) {
logger.info {
message("Terminal is not a TTY, won't enter raw mode.")
}
return AutoCloseable { }
}
val existingState = getExistingTerminalState()
startRawMode()
return TerminalStateRestorer(existingState, processRunner)
}
private fun getExistingTerminalState(): String {
val output = processRunner.runWithStdinAttached(listOf("stty", "-g"))
if (output.exitCode != 0) {
throw RuntimeException("Invoking 'stty -g' failed with exit code ${output.exitCode}: ${output.output.trim()}")
}
return output.output.trim()
}
private fun startRawMode() {
val command = listOf("stty", "-ignbrk", "-brkint", "-parmrk", "-istrip", "-inlcr", "-igncr", "-icrnl", "-ixon", "-opost", "-echo", "-echonl",
"-icanon", "-isig", "-iexten", "-parenb", "cs8", "min", "1", "time", "0")
val output = processRunner.runWithStdinAttached(command)
if (output.exitCode != 0) {
throw RuntimeException("Invoking '${command.joinToString(" ")}' failed with exit code ${output.exitCode}: ${output.output.trim()}")
}
}
}
data class TerminalStateRestorer(private val oldState: String, private val processRunner: ProcessRunner) : AutoCloseable {
override fun close() {
val output = processRunner.runWithStdinAttached(listOf("stty", oldState))
if (output.exitCode != 0) {
throw RuntimeException("Invoking 'stty $oldState' failed with exit code ${output.exitCode}: ${output.output.trim()}")
}
}
}