Skip to content

Instantly share code, notes, and snippets.

@JadenGeller
Created January 17, 2016 07:33
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JadenGeller/72602d7073e4339980cd to your computer and use it in GitHub Desktop.
Save JadenGeller/72602d7073e4339980cd to your computer and use it in GitHub Desktop.
Swift wrapper around GNU readline
// #include <readline/readline.h>
extension String {
private init(pointer: UnsafePointer<Int8>) {
self.init()
var p = pointer
while p.memory != 0 {
append(Character(UnicodeScalar(unsafeBitCast(p.memory, UInt8.self))))
p = p.successor()
}
}
}
func readLine(prompt: String = "", addHistory: Bool = false) -> String? {
let line = readline(prompt)
guard line != nil else { return nil }
defer { free(line) }
if addHistory { add_history(line) }
return String(pointer: line)
}
while let line = readLine("> ", addHistory: true) {
print(line)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment