Skip to content

Commit

Permalink
Check environment for stty location (#74)
Browse files Browse the repository at this point in the history
  • Loading branch information
ajalt committed Oct 16, 2022
1 parent 863df04 commit 4087fda
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 11 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

### Fixed
- Terminal detection would sometimes incorrectly identify the process as running in IntelliJ [(#72)](https://github.com/ajalt/mordant/issues/72)
- `updateTerminalSize` would sometimes fail to find the `stty` command [(#66)](https://github.com/ajalt/mordant/issues/66)

## 2.0.0-beta7
### Added
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,20 +25,21 @@ internal actual class AtomicInt actual constructor(initial: Int) {
// We have to shell out to another program on JVM, which takes ~10ms for stty and ~100ms for powershell
internal actual fun terminalSizeDetectionIsFast(): Boolean = false

internal actual fun getTerminalSize(timeoutMs: Long): Pair<Int, Int>? {
val process = try {
val cmd = when {
isWindows() -> ProcessBuilder("powershell.exe",
"-noprofile",
"-command",
"\$host.ui.rawui")
else -> ProcessBuilder("stty", "size")
}
cmd.redirectInput(ProcessBuilder.Redirect.INHERIT)
private fun runCommand(vararg args: String): Process? {
return try {
ProcessBuilder(*args)
.redirectInput(ProcessBuilder.Redirect.INHERIT)
.start()
} catch (e: IOException) {
return null
null
}
}
internal actual fun getTerminalSize(timeoutMs: Long): Pair<Int, Int>? {
val process = when {
isWindows() -> runCommand("powershell.exe", "-noprofile", "-command", "\$host.ui.rawui")
// Try running stty both directly and via env, since neither one works on all systems
else -> runCommand("stty", "size") ?: runCommand("/use/bin/env", "stty", "size")
} ?: return null
try {
if (!process.waitFor(timeoutMs, TimeUnit.MILLISECONDS)) {
return null
Expand Down

0 comments on commit 4087fda

Please sign in to comment.