Lift line editor construction out of loop (#5041)

Enables the use of some features on reedline

- Keeping the line when clearing the screen with `Ctrl-L`
- Using the internal cut buffer between lines
- Submitting external commands via keybinding and keeping the line

Additional effect:

Keep the history around and do basic syncs (performance improvement
minimal as session changes have to be read and written)

Additional change:

Give the option to defer writing/rereading the history file to the
closing of the session ($config.sync_history_on_enter)
This commit is contained in:
Stefan Holderbach
2022-03-31 23:25:48 +02:00
committed by GitHub
parent 6a6471b04b
commit 0986c61a5d
4 changed files with 63 additions and 41 deletions

View File

@ -104,6 +104,45 @@ pub fn evaluate_repl(
},
);
if is_perf_true {
info!(
"load config initially {}:{}:{}",
file!(),
line!(),
column!()
);
}
// Get the config once for the history `max_history_size`
// Updating that will not be possible in one session
let mut config = match stack.get_config() {
Ok(config) => config,
Err(e) => {
let working_set = StateWorkingSet::new(engine_state);
report_error(&working_set, &e);
Config::default()
}
};
if is_perf_true {
info!("setup reedline {}:{}:{}", file!(), line!(), column!());
}
let mut line_editor = Reedline::create().into_diagnostic()?;
if let Some(history_path) = history_path.as_deref() {
if is_perf_true {
info!("setup history {}:{}:{}", file!(), line!(), column!());
}
let history = Box::new(
FileBackedHistory::with_file(
config.max_history_size as usize,
history_path.to_path_buf(),
)
.into_diagnostic()?,
);
line_editor = line_editor.with_history(history).into_diagnostic()?;
};
loop {
if is_perf_true {
info!(
@ -114,7 +153,7 @@ pub fn evaluate_repl(
);
}
let config = match stack.get_config() {
config = match stack.get_config() {
Ok(config) => config,
Err(e) => {
let working_set = StateWorkingSet::new(engine_state);
@ -123,18 +162,14 @@ pub fn evaluate_repl(
Config::default()
}
};
let color_hm = get_color_config(&config);
//Reset the ctrl-c handler
if let Some(ctrlc) = &mut engine_state.ctrlc {
ctrlc.store(false, Ordering::SeqCst);
}
if is_perf_true {
info!("setup line editor {}:{}:{}", file!(), line!(), column!());
}
let mut line_editor = Reedline::create()
.into_diagnostic()?
line_editor = line_editor
.with_highlighter(Box::new(NuHighlighter {
engine_state: engine_state.clone(),
config: config.clone(),
@ -143,6 +178,9 @@ pub fn evaluate_repl(
.with_validator(Box::new(NuValidator {
engine_state: engine_state.clone(),
}))
.with_hinter(Box::new(
DefaultHinter::default().with_style(color_hm["hints"]),
))
.with_completer(Box::new(NuCompleter::new(
engine_state.clone(),
stack.clone(),
@ -153,7 +191,7 @@ pub fn evaluate_repl(
.with_ansi_colors(config.use_ansi_coloring);
if is_perf_true {
info!("setup reedline {}:{}:{}", file!(), line!(), column!());
info!("update reedline {}:{}:{}", file!(), line!(), column!());
}
line_editor = add_completion_menu(line_editor, &config);
@ -168,38 +206,12 @@ pub fn evaluate_repl(
//FIXME: if config.use_ansi_coloring is false then we should
// turn off the hinter but I don't see any way to do that yet.
let color_hm = get_color_config(&config);
if is_perf_true {
info!(
"setup history and hinter {}:{}:{}",
file!(),
line!(),
column!()
);
}
line_editor = if let Some(history_path) = history_path.clone() {
let history = std::fs::read_to_string(&history_path);
if history.is_ok() {
line_editor
.with_hinter(Box::new(
DefaultHinter::default().with_style(color_hm["hints"]),
))
.with_history(Box::new(
FileBackedHistory::with_file(
config.max_history_size as usize,
history_path.clone(),
)
.into_diagnostic()?,
))
.into_diagnostic()?
} else {
line_editor
if config.sync_history_on_enter {
if is_perf_true {
info!("sync history {}:{}:{}", file!(), line!(), column!());
}
} else {
line_editor
};
line_editor.sync_history().into_diagnostic()?;
}
if is_perf_true {
info!("setup keybindings {}:{}:{}", file!(), line!(), column!());