2022-01-27 19:44:35 +01:00
|
|
|
use crate::reedline_config::{add_completion_menu, add_history_menu};
|
2022-03-16 19:17:06 +01:00
|
|
|
use crate::{prompt_update, reedline_config};
|
2022-01-18 20:32:45 +01:00
|
|
|
use crate::{
|
|
|
|
reedline_config::KeybindingsMode,
|
2022-03-16 19:17:06 +01:00
|
|
|
util::{eval_source, report_error},
|
2022-01-18 20:32:45 +01:00
|
|
|
};
|
2022-03-16 19:17:06 +01:00
|
|
|
use crate::{NuCompleter, NuHighlighter, NuValidator, NushellPrompt};
|
2022-02-09 23:08:16 +01:00
|
|
|
use log::info;
|
2022-01-18 09:48:28 +01:00
|
|
|
use log::trace;
|
|
|
|
use miette::{IntoDiagnostic, Result};
|
|
|
|
use nu_color_config::get_color_config;
|
|
|
|
use nu_engine::convert_env_values;
|
|
|
|
use nu_parser::lex;
|
2022-03-16 19:17:06 +01:00
|
|
|
use nu_protocol::engine::Stack;
|
|
|
|
use nu_protocol::PipelineData;
|
2022-01-18 09:48:28 +01:00
|
|
|
use nu_protocol::{
|
|
|
|
engine::{EngineState, StateWorkingSet},
|
|
|
|
Config, ShellError, Span, Value, CONFIG_VARIABLE_ID,
|
|
|
|
};
|
|
|
|
use reedline::{DefaultHinter, Emacs, Vi};
|
2022-03-16 19:17:06 +01:00
|
|
|
use std::path::PathBuf;
|
2022-02-09 23:08:16 +01:00
|
|
|
use std::{sync::atomic::Ordering, time::Instant};
|
2022-01-18 09:48:28 +01:00
|
|
|
|
2022-03-16 19:17:06 +01:00
|
|
|
pub fn evaluate_repl(
|
2022-02-19 21:54:43 +01:00
|
|
|
engine_state: &mut EngineState,
|
2022-03-16 19:17:06 +01:00
|
|
|
stack: &mut Stack,
|
|
|
|
history_path: Option<PathBuf>,
|
|
|
|
is_perf_true: bool,
|
2022-02-19 21:54:43 +01:00
|
|
|
) -> Result<()> {
|
2022-02-09 23:08:16 +01:00
|
|
|
// use crate::logger::{configure, logger};
|
2022-01-18 09:48:28 +01:00
|
|
|
use reedline::{FileBackedHistory, Reedline, Signal};
|
|
|
|
|
|
|
|
let mut entry_num = 0;
|
|
|
|
|
|
|
|
let mut nu_prompt = NushellPrompt::new();
|
2022-03-16 19:17:06 +01:00
|
|
|
// let mut stack = nu_protocol::engine::Stack::new();
|
2022-01-18 09:48:28 +01:00
|
|
|
|
|
|
|
// First, set up env vars as strings only
|
2022-03-16 19:17:06 +01:00
|
|
|
// gather_parent_env_vars(engine_state);
|
2022-01-18 09:48:28 +01:00
|
|
|
|
|
|
|
// Set up our initial config to start from
|
2022-03-16 19:17:06 +01:00
|
|
|
// stack.vars.insert(
|
|
|
|
// CONFIG_VARIABLE_ID,
|
|
|
|
// Value::Record {
|
|
|
|
// cols: vec![],
|
|
|
|
// vals: vec![],
|
|
|
|
// span: Span::new(0, 0),
|
|
|
|
// },
|
|
|
|
// );
|
|
|
|
|
|
|
|
if is_perf_true {
|
2022-02-10 22:22:39 +01:00
|
|
|
info!("read_plugin_file {}:{}:{}", file!(), line!(), column!());
|
|
|
|
}
|
|
|
|
|
2022-03-16 19:17:06 +01:00
|
|
|
// #[cfg(feature = "plugin")]
|
|
|
|
// config_files::read_plugin_file(engine_state, &mut stack, is_perf_true);
|
|
|
|
//
|
|
|
|
// if is_perf_true {
|
|
|
|
// info!("read_config_file {}:{}:{}", file!(), line!(), column!());
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
// config_files::read_config_file(engine_state, &mut stack, config_file, is_perf_true);
|
|
|
|
// let history_path = config_files::create_history_path();
|
2022-01-18 09:48:28 +01:00
|
|
|
|
2022-02-09 23:08:16 +01:00
|
|
|
// logger(|builder| {
|
|
|
|
// configure(&config.log_level, builder)?;
|
|
|
|
// // trace_filters(self, builder)?;
|
|
|
|
// // debug_filters(self, builder)?;
|
2022-01-18 09:48:28 +01:00
|
|
|
|
2022-02-09 23:08:16 +01:00
|
|
|
// Ok(())
|
|
|
|
// })?;
|
2022-01-18 09:48:28 +01:00
|
|
|
|
2022-03-16 19:17:06 +01:00
|
|
|
if is_perf_true {
|
2022-02-10 22:22:39 +01:00
|
|
|
info!(
|
|
|
|
"translate environment vars {}:{}:{}",
|
|
|
|
file!(),
|
|
|
|
line!(),
|
|
|
|
column!()
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2022-01-18 09:48:28 +01:00
|
|
|
// Translate environment variables from Strings to Values
|
2022-03-16 19:17:06 +01:00
|
|
|
if let Some(e) = convert_env_values(engine_state, stack) {
|
2022-01-18 09:48:28 +01:00
|
|
|
let working_set = StateWorkingSet::new(engine_state);
|
|
|
|
report_error(&working_set, &e);
|
|
|
|
}
|
|
|
|
|
2022-02-24 20:02:28 +01:00
|
|
|
// Make a note of the exceptions we see for externals that look like math expressions
|
2022-03-16 19:17:06 +01:00
|
|
|
let exceptions = crate::util::external_exceptions(engine_state, stack);
|
2022-02-24 20:02:28 +01:00
|
|
|
engine_state.external_exceptions = exceptions;
|
|
|
|
|
2022-02-26 14:57:45 +01:00
|
|
|
// seed env vars
|
2022-01-21 20:50:44 +01:00
|
|
|
stack.add_env_var(
|
|
|
|
"CMD_DURATION_MS".into(),
|
|
|
|
Value::String {
|
|
|
|
val: "0823".to_string(),
|
|
|
|
span: Span { start: 0, end: 0 },
|
|
|
|
},
|
|
|
|
);
|
|
|
|
|
2022-02-26 14:57:45 +01:00
|
|
|
stack.add_env_var(
|
|
|
|
"LAST_EXIT_CODE".into(),
|
|
|
|
Value::Int {
|
|
|
|
val: 0,
|
|
|
|
span: Span { start: 0, end: 0 },
|
|
|
|
},
|
|
|
|
);
|
|
|
|
|
2022-01-18 09:48:28 +01:00
|
|
|
loop {
|
2022-03-16 19:17:06 +01:00
|
|
|
if is_perf_true {
|
2022-02-10 22:22:39 +01:00
|
|
|
info!(
|
|
|
|
"load config each loop {}:{}:{}",
|
|
|
|
file!(),
|
|
|
|
line!(),
|
|
|
|
column!()
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2022-01-18 09:48:28 +01:00
|
|
|
let config = match stack.get_config() {
|
|
|
|
Ok(config) => config,
|
|
|
|
Err(e) => {
|
|
|
|
let working_set = StateWorkingSet::new(engine_state);
|
|
|
|
|
|
|
|
report_error(&working_set, &e);
|
|
|
|
Config::default()
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
//Reset the ctrl-c handler
|
2022-01-26 18:26:43 +01:00
|
|
|
if let Some(ctrlc) = &mut engine_state.ctrlc {
|
|
|
|
ctrlc.store(false, Ordering::SeqCst);
|
|
|
|
}
|
2022-01-18 09:48:28 +01:00
|
|
|
|
2022-03-16 19:17:06 +01:00
|
|
|
if is_perf_true {
|
2022-02-10 22:22:39 +01:00
|
|
|
info!("setup line editor {}:{}:{}", file!(), line!(), column!());
|
|
|
|
}
|
|
|
|
|
2022-01-27 08:53:23 +01:00
|
|
|
let mut line_editor = Reedline::create()
|
2022-01-18 09:48:28 +01:00
|
|
|
.into_diagnostic()?
|
|
|
|
.with_highlighter(Box::new(NuHighlighter {
|
|
|
|
engine_state: engine_state.clone(),
|
|
|
|
config: config.clone(),
|
|
|
|
}))
|
|
|
|
.with_animation(config.animate_prompt)
|
|
|
|
.with_validator(Box::new(NuValidator {
|
|
|
|
engine_state: engine_state.clone(),
|
|
|
|
}))
|
2022-02-18 19:54:13 +01:00
|
|
|
.with_completer(Box::new(NuCompleter::new(
|
|
|
|
engine_state.clone(),
|
|
|
|
stack.vars.get(&CONFIG_VARIABLE_ID).cloned(),
|
|
|
|
)))
|
2022-02-04 16:30:21 +01:00
|
|
|
.with_quick_completions(config.quick_completions)
|
2022-03-03 10:13:44 +01:00
|
|
|
.with_partial_completions(config.partial_completions)
|
2022-01-27 08:53:23 +01:00
|
|
|
.with_ansi_colors(config.use_ansi_coloring);
|
|
|
|
|
2022-03-16 19:17:06 +01:00
|
|
|
if is_perf_true {
|
2022-02-10 22:22:39 +01:00
|
|
|
info!("setup reedline {}:{}:{}", file!(), line!(), column!());
|
|
|
|
}
|
|
|
|
|
2022-01-27 19:44:35 +01:00
|
|
|
line_editor = add_completion_menu(line_editor, &config);
|
2022-01-27 08:53:23 +01:00
|
|
|
line_editor = add_history_menu(line_editor, &config);
|
2022-01-18 09:48:28 +01:00
|
|
|
|
2022-03-16 19:17:06 +01:00
|
|
|
if is_perf_true {
|
2022-02-10 22:22:39 +01:00
|
|
|
info!("setup colors {}:{}:{}", file!(), line!(), column!());
|
|
|
|
}
|
2022-01-18 09:48:28 +01:00
|
|
|
//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);
|
|
|
|
|
2022-03-16 19:17:06 +01:00
|
|
|
if is_perf_true {
|
2022-02-10 22:22:39 +01:00
|
|
|
info!(
|
|
|
|
"setup history and hinter {}:{}:{}",
|
|
|
|
file!(),
|
|
|
|
line!(),
|
|
|
|
column!()
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2022-01-27 08:53:23 +01:00
|
|
|
line_editor = if let Some(history_path) = history_path.clone() {
|
2022-01-18 09:48:28 +01:00
|
|
|
let history = std::fs::read_to_string(&history_path);
|
|
|
|
if history.is_ok() {
|
|
|
|
line_editor
|
|
|
|
.with_hinter(Box::new(
|
2022-01-20 19:57:47 +01:00
|
|
|
DefaultHinter::default().with_style(color_hm["hints"]),
|
2022-01-18 09:48:28 +01:00
|
|
|
))
|
|
|
|
.with_history(Box::new(
|
|
|
|
FileBackedHistory::with_file(
|
|
|
|
config.max_history_size as usize,
|
|
|
|
history_path.clone(),
|
|
|
|
)
|
|
|
|
.into_diagnostic()?,
|
|
|
|
))
|
|
|
|
.into_diagnostic()?
|
|
|
|
} else {
|
|
|
|
line_editor
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
line_editor
|
|
|
|
};
|
|
|
|
|
2022-03-16 19:17:06 +01:00
|
|
|
if is_perf_true {
|
2022-02-10 22:22:39 +01:00
|
|
|
info!("setup keybindings {}:{}:{}", file!(), line!(), column!());
|
|
|
|
}
|
|
|
|
|
2022-01-18 20:32:45 +01:00
|
|
|
// Changing the line editor based on the found keybindings
|
2022-01-27 08:53:23 +01:00
|
|
|
line_editor = match reedline_config::create_keybindings(&config) {
|
2022-01-19 14:28:08 +01:00
|
|
|
Ok(keybindings) => match keybindings {
|
|
|
|
KeybindingsMode::Emacs(keybindings) => {
|
|
|
|
let edit_mode = Box::new(Emacs::new(keybindings));
|
|
|
|
line_editor.with_edit_mode(edit_mode)
|
|
|
|
}
|
|
|
|
KeybindingsMode::Vi {
|
|
|
|
insert_keybindings,
|
|
|
|
normal_keybindings,
|
|
|
|
} => {
|
|
|
|
let edit_mode = Box::new(Vi::new(insert_keybindings, normal_keybindings));
|
|
|
|
line_editor.with_edit_mode(edit_mode)
|
|
|
|
}
|
|
|
|
},
|
|
|
|
Err(e) => {
|
|
|
|
let working_set = StateWorkingSet::new(engine_state);
|
|
|
|
report_error(&working_set, &e);
|
|
|
|
line_editor
|
2022-01-18 20:32:45 +01:00
|
|
|
}
|
2022-01-18 09:48:28 +01:00
|
|
|
};
|
|
|
|
|
2022-03-16 19:17:06 +01:00
|
|
|
if is_perf_true {
|
2022-02-10 22:22:39 +01:00
|
|
|
info!("prompt_update {}:{}:{}", file!(), line!(), column!());
|
2022-02-09 23:08:16 +01:00
|
|
|
}
|
|
|
|
|
2022-03-16 19:17:06 +01:00
|
|
|
let prompt = prompt_update::update_prompt(
|
|
|
|
&config,
|
|
|
|
engine_state,
|
|
|
|
stack,
|
|
|
|
&mut nu_prompt,
|
|
|
|
is_perf_true,
|
|
|
|
);
|
2022-01-18 09:48:28 +01:00
|
|
|
|
2022-02-10 22:22:39 +01:00
|
|
|
entry_num += 1;
|
|
|
|
|
2022-03-16 19:17:06 +01:00
|
|
|
if is_perf_true {
|
2022-02-09 23:08:16 +01:00
|
|
|
info!(
|
2022-02-10 22:22:39 +01:00
|
|
|
"finished setup, starting repl {}:{}:{}",
|
2022-02-09 23:08:16 +01:00
|
|
|
file!(),
|
|
|
|
line!(),
|
|
|
|
column!()
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2022-01-18 09:48:28 +01:00
|
|
|
let input = line_editor.read_line(prompt);
|
|
|
|
match input {
|
|
|
|
Ok(Signal::Success(s)) => {
|
2022-01-21 15:53:49 +01:00
|
|
|
let start_time = Instant::now();
|
2022-01-18 09:48:28 +01:00
|
|
|
let tokens = lex(s.as_bytes(), 0, &[], &[], false);
|
|
|
|
// Check if this is a single call to a directory, if so auto-cd
|
2022-03-16 19:17:06 +01:00
|
|
|
let cwd = nu_engine::env::current_dir_str(engine_state, stack)?;
|
2022-01-18 09:48:28 +01:00
|
|
|
let path = nu_path::expand_path_with(&s, &cwd);
|
|
|
|
|
|
|
|
let orig = s.clone();
|
|
|
|
|
|
|
|
if (orig.starts_with('.')
|
|
|
|
|| orig.starts_with('~')
|
|
|
|
|| orig.starts_with('/')
|
|
|
|
|| orig.starts_with('\\'))
|
|
|
|
&& path.is_dir()
|
|
|
|
&& tokens.0.len() == 1
|
|
|
|
{
|
|
|
|
// We have an auto-cd
|
|
|
|
let (path, span) = {
|
|
|
|
if !path.exists() {
|
|
|
|
let working_set = StateWorkingSet::new(engine_state);
|
|
|
|
|
|
|
|
report_error(
|
|
|
|
&working_set,
|
|
|
|
&ShellError::DirectoryNotFound(tokens.0[0].span),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
let path = nu_path::canonicalize_with(path, &cwd)
|
|
|
|
.expect("internal error: cannot canonicalize known path");
|
|
|
|
(path.to_string_lossy().to_string(), tokens.0[0].span)
|
|
|
|
};
|
|
|
|
|
|
|
|
//FIXME: this only changes the current scope, but instead this environment variable
|
|
|
|
//should probably be a block that loads the information from the state in the overlay
|
|
|
|
stack.add_env_var(
|
|
|
|
"PWD".into(),
|
|
|
|
Value::String {
|
|
|
|
val: path.clone(),
|
|
|
|
span: Span { start: 0, end: 0 },
|
|
|
|
},
|
|
|
|
);
|
|
|
|
let cwd = Value::String { val: cwd, span };
|
|
|
|
|
|
|
|
let shells = stack.get_env_var(engine_state, "NUSHELL_SHELLS");
|
|
|
|
let mut shells = if let Some(v) = shells {
|
|
|
|
v.as_list()
|
|
|
|
.map(|x| x.to_vec())
|
|
|
|
.unwrap_or_else(|_| vec![cwd])
|
|
|
|
} else {
|
|
|
|
vec![cwd]
|
|
|
|
};
|
|
|
|
|
|
|
|
let current_shell = stack.get_env_var(engine_state, "NUSHELL_CURRENT_SHELL");
|
|
|
|
let current_shell = if let Some(v) = current_shell {
|
|
|
|
v.as_integer().unwrap_or_default() as usize
|
|
|
|
} else {
|
|
|
|
0
|
|
|
|
};
|
|
|
|
|
|
|
|
shells[current_shell] = Value::String { val: path, span };
|
|
|
|
|
|
|
|
stack.add_env_var("NUSHELL_SHELLS".into(), Value::List { vals: shells, span });
|
|
|
|
} else {
|
|
|
|
trace!("eval source: {}", s);
|
|
|
|
|
|
|
|
eval_source(
|
|
|
|
engine_state,
|
2022-03-16 19:17:06 +01:00
|
|
|
stack,
|
2022-02-14 16:53:48 +01:00
|
|
|
s.as_bytes(),
|
2022-01-18 09:48:28 +01:00
|
|
|
&format!("entry #{}", entry_num),
|
2022-02-14 16:53:48 +01:00
|
|
|
PipelineData::new(Span::new(0, 0)),
|
2022-01-18 09:48:28 +01:00
|
|
|
);
|
2022-01-21 15:53:49 +01:00
|
|
|
|
|
|
|
stack.add_env_var(
|
|
|
|
"CMD_DURATION_MS".into(),
|
|
|
|
Value::String {
|
|
|
|
val: format!("{}", start_time.elapsed().as_millis()),
|
|
|
|
span: Span { start: 0, end: 0 },
|
|
|
|
},
|
|
|
|
);
|
2022-01-18 09:48:28 +01:00
|
|
|
}
|
|
|
|
// FIXME: permanent state changes like this hopefully in time can be removed
|
|
|
|
// and be replaced by just passing the cwd in where needed
|
|
|
|
if let Some(cwd) = stack.get_env_var(engine_state, "PWD") {
|
|
|
|
let path = cwd.as_string()?;
|
|
|
|
let _ = std::env::set_current_dir(path);
|
|
|
|
engine_state.env_vars.insert("PWD".into(), cwd);
|
|
|
|
}
|
2022-02-24 16:31:24 +01:00
|
|
|
|
|
|
|
// Make a note of the exceptions we see for externals that look like math expressions
|
2022-03-16 19:17:06 +01:00
|
|
|
let exceptions = crate::util::external_exceptions(engine_state, stack);
|
2022-02-24 16:31:24 +01:00
|
|
|
engine_state.external_exceptions = exceptions;
|
2022-01-18 09:48:28 +01:00
|
|
|
}
|
|
|
|
Ok(Signal::CtrlC) => {
|
|
|
|
// `Reedline` clears the line content. New prompt is shown
|
|
|
|
}
|
|
|
|
Ok(Signal::CtrlD) => {
|
|
|
|
// When exiting clear to a new line
|
|
|
|
println!();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
Ok(Signal::CtrlL) => {
|
|
|
|
line_editor.clear_screen().into_diagnostic()?;
|
|
|
|
}
|
|
|
|
Err(err) => {
|
|
|
|
let message = err.to_string();
|
|
|
|
if !message.contains("duration") {
|
|
|
|
println!("Error: {:?}", err);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|