2012-12-10 17:44:02 -06:00
|
|
|
// Copyright 2012 The Rust Project Developers. See the COPYRIGHT
|
|
|
|
// file at the top-level directory of this distribution and at
|
|
|
|
// http://rust-lang.org/COPYRIGHT.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
|
|
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
|
|
|
// option. This file may not be copied, modified, or distributed
|
|
|
|
// except according to those terms.
|
|
|
|
|
2012-11-05 13:20:44 -06:00
|
|
|
// FIXME #3921. This is unsafe because linenoise uses global mutable
|
|
|
|
// state without mutexes.
|
|
|
|
|
2013-05-17 17:28:44 -05:00
|
|
|
|
2013-06-28 17:32:26 -05:00
|
|
|
use std::libc::{c_char, c_int};
|
|
|
|
use std::local_data;
|
|
|
|
use std::str;
|
2012-10-02 20:15:02 -05:00
|
|
|
|
2013-03-05 14:21:02 -06:00
|
|
|
pub mod rustrt {
|
2013-06-28 17:32:26 -05:00
|
|
|
use std::libc::{c_char, c_int};
|
2013-03-05 14:21:02 -06:00
|
|
|
|
|
|
|
pub extern {
|
|
|
|
pub unsafe fn linenoise(prompt: *c_char) -> *c_char;
|
|
|
|
pub unsafe fn linenoiseHistoryAdd(line: *c_char) -> c_int;
|
|
|
|
pub unsafe fn linenoiseHistorySetMaxLen(len: c_int) -> c_int;
|
|
|
|
pub unsafe fn linenoiseHistorySave(file: *c_char) -> c_int;
|
|
|
|
pub unsafe fn linenoiseHistoryLoad(file: *c_char) -> c_int;
|
|
|
|
pub unsafe fn linenoiseSetCompletionCallback(callback: *u8);
|
|
|
|
pub unsafe fn linenoiseAddCompletion(completions: *(), line: *c_char);
|
|
|
|
}
|
2012-10-02 20:15:02 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Add a line to history
|
2013-05-12 01:41:15 -05:00
|
|
|
pub unsafe fn add_history(line: &str) -> bool {
|
2012-10-27 06:41:41 -05:00
|
|
|
do str::as_c_str(line) |buf| {
|
2012-10-29 20:08:36 -05:00
|
|
|
rustrt::linenoiseHistoryAdd(buf) == 1 as c_int
|
2012-10-27 06:41:41 -05:00
|
|
|
}
|
2012-10-02 20:15:02 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Set the maximum amount of lines stored
|
2012-11-05 13:20:44 -06:00
|
|
|
pub unsafe fn set_history_max_len(len: int) -> bool {
|
2012-10-29 20:08:36 -05:00
|
|
|
rustrt::linenoiseHistorySetMaxLen(len as c_int) == 1 as c_int
|
2012-10-02 20:15:02 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Save line history to a file
|
2013-05-12 01:41:15 -05:00
|
|
|
pub unsafe fn save_history(file: &str) -> bool {
|
2012-10-27 06:41:41 -05:00
|
|
|
do str::as_c_str(file) |buf| {
|
2012-10-29 20:08:36 -05:00
|
|
|
rustrt::linenoiseHistorySave(buf) == 1 as c_int
|
2012-10-27 06:41:41 -05:00
|
|
|
}
|
2012-10-02 20:15:02 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Load line history from a file
|
2013-05-12 01:41:15 -05:00
|
|
|
pub unsafe fn load_history(file: &str) -> bool {
|
2012-10-27 06:41:41 -05:00
|
|
|
do str::as_c_str(file) |buf| {
|
2012-10-29 20:08:36 -05:00
|
|
|
rustrt::linenoiseHistoryLoad(buf) == 1 as c_int
|
2012-10-27 06:41:41 -05:00
|
|
|
}
|
2012-10-02 20:15:02 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Print out a prompt and then wait for input and return it
|
2013-05-12 01:41:15 -05:00
|
|
|
pub unsafe fn read(prompt: &str) -> Option<~str> {
|
2013-01-23 13:43:58 -06:00
|
|
|
do str::as_c_str(prompt) |buf| {
|
2013-04-09 00:31:42 -05:00
|
|
|
let line = rustrt::linenoise(buf);
|
2012-10-02 20:15:02 -05:00
|
|
|
|
2013-04-09 00:31:42 -05:00
|
|
|
if line.is_null() { None }
|
|
|
|
else { Some(str::raw::from_c_str(line)) }
|
2012-10-27 06:41:41 -05:00
|
|
|
}
|
2012-10-02 20:15:02 -05:00
|
|
|
}
|
|
|
|
|
2013-03-07 17:44:21 -06:00
|
|
|
pub type CompletionCb<'self> = @fn(~str, &'self fn(~str));
|
2012-10-02 20:15:02 -05:00
|
|
|
|
|
|
|
fn complete_key(_v: @CompletionCb) {}
|
|
|
|
|
|
|
|
/// Bind to the main completion callback
|
2013-01-23 13:43:58 -06:00
|
|
|
pub unsafe fn complete(cb: CompletionCb) {
|
2013-07-09 19:25:28 -05:00
|
|
|
local_data::set(complete_key, @(cb));
|
2013-04-09 00:31:42 -05:00
|
|
|
|
|
|
|
extern fn callback(line: *c_char, completions: *()) {
|
|
|
|
unsafe {
|
2013-07-11 02:35:43 -05:00
|
|
|
let cb = *local_data::get(complete_key, |k| k.map(|&k| *k))
|
2013-04-09 00:31:42 -05:00
|
|
|
.get();
|
|
|
|
|
|
|
|
do cb(str::raw::from_c_str(line)) |suggestion| {
|
|
|
|
do str::as_c_str(suggestion) |buf| {
|
|
|
|
rustrt::linenoiseAddCompletion(completions, buf);
|
2013-01-23 13:43:58 -06:00
|
|
|
}
|
2012-10-27 06:41:41 -05:00
|
|
|
}
|
|
|
|
}
|
2013-01-23 13:43:58 -06:00
|
|
|
}
|
2013-04-09 00:31:42 -05:00
|
|
|
|
|
|
|
rustrt::linenoiseSetCompletionCallback(callback);
|
2012-12-23 16:41:37 -06:00
|
|
|
}
|