1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
use std::fmt::Display;
#[cfg(feature = "gnu-readline")]
use rl_sys::readline;
#[cfg(feature = "gnu-readline")]
use rl_sys::history::{listmgmt, mgmt};
#[cfg(feature = "gnu-readline")]
use rl_sys::ReadlineError;
pub trait ReadLine<Error: Display> {
fn read(&self, prompt: &str) -> Result<Option<String>, Error>;
fn add(&self, line: &str);
}
#[cfg(feature = "gnu-readline")]
pub struct GnuReadLine;
#[cfg(feature = "gnu-readline")]
impl ReadLine<ReadlineError> for GnuReadLine {
fn read(&self, prompt: &str) -> Result<Option<String>, ReadlineError> {
readline::readline(prompt)
}
fn add(&self, line: &str) {
listmgmt::add(line).unwrap();
}
}
#[cfg(feature = "gnu-readline")]
impl Drop for GnuReadLine {
fn drop(&mut self) {
mgmt::cleanup();
}
}