它的功能非常简单:从终端读取输入,一次读取一个字符,无需用户按下回车键,也不会回显字符。
使用方法很简单:首先调用 enableRawMode()
启用原始模式,然后重复调用 readChar()
逐个读取字符。最后,在完成时调用 disableRawMode()
禁用原始模式。
它支持 Unicode,包括表情符号。 如果您收到无效字符,请确保您的终端配置为使用 UTF8 编码。
不要在 Xcode 或 Swift Playgrounds 中使用终端进行测试,请使用功能齐全的独立终端。
// Add to your package dependencies
.package(url: "https://github.com/Swonkie/RawTerm.git", from: "0.1.0")
// Add to your target dependencies
.product(name: "RawTerm", package: "RawTerm")
// Import the module in your code
import RawTerm
一个简单的拼写辅助工具
import RawTerm
@main
struct Spell {
static func main() throws {
print("Type or paste some text\nPress Esc to quit\n")
try enableRawMode()
defer {
try! disableRawMode()
}
while true {
// print requires an extra \r at the end when in raw mode
guard let input = try? readChar() else {
print("Invalid input - make sure the terminal uses UTF8.\r")
continue
}
if input == CharCode.ESC {
break
}
if let spelling = dict[input.lowercased()] {
print("\(input) \(spelling)\r")
} else {
print("\(input) \(input)\r")
}
}
}
static let dict = [
"a": "Alpha",
"b": "Bravo",
"c": "Charlie",
"d": "Delta",
"e": "Echo",
"f": "Foxtrott",
"g": "Golf",
"h": "Hotel",
"i": "India",
"j": "Juliett",
"k": "Kilo",
"l": "Lima",
"m": "Mike",
"n": "November",
"o": "Oscar",
"p": "Papa",
"q": "Quebec",
"r": "Romeo",
"s": "Sierra",
"t": "Tango",
"u": "Uniform",
"v": "Victor",
"w": "Whiskey",
"x": "X-Ray",
"y": "Yankee",
"z": "Zulu",
]
}
Type or paste some text
Press Esc to quit
R Romeo
a Alpha
w Whiskey
I India
n November
p Papa
u Uniform
t Tango
! !
🥸 🥸