extra: improvements & bug fixes to rl.

- Removes a layer of indirection in the storage of the completion
  callback.
- Handles user tab completion in a task in which `complete` hasn't been
  properly. Previously, if `complete` was called in one task, and `read`
  called in another, attempting to get completions would crash. This
  makes the completion handlers non-ambiguously task-local only.
- Fix a mismatch in return values between the Rust code and linenoise.
This commit is contained in:
Huon Wilson 2013-09-11 21:31:14 +10:00
parent fe03d827a4
commit 4ed5fcb122

View File

@ -74,14 +74,16 @@ pub fn set_history_max_len(len: int) -> bool {
/// Save line history to a file
pub fn save_history(file: &str) -> bool {
do file.with_c_str |buf| {
(locked!(rustrt::linenoiseHistorySave(buf))) == 1 as c_int
// 0 on success, -1 on failure
(locked!(rustrt::linenoiseHistorySave(buf))) == 0 as c_int
}
}
/// Load line history from a file
pub fn load_history(file: &str) -> bool {
do file.with_c_str |buf| {
(locked!(rustrt::linenoiseHistoryLoad(buf))) == 1 as c_int
// 0 on success, -1 on failure
(locked!(rustrt::linenoiseHistoryLoad(buf))) == 0 as c_int
}
}
@ -107,34 +109,40 @@ pub fn read(prompt: &str) -> Option<~str> {
pub type CompletionCb = @fn(~str, @fn(~str));
static complete_key: local_data::Key<@CompletionCb> = &local_data::Key;
static complete_key: local_data::Key<CompletionCb> = &local_data::Key;
/// Bind to the main completion callback.
/// Bind to the main completion callback in the current task.
///
/// The completion callback should not call any `extra::rl` functions
/// other than the closure that it receives as its second
/// argument. Calling such a function will deadlock on the mutex used
/// to ensure that the calls are thread-safe.
pub fn complete(cb: CompletionCb) {
local_data::set(complete_key, @cb);
local_data::set(complete_key, cb);
extern fn callback(line: *c_char, completions: *()) {
do local_data::get(complete_key) |cb| {
let cb = **cb.unwrap();
unsafe {
do cb(str::raw::from_c_str(line)) |suggestion| {
extern fn callback(c_line: *c_char, completions: *()) {
do local_data::get(complete_key) |opt_cb| {
// only fetch completions if a completion handler has been
// registered in the current task.
match opt_cb {
None => {},
Some(cb) => {
let line = unsafe { str::raw::from_c_str(c_line) };
do (*cb)(line) |suggestion| {
do suggestion.with_c_str |buf| {
// This isn't locked, because `callback` gets
// called inside `rustrt::linenoise`, which
// *is* already inside the mutex, so
// re-locking would be a deadlock.
unsafe {
rustrt::linenoiseAddCompletion(completions, buf);
}
}
}
}
}
}
}
locked!(rustrt::linenoiseSetCompletionCallback(callback));
}