When using libedit/readline to insert a prompt and default response, be more careful to ensure the readline structures are initialized before proceeding

This commit is contained in:
Greg Neagle
2025-11-10 10:47:12 -08:00
parent 7c4a87670e
commit b08b5800bd

View File

@@ -32,12 +32,22 @@ func getInput(prompt: String? = nil, defaultText: String? = nil) -> String? {
// the readline implementation is so broken
let queue = OperationQueue()
let insertOperation = BlockOperation {
usleep(10000) // 0.01 seconds
rl_set_prompt(prompt ?? "")
if let defaultText {
rl_insert_text(defaultText)
var counter = 10
while counter > 0 {
// sleep until rl_line_buffer is initialized
usleep(10000) // 0.01 second
if rl_line_buffer != nil {
// rl_line_buffer is initialized --
// set prompt and insert default text
rl_set_prompt(prompt ?? "")
if let defaultText {
rl_insert_text(defaultText)
}
rl_forced_update_display()
break
}
counter -= 1
}
rl_forced_update_display()
}
queue.addOperation(insertOperation)