Merge pull request #775 from matthiasbeyer/libimaginteraction/readline

libimaginteraction: readline support
This commit is contained in:
Matthias Beyer 2016-10-11 18:22:41 +02:00 committed by GitHub
commit ac1af164e9
5 changed files with 166 additions and 1 deletions

View file

@ -1,6 +1,32 @@
# This is a example configuration file for the imag suite.
# It is written in TOML
#
# Configuration options for the user interface
#
[ui]
#
# Configuration options for the commandline user interface
#
[ui.cli]
# History file path for readline. Will be created by imag if it does not exist.
readline_history_file = "/tmp/readline.history"
# Number of lines to safe in the history file
readline_history_size = 100
# Ignore duplicated lines
readline_history_ignore_dups = true
# Tell if lines which begin with a space character are saved or not in the
# history list.
readline_history_ignore_space = true
# The prompt string to use
readline_prompt = ">> "
[store]
# Set to false if you do not want imag to create the directory where the store

View file

@ -10,7 +10,9 @@ interactor = "0.1"
lazy_static = "0.1.15"
log = "0.3"
regex = "0.1"
toml = "0.2.1"
spinner = "0.4"
rustyline = "1.0"
[dependencies.libimagstore]
path = "../libimagstore"

View file

@ -23,7 +23,13 @@ generate_error_module!(
CLIError => "Error on commandline",
IdMissingError => "Commandline: ID missing",
StoreIdParsingError => "Error while parsing StoreId",
IdSelectingError => "Error while selecting id"
IdSelectingError => "Error while selecting id",
ConfigError => "Configuration error",
ConfigMissingError => "Configuration missing",
ConfigTypeError => "Config Type Error",
NoConfigError => "No configuration",
ReadlineHistoryFileCreationError => "Could not create history file for readline",
ReadlineError => "Readline error"
);
);

View file

@ -38,6 +38,8 @@ extern crate ansi_term;
#[macro_use] extern crate lazy_static;
extern crate regex;
extern crate clap;
extern crate toml;
extern crate rustyline;
extern crate libimagentryfilter;
extern crate libimagstore;

View file

@ -0,0 +1,129 @@
//
// imag - the personal information management suite for the commandline
// Copyright (C) 2015, 2016 Matthias Beyer <mail@beyermatthias.de> and contributors
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; version
// 2.1 of the License.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
//
use error::InteractionErrorKind as IEK;
use error::MapErrInto;
use toml::Value;
use rustyline::{Config, Editor};
pub struct Readline {
editor: Editor,
history_file: PathBuf,
prompt: String,
}
impl Readline {
pub fn new(rt: &Runtime) -> Result<Readline> {
let cfg = try!(rt.config().ok_or(IEK::NoConfigError));
let c = cfg.config();
let histfile = try!(c.lookup("ui.cli.readline_history_file").ok_or(IEK::ConfigError));
let histsize = try!(c.lookup("ui.cli.readline_history_size").ok_or(IEK::ConfigError));
let histigndups = try!(c.lookup("ui.cli.readline_history_ignore_dups").ok_or(IEK::ConfigError));
let histignspace = try!(c.lookup("ui.cli.readline_history_ignore_space").ok_or(IEK::ConfigError));
let prompt = try!(c.lookup("ui.cli.readline_prompt").ok_or(IEK::ConfigError));
let histfile = try!(match histfile {
Value::String(s) => PathBuf::from(s),
_ => Err(IEK::ConfigTypeError.into_error())
.map_err_into(IEK::ConfigError)
.map_err_into(IEK::ReadlineError)
});
let histsize = try!(match histsize {
Value::Integer(i) => i,
_ => Err(IEK::ConfigTypeError.into_error())
.map_err_into(IEK::ConfigError)
.map_err_into(IEK::ReadlineError)
});
let histigndups = try!(match histigndups {
Value::Boolean(b) => b,
_ => Err(IEK::ConfigTypeError.into_error())
.map_err_into(IEK::ConfigError)
.map_err_into(IEK::ReadlineError)
});
let histignspace = try!(match histignspace {
Value::Boolean(b) => b,
_ => Err(IEK::ConfigTypeError.into_error())
.map_err_into(IEK::ConfigError)
.map_err_into(IEK::ReadlineError)
});
let prompt = try!(match prompt {
Value::String(s) => s,
_ => Err(IEK::ConfigTypeError.into_error())
.map_err_into(IEK::ConfigError)
.map_err_into(IEK::ReadlineError)
});
let config = Config::builder().
.max_history_size(histsize)
.history_ignore_dups(histigndups)
.history_ignore_space(histignspace)
.build();
let mut editor = Editor::new(config);
if !histfile.exists() {
let _ = try!(File::create(histfile.clone())
.map_err_into(IEK::ReadlineHistoryFileCreationError));
}
let _ = try!(editor.load_history(&histfile).map_err_into(ReadlineError));
Ok(Readline {
editor: editor,
history_file: histfile,
prompt: prompt,
})
}
pub fn read_line(&mut self) -> Result<Option<String>> {
use rustyline::ReadlineError;
use libimagutil::warn_result::*;
match self.editor.readline(&self.prompt) {
Ok(line) => {
self.editor.add_history_line(&line);
self.editor
.save_history(&self.history_file)
.map_warn_err_str(|e| format!("Could not save history file {} -> {:?}",
self.history_file.display(), e));
return line;
},
Err(ReadlineError::Interrupted) => {
info!("CTRL-C");
Ok(None)
},
Err(ReadlineError::Eof) => {
info!("CTRL-D");
Ok(None)
},
Err(err) => Err(err).map_err_into(ReadlineError),
}
}
}