我正在尝试基于Stack Overflow答案创建一个快速的框架(我功劳。我正在将其用于个人用途)。在命令行工具中使用它时出现错误。代码中的注释是显示的错误。 我的框架代码:
import Foundation
public func getPass(_ prompt: String) -> String {
var result = ""
let buf = [Int8](repeating: 0, count: 8192)
if let passphrase = readpassphrase("Enter passphrase: ", (UnsafeMutablePointer<Int8>)(mutating: buf), buf.count, 0),
let passphraseStr = String.init(validatingUTF8: passphrase) {
result = passphraseStr
}
return result
}
public func setColor(_ color: String) {
let colorDictionary = ["black":"30", "red":"31", "green":"32", "yellow":"33", "blue":"34", "magenta":"35", "cyan":"36","white":"37"]
let colorResult = colorDictionary[color]
print("\u{001B}[0;" + colorResult! + "m", terminator: "")
}
public class TerminalProgressBar {
private var progress : String
public init(_ input: String) {
print(input + "\r", terminator:"")
fflush(__stdoutp)
progress = input
}
public func setStatus(_ input: String) {
clearLine()
print(input + "\r", terminator:"")
fflush(__stdoutp)
progress = input
}
public func clearLine() {
var status = ""
if 2 <= progress.count {
for _ in 1...progress.count {
status = status + " "
}
} else {
status = " "
}
print(status + "\r", terminator:"")
fflush(__stdoutp)
print("\r", terminator:"")
fflush(__stdoutp)
}
public func restoreLine() {
clearLine()
print(progress + "\r", terminator:"")
fflush(__stdoutp)
}
public func endProgress() {
progress = ""
print("")
}
private func getCount() -> Int {
return progress.count
}
}
我的命令行工具代码:
import Foundation
import CommandLine
var progress = TerminalProgressBar("abcdef") // Error: 'TerminalProgressBar' initializer is inaccessible due to 'internal' protection level
sleep(2)
setColor("blue") // Error: Use of unresolved identifier 'setColor'
progress.setStatus("abc")
sleep(3)
progress.clearLine()
sleep(3)
print("a")
print("some console output")
sleep(3)
progress.restoreLine()
sleep(2)
progress.endProgress()
框架类型是可可框架(macOS)。 我测试了框架的项目类型:命令行工具。 库也不起作用。 注意:如果您尝试查找错误,请查看代码中的注释。 如何创建功能框架?