Swift Package Index


Type-safe Swift wrapper for termios


  • The latest stable release is 0.1.1. Released over 1 year ago.
  • The last commit to master was over 1 year ago.

Swift Version Compatibility

  • 0.1.1
    5.3
    5.2
    5.1
    5.0
    4.2
  • master
    5.3
    5.2
    5.1
    5.0
    4.2
Full build results

Platform Compatibility

  • 0.1.1 and master
    iOS
    macOS(Intel)
    macOS(ARM)
    Linux
    tvOS
    watchOS
Full build results

Termios

Improving the auto-generated bindings for termios with additional type-safety and Swift-isms.

Inspiration

Lots of ideas were borrowed from japaric/termios.rs.

Installation

SPM:

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

Usage

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")

License

Termios is released under the MIT license.