Created
January 17, 2016 07:33
-
-
Save JadenGeller/72602d7073e4339980cd to your computer and use it in GitHub Desktop.
Swift wrapper around GNU readline
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// #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