Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

implement 'hideInput' on posix systems #63

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 4 additions & 4 deletions mordant/build.gradle.kts
Expand Up @@ -64,13 +64,13 @@ kotlin {
}

val nativeMain by creating { dependsOn(commonMain) }
val macosMain by creating { dependsOn(nativeMain) }
val mingwX64Main by getting { dependsOn(nativeMain) }
val posixMain by creating { dependsOn(nativeMain) }
val linuxX64Main by getting { dependsOn(posixMain) }
val macosMain by creating { dependsOn(posixMain) }
listOf("macosX64", "macosArm64").forEach { target ->
getByName(target + "Main").dependsOn(macosMain)
}
listOf("macos", "linuxX64", "mingwX64").forEach { target ->
getByName(target + "Main").dependsOn(nativeMain)
}

targets.withType<KotlinNativeTargetWithTests<*>> {
binaries {
Expand Down
@@ -0,0 +1,6 @@
package com.github.ajalt.mordant.internal

// hideInput is not currently implemented
internal actual fun ttySetEcho(echo: Boolean) {

}
Expand Up @@ -54,9 +54,19 @@ internal actual fun printStderr(message: String, newline: Boolean) {
fflush(stderr)
}

internal expect fun ttySetEcho(echo: Boolean)

// hideInput is not currently implemented
internal actual fun readLineOrNullMpp(hideInput: Boolean): String? = readlnOrNull()
internal actual fun readLineOrNullMpp(hideInput: Boolean): String? {
if (hideInput) {
ttySetEcho(false)
}
val lineOrNull = readlnOrNull()
if (hideInput) {
ttySetEcho(true)
}

return lineOrNull
}

internal actual fun makePrintingTerminalCursor(terminal: Terminal): TerminalCursor = NativeTerminalCursor(terminal)

Expand Down
@@ -0,0 +1,30 @@
package com.github.ajalt.mordant.internal

import kotlinx.cinterop.*
import platform.posix.*

@OptIn(UnsafeNumber::class)
internal actual fun ttySetEcho(echo: Boolean) {
updateTermios {
c_lflag = if (echo) {
c_lflag.or(ECHO.convert())
} else {
c_lflag.and(ECHO.inv().convert())
}
}
}

private fun updateTermios(block: termios.() -> Unit) {
memScoped {
val termios = alloc<termios>()
check(tcgetattr(0, termios.ptr) == 0) {
error("tcgetattr() error: $errno")
}

block(termios)

check(tcsetattr(0, TCSADRAIN, termios.ptr) == 0) {
error("tcsetattr() error: $errno")
}
}
}