Termios

改进为 termios 自动生成的绑定,增加类型安全性和 Swift 风格

灵感

许多想法借鉴自 japaric/termios.rs

安装

SPM (Swift Package Manager)

.package(url: "https://github.com/Ponyboy47/Termios.git", from: "0.1.0")

用法

import Termios

let password: String
print("Please enter the account password: ", terminator: "")
do {
    // Save the current terminal state
    var old = try Termios.fetch(fd: STDIN_FILENO)

    // Update the current terminal state to turn off echo
    var new = old
    new.localFlags.formSymmetricDifference([.echo, .canonical])
    try new.update(fd: STDIN_FILENO)

    // Ensure that the original terminal state is restored upon scope completion
    defer {
        do {
            try old.update(fd: STDIN_FILENO)
        } catch {
            fatalError("Failed to reenable stdin echo with error: \(error)")
        }
    }

    // Read input securely (without echoing characters to stdout)
    guard let _pass = readLine() else {
        fatalError("Swift process does not have a controlling terminal and cannot prompt for input")
    }
    password = _pass
} catch {
    fatalError("Failed to disable stdin echo with error: \(error)")
}

// Print a newline because the return key pressed by the user is not echoed
print()

print("The user entered \(password) as their secure input")

许可

Termios 在 MIT 许可下发布。