diff options
| author | Zack Corr <zack@z0w0.me> | 2012-11-04 15:40:57 +1000 |
|---|---|---|
| committer | Zack Corr <zack@z0w0.me> | 2012-11-04 15:40:57 +1000 |
| commit | a450119b0bd454038c483785f652419ab3c3b1c8 (patch) | |
| tree | 64d46fdf82f87274a86a2bc1f08acc3bf4b11d4b /src/libstd | |
| parent | 4f56b4983bd5cb1804ef20e8ff01639b024cb58e (diff) | |
Move rusti::rl to std::rl
Diffstat (limited to 'src/libstd')
| -rw-r--r-- | src/libstd/rl.rs | 75 | ||||
| -rw-r--r-- | src/libstd/std.rc | 1 |
2 files changed, 76 insertions, 0 deletions
diff --git a/src/libstd/rl.rs b/src/libstd/rl.rs new file mode 100644 index 00000000000..cbfa9777b29 --- /dev/null +++ b/src/libstd/rl.rs @@ -0,0 +1,75 @@ +use libc::{c_char, c_int}; + +extern mod rustrt { + #[legacy_exports]; + fn linenoise(prompt: *c_char) -> *c_char; + fn linenoiseHistoryAdd(line: *c_char) -> c_int; + fn linenoiseHistorySetMaxLen(len: c_int) -> c_int; + fn linenoiseHistorySave(file: *c_char) -> c_int; + fn linenoiseHistoryLoad(file: *c_char) -> c_int; + fn linenoiseSetCompletionCallback(callback: *u8); + fn linenoiseAddCompletion(completions: *(), line: *c_char); + fn linenoiseClearScreen(); +} + +/// Add a line to history +pub fn add_history(line: ~str) -> bool { + do str::as_c_str(line) |buf| { + rustrt::linenoiseHistoryAdd(buf) == 1 as c_int + } +} + +/// Set the maximum amount of lines stored +pub fn set_history_max_len(len: int) -> bool { + rustrt::linenoiseHistorySetMaxLen(len as c_int) == 1 as c_int +} + +/// Save line history to a file +pub fn save_history(file: ~str) -> bool { + do str::as_c_str(file) |buf| { + rustrt::linenoiseHistorySave(buf) == 1 as c_int + } +} + +/// Load line history from a file +pub fn load_history(file: ~str) -> bool { + do str::as_c_str(file) |buf| { + rustrt::linenoiseHistoryLoad(buf) == 1 as c_int + } +} + +/// Print out a prompt and then wait for input and return it +pub fn read(prompt: ~str) -> Option<~str> { + do str::as_c_str(prompt) |buf| unsafe { + let line = rustrt::linenoise(buf); + + if line.is_null() { None } + else { Some(str::raw::from_c_str(line)) } + } +} + +/// Clear the screen +pub fn clear() { + rustrt::linenoiseClearScreen(); +} + +pub type CompletionCb = fn~(~str, fn(~str)); + +fn complete_key(_v: @CompletionCb) {} + +/// Bind to the main completion callback +pub fn complete(cb: CompletionCb) unsafe { + task::local_data::local_data_set(complete_key, @(move cb)); + + extern fn callback(line: *c_char, completions: *()) unsafe { + let cb = copy *task::local_data::local_data_get(complete_key).get(); + + do cb(str::raw::from_c_str(line)) |suggestion| { + do str::as_c_str(suggestion) |buf| { + rustrt::linenoiseAddCompletion(completions, buf); + } + } + } + + rustrt::linenoiseSetCompletionCallback(callback); +} \ No newline at end of file diff --git a/src/libstd/std.rc b/src/libstd/std.rc index 88ef03aa534..62601ebb67b 100644 --- a/src/libstd/std.rc +++ b/src/libstd/std.rc @@ -82,6 +82,7 @@ pub mod arena; pub mod par; pub mod cmp; pub mod base64; +pub mod rl; #[cfg(unicode)] mod unicode; |
