2022-01-09 23:17:58 +01:00
|
|
|
use crossterm::event::{KeyCode, KeyModifiers};
|
2021-12-03 20:49:25 +01:00
|
|
|
#[cfg(windows)]
|
|
|
|
use crossterm_winapi::{ConsoleMode, Handle};
|
2021-10-15 22:42:36 +02:00
|
|
|
use dialoguer::{
|
|
|
|
console::{Style, Term},
|
|
|
|
theme::ColorfulTheme,
|
|
|
|
Select,
|
|
|
|
};
|
2022-01-01 22:42:50 +01:00
|
|
|
use log::trace;
|
2021-09-20 23:37:26 +02:00
|
|
|
use miette::{IntoDiagnostic, Result};
|
2021-12-04 06:02:57 +01:00
|
|
|
use nu_cli::{CliError, NuCompleter, NuHighlighter, NuValidator, NushellPrompt};
|
2021-12-23 22:02:57 +01:00
|
|
|
use nu_color_config::get_color_config;
|
2021-09-03 00:58:15 +02:00
|
|
|
use nu_command::create_default_context;
|
2021-12-21 23:32:38 +01:00
|
|
|
use nu_engine::{convert_env_values, eval_block};
|
2021-12-21 22:31:30 +01:00
|
|
|
use nu_parser::{lex, parse, trim_quotes, Token, TokenContents};
|
2021-09-03 04:15:01 +02:00
|
|
|
use nu_protocol::{
|
2021-09-25 16:47:23 +02:00
|
|
|
ast::Call,
|
2021-10-25 18:58:58 +02:00
|
|
|
engine::{EngineState, Stack, StateWorkingSet},
|
2021-12-13 04:16:51 +01:00
|
|
|
Config, PipelineData, ShellError, Span, Value, CONFIG_VARIABLE_ID,
|
2021-09-03 04:15:01 +02:00
|
|
|
};
|
2022-01-09 23:17:58 +01:00
|
|
|
use reedline::{
|
|
|
|
default_emacs_keybindings, Completer, CompletionActionHandler, DefaultHinter, EditCommand,
|
|
|
|
Emacs, LineBuffer, Prompt, ReedlineEvent, Vi,
|
|
|
|
};
|
2021-12-03 20:49:25 +01:00
|
|
|
use std::{
|
|
|
|
io::Write,
|
2022-01-04 23:30:34 +01:00
|
|
|
path::PathBuf,
|
2021-12-03 20:49:25 +01:00
|
|
|
sync::{
|
|
|
|
atomic::{AtomicBool, Ordering},
|
|
|
|
Arc,
|
|
|
|
},
|
|
|
|
};
|
2021-06-30 03:42:56 +02:00
|
|
|
|
2021-08-10 20:57:08 +02:00
|
|
|
#[cfg(test)]
|
|
|
|
mod tests;
|
|
|
|
|
2022-01-01 22:42:50 +01:00
|
|
|
mod logger;
|
|
|
|
|
2021-10-02 15:10:28 +02:00
|
|
|
// Name of environment variable where the prompt could be stored
|
|
|
|
const PROMPT_COMMAND: &str = "PROMPT_COMMAND";
|
2022-01-06 13:57:55 +01:00
|
|
|
const PROMPT_COMMAND_RIGHT: &str = "PROMPT_COMMAND_RIGHT";
|
2022-01-01 23:53:16 +01:00
|
|
|
const PROMPT_INDICATOR: &str = "PROMPT_INDICATOR";
|
|
|
|
const PROMPT_INDICATOR_VI_INSERT: &str = "PROMPT_INDICATOR_VI_INSERT";
|
|
|
|
const PROMPT_INDICATOR_VI_VISUAL: &str = "PROMPT_INDICATOR_VI_VISUAL";
|
|
|
|
const PROMPT_MULTILINE_INDICATOR: &str = "PROMPT_MULTILINE_INDICATOR";
|
2021-10-02 15:10:28 +02:00
|
|
|
|
2021-10-15 20:37:58 +02:00
|
|
|
struct FuzzyCompletion {
|
|
|
|
completer: Box<dyn Completer>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl CompletionActionHandler for FuzzyCompletion {
|
|
|
|
fn handle(&mut self, present_buffer: &mut LineBuffer) {
|
|
|
|
let completions = self
|
|
|
|
.completer
|
|
|
|
.complete(present_buffer.get_buffer(), present_buffer.offset());
|
|
|
|
|
|
|
|
if completions.is_empty() {
|
|
|
|
// do nothing
|
|
|
|
} else if completions.len() == 1 {
|
|
|
|
let span = completions[0].0;
|
|
|
|
|
|
|
|
let mut offset = present_buffer.offset();
|
|
|
|
offset += completions[0].1.len() - (span.end - span.start);
|
|
|
|
|
|
|
|
// TODO improve the support for multiline replace
|
|
|
|
present_buffer.replace(span.start..span.end, &completions[0].1);
|
|
|
|
present_buffer.set_insertion_point(offset);
|
|
|
|
} else {
|
|
|
|
let selections: Vec<_> = completions.iter().map(|(_, string)| string).collect();
|
|
|
|
|
|
|
|
let _ = crossterm::terminal::disable_raw_mode();
|
|
|
|
println!();
|
2021-10-15 22:52:03 +02:00
|
|
|
let theme = ColorfulTheme {
|
|
|
|
active_item_style: Style::new().for_stderr().on_green().black(),
|
|
|
|
..Default::default()
|
|
|
|
};
|
2021-10-15 22:42:36 +02:00
|
|
|
let result = Select::with_theme(&theme)
|
2021-10-15 20:37:58 +02:00
|
|
|
.default(0)
|
|
|
|
.items(&selections[..])
|
2021-10-15 22:33:56 +02:00
|
|
|
.interact_on_opt(&Term::stdout())
|
2021-12-16 22:11:06 +01:00
|
|
|
.unwrap_or(None);
|
2021-10-15 20:37:58 +02:00
|
|
|
let _ = crossterm::terminal::enable_raw_mode();
|
|
|
|
|
2021-10-15 22:42:36 +02:00
|
|
|
if let Some(result) = result {
|
|
|
|
let span = completions[result].0;
|
2021-10-15 20:37:58 +02:00
|
|
|
|
2021-10-15 22:42:36 +02:00
|
|
|
let mut offset = present_buffer.offset();
|
|
|
|
offset += completions[result].1.len() - (span.end - span.start);
|
2021-10-15 20:37:58 +02:00
|
|
|
|
2021-10-15 22:42:36 +02:00
|
|
|
// TODO improve the support for multiline replace
|
|
|
|
present_buffer.replace(span.start..span.end, &completions[result].1);
|
|
|
|
present_buffer.set_insertion_point(offset);
|
2021-10-15 22:33:56 +02:00
|
|
|
}
|
2021-10-15 20:37:58 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-20 23:37:26 +02:00
|
|
|
fn main() -> Result<()> {
|
2021-11-14 20:25:57 +01:00
|
|
|
// miette::set_panic_hook();
|
2021-10-01 18:39:50 +02:00
|
|
|
let miette_hook = std::panic::take_hook();
|
|
|
|
std::panic::set_hook(Box::new(move |x| {
|
2021-12-04 13:38:21 +01:00
|
|
|
crossterm::terminal::disable_raw_mode().expect("unable to disable raw mode");
|
2021-10-01 18:39:50 +02:00
|
|
|
miette_hook(x);
|
|
|
|
}));
|
2021-09-21 21:37:16 +02:00
|
|
|
|
2022-01-04 23:30:34 +01:00
|
|
|
// Get initial current working directory.
|
|
|
|
let init_cwd = get_init_cwd();
|
|
|
|
let mut engine_state = create_default_context(&init_cwd);
|
2021-07-17 08:31:34 +02:00
|
|
|
|
2021-10-28 06:13:10 +02:00
|
|
|
// TODO: make this conditional in the future
|
|
|
|
// Ctrl-c protection section
|
|
|
|
let ctrlc = Arc::new(AtomicBool::new(false));
|
|
|
|
let handler_ctrlc = ctrlc.clone();
|
|
|
|
let engine_state_ctrlc = ctrlc.clone();
|
|
|
|
|
|
|
|
ctrlc::set_handler(move || {
|
|
|
|
handler_ctrlc.store(true, Ordering::SeqCst);
|
|
|
|
})
|
|
|
|
.expect("Error setting Ctrl-C handler");
|
|
|
|
|
|
|
|
engine_state.ctrlc = Some(engine_state_ctrlc);
|
|
|
|
// End ctrl-c protection section
|
|
|
|
|
2021-07-22 08:04:50 +02:00
|
|
|
if let Some(path) = std::env::args().nth(1) {
|
2022-01-05 01:26:01 +01:00
|
|
|
// First, set up env vars as strings only
|
|
|
|
gather_parent_env_vars(&mut engine_state);
|
|
|
|
|
2021-09-20 23:37:26 +02:00
|
|
|
let file = std::fs::read(&path).into_diagnostic()?;
|
2021-07-23 07:14:49 +02:00
|
|
|
|
2021-07-30 23:26:05 +02:00
|
|
|
let (block, delta) = {
|
2021-10-25 08:31:39 +02:00
|
|
|
let mut working_set = StateWorkingSet::new(&engine_state);
|
2022-01-01 22:42:50 +01:00
|
|
|
trace!("parsing file: {}", path);
|
|
|
|
|
2021-09-06 22:41:30 +02:00
|
|
|
let (output, err) = parse(&mut working_set, Some(&path), &file, false);
|
2021-07-30 23:26:05 +02:00
|
|
|
if let Some(err) = err {
|
2021-09-20 23:37:26 +02:00
|
|
|
report_error(&working_set, &err);
|
2021-08-10 07:08:10 +02:00
|
|
|
|
2021-07-30 23:26:05 +02:00
|
|
|
std::process::exit(1);
|
|
|
|
}
|
|
|
|
(output, working_set.render())
|
|
|
|
};
|
2021-07-16 08:24:46 +02:00
|
|
|
|
2022-01-04 23:30:34 +01:00
|
|
|
if let Err(err) = engine_state.merge_delta(delta, None, &init_cwd) {
|
2021-11-19 03:51:42 +01:00
|
|
|
let working_set = StateWorkingSet::new(&engine_state);
|
|
|
|
report_error(&working_set, &err);
|
|
|
|
}
|
2021-07-03 05:35:15 +02:00
|
|
|
|
2021-10-25 08:31:39 +02:00
|
|
|
let mut stack = nu_protocol::engine::Stack::new();
|
2021-07-16 03:10:22 +02:00
|
|
|
|
2021-11-14 20:25:57 +01:00
|
|
|
// Set up our initial config to start from
|
|
|
|
stack.vars.insert(
|
|
|
|
CONFIG_VARIABLE_ID,
|
|
|
|
Value::Record {
|
|
|
|
cols: vec![],
|
|
|
|
vals: vec![],
|
2021-12-19 08:46:13 +01:00
|
|
|
span: Span { start: 0, end: 0 },
|
2021-11-14 20:25:57 +01:00
|
|
|
},
|
|
|
|
);
|
|
|
|
|
2021-12-13 04:16:51 +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()
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2021-12-17 02:04:54 +01:00
|
|
|
// Translate environment variables from Strings to Values
|
2022-01-04 23:30:34 +01:00
|
|
|
if let Some(e) = convert_env_values(&mut engine_state, &stack, &config) {
|
2021-12-17 02:04:54 +01:00
|
|
|
let working_set = StateWorkingSet::new(&engine_state);
|
|
|
|
report_error(&working_set, &e);
|
|
|
|
std::process::exit(1);
|
|
|
|
}
|
|
|
|
|
2021-11-06 06:50:33 +01:00
|
|
|
match eval_block(
|
|
|
|
&engine_state,
|
|
|
|
&mut stack,
|
|
|
|
&block,
|
2021-12-19 08:46:13 +01:00
|
|
|
PipelineData::new(Span::new(0, 0)), // Don't try this at home, 0 span is ignored
|
2021-11-06 06:50:33 +01:00
|
|
|
) {
|
2021-10-25 08:31:39 +02:00
|
|
|
Ok(pipeline_data) => {
|
2021-12-03 07:15:23 +01:00
|
|
|
for item in pipeline_data {
|
|
|
|
if let Value::Error { error } = item {
|
|
|
|
let working_set = StateWorkingSet::new(&engine_state);
|
|
|
|
|
|
|
|
report_error(&working_set, &error);
|
|
|
|
|
|
|
|
std::process::exit(1);
|
|
|
|
}
|
|
|
|
println!("{}", item.into_string("\n", &config));
|
|
|
|
}
|
2021-12-04 18:06:17 +01:00
|
|
|
|
|
|
|
// Next, let's check if there are any flags we want to pass to the main function
|
|
|
|
let args: Vec<String> = std::env::args().skip(2).collect();
|
|
|
|
|
2021-12-04 19:02:53 +01:00
|
|
|
if args.is_empty() && engine_state.find_decl(b"main").is_none() {
|
2021-12-04 18:06:17 +01:00
|
|
|
return Ok(());
|
|
|
|
}
|
|
|
|
|
|
|
|
let args = format!("main {}", args.join(" ")).as_bytes().to_vec();
|
|
|
|
|
|
|
|
let (block, delta) = {
|
|
|
|
let mut working_set = StateWorkingSet::new(&engine_state);
|
|
|
|
let (output, err) = parse(&mut working_set, Some("<cmdline>"), &args, false);
|
|
|
|
if let Some(err) = err {
|
|
|
|
report_error(&working_set, &err);
|
|
|
|
|
|
|
|
std::process::exit(1);
|
|
|
|
}
|
|
|
|
(output, working_set.render())
|
|
|
|
};
|
|
|
|
|
2022-01-04 23:30:34 +01:00
|
|
|
let cwd = nu_engine::env::current_dir_str(&engine_state, &stack)?;
|
|
|
|
|
|
|
|
if let Err(err) = engine_state.merge_delta(delta, Some(&mut stack), &cwd) {
|
2021-12-04 18:06:17 +01:00
|
|
|
let working_set = StateWorkingSet::new(&engine_state);
|
|
|
|
report_error(&working_set, &err);
|
|
|
|
}
|
|
|
|
|
|
|
|
match eval_block(
|
|
|
|
&engine_state,
|
|
|
|
&mut stack,
|
|
|
|
&block,
|
2021-12-19 08:46:13 +01:00
|
|
|
PipelineData::new(Span::new(0, 0)), // Don't try this at home, 0 span is ignored
|
2021-12-04 18:06:17 +01:00
|
|
|
) {
|
|
|
|
Ok(pipeline_data) => {
|
|
|
|
for item in pipeline_data {
|
|
|
|
if let Value::Error { error } = item {
|
|
|
|
let working_set = StateWorkingSet::new(&engine_state);
|
|
|
|
|
|
|
|
report_error(&working_set, &error);
|
|
|
|
|
|
|
|
std::process::exit(1);
|
|
|
|
}
|
|
|
|
println!("{}", item.into_string("\n", &config));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Err(err) => {
|
|
|
|
let working_set = StateWorkingSet::new(&engine_state);
|
|
|
|
|
|
|
|
report_error(&working_set, &err);
|
|
|
|
|
|
|
|
std::process::exit(1);
|
|
|
|
}
|
|
|
|
}
|
2021-07-30 22:02:16 +02:00
|
|
|
}
|
|
|
|
Err(err) => {
|
2021-10-25 08:31:39 +02:00
|
|
|
let working_set = StateWorkingSet::new(&engine_state);
|
2021-08-10 07:08:10 +02:00
|
|
|
|
2021-09-20 23:37:26 +02:00
|
|
|
report_error(&working_set, &err);
|
2021-08-10 07:08:10 +02:00
|
|
|
|
2021-07-30 22:02:16 +02:00
|
|
|
std::process::exit(1);
|
|
|
|
}
|
|
|
|
}
|
2021-06-30 03:42:56 +02:00
|
|
|
|
|
|
|
Ok(())
|
|
|
|
} else {
|
2021-10-15 20:37:58 +02:00
|
|
|
use reedline::{FileBackedHistory, Reedline, Signal};
|
2021-07-22 08:04:50 +02:00
|
|
|
|
2021-09-22 05:14:57 +02:00
|
|
|
let mut entry_num = 0;
|
2021-09-09 11:06:55 +02:00
|
|
|
|
2021-10-02 15:10:28 +02:00
|
|
|
let mut nu_prompt = NushellPrompt::new();
|
2021-10-25 08:31:39 +02:00
|
|
|
let mut stack = nu_protocol::engine::Stack::new();
|
2021-07-22 08:04:50 +02:00
|
|
|
|
2021-12-17 02:04:54 +01:00
|
|
|
// First, set up env vars as strings only
|
2022-01-04 23:30:34 +01:00
|
|
|
gather_parent_env_vars(&mut engine_state);
|
2021-10-29 20:15:17 +02:00
|
|
|
|
2021-11-14 20:25:57 +01:00
|
|
|
// Set up our initial config to start from
|
|
|
|
stack.vars.insert(
|
|
|
|
CONFIG_VARIABLE_ID,
|
|
|
|
Value::Record {
|
|
|
|
cols: vec![],
|
|
|
|
vals: vec![],
|
2021-12-19 08:46:13 +01:00
|
|
|
span: Span::new(0, 0),
|
2021-11-14 20:25:57 +01:00
|
|
|
},
|
|
|
|
);
|
|
|
|
|
2022-01-02 04:20:33 +01:00
|
|
|
#[cfg(feature = "plugin")]
|
|
|
|
{
|
|
|
|
// Reading signatures from signature file
|
|
|
|
// The plugin.nu file stores the parsed signature collected from each registered plugin
|
|
|
|
if let Some(mut plugin_path) = nu_path::config_dir() {
|
|
|
|
// Path to store plugins signatures
|
|
|
|
plugin_path.push("nushell");
|
|
|
|
plugin_path.push("plugin.nu");
|
|
|
|
engine_state.plugin_signatures = Some(plugin_path.clone());
|
|
|
|
|
|
|
|
let plugin_filename = plugin_path.to_string_lossy().to_owned();
|
|
|
|
|
|
|
|
if let Ok(contents) = std::fs::read_to_string(&plugin_path) {
|
|
|
|
eval_source(&mut engine_state, &mut stack, &contents, &plugin_filename);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-13 05:57:05 +02:00
|
|
|
// Load config startup file
|
|
|
|
if let Some(mut config_path) = nu_path::config_dir() {
|
|
|
|
config_path.push("nushell");
|
|
|
|
|
2021-12-30 11:47:51 +01:00
|
|
|
// Create config directory if it does not exist
|
|
|
|
if !config_path.exists() {
|
|
|
|
if let Err(err) = std::fs::create_dir_all(&config_path) {
|
|
|
|
eprintln!("Failed to create config directory: {}", err);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
config_path.push("config.nu");
|
|
|
|
|
|
|
|
if config_path.exists() {
|
|
|
|
// FIXME: remove this message when we're ready
|
|
|
|
println!("Loading config from: {:?}", config_path);
|
|
|
|
let config_filename = config_path.to_string_lossy().to_owned();
|
2021-10-13 05:57:05 +02:00
|
|
|
|
2021-12-30 11:47:51 +01:00
|
|
|
if let Ok(contents) = std::fs::read_to_string(&config_path) {
|
|
|
|
eval_source(&mut engine_state, &mut stack, &contents, &config_filename);
|
|
|
|
}
|
2021-10-13 05:57:05 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-17 02:04:54 +01:00
|
|
|
// Get the config
|
|
|
|
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()
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2022-01-01 22:42:50 +01:00
|
|
|
use logger::{configure, logger};
|
|
|
|
|
|
|
|
logger(|builder| {
|
|
|
|
configure(&config.log_level, builder)?;
|
|
|
|
// trace_filters(self, builder)?;
|
|
|
|
// debug_filters(self, builder)?;
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
})?;
|
|
|
|
|
2021-12-17 02:04:54 +01:00
|
|
|
// Translate environment variables from Strings to Values
|
2022-01-04 23:30:34 +01:00
|
|
|
if let Some(e) = convert_env_values(&mut engine_state, &stack, &config) {
|
2021-12-17 02:04:54 +01:00
|
|
|
let working_set = StateWorkingSet::new(&engine_state);
|
|
|
|
report_error(&working_set, &e);
|
|
|
|
}
|
|
|
|
|
2021-12-27 20:14:23 +01:00
|
|
|
let history_path = nu_path::config_dir().and_then(|mut history_path| {
|
2021-10-28 02:30:58 +02:00
|
|
|
history_path.push("nushell");
|
|
|
|
history_path.push("history.txt");
|
|
|
|
|
2021-12-27 20:14:23 +01:00
|
|
|
if !history_path.exists() {
|
|
|
|
// Creating an empty file to store the history
|
|
|
|
match std::fs::File::create(&history_path) {
|
|
|
|
Ok(_) => Some(history_path),
|
|
|
|
Err(_) => None,
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
Some(history_path)
|
|
|
|
}
|
|
|
|
});
|
2021-10-28 02:30:58 +02:00
|
|
|
|
2021-07-22 08:04:50 +02:00
|
|
|
loop {
|
2021-12-13 04:16:51 +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()
|
|
|
|
}
|
|
|
|
};
|
2021-12-02 08:10:40 +01:00
|
|
|
|
2021-10-28 06:13:10 +02:00
|
|
|
//Reset the ctrl-c handler
|
|
|
|
ctrlc.store(false, Ordering::SeqCst);
|
|
|
|
|
2022-01-09 23:17:58 +01:00
|
|
|
let mut keybindings = default_emacs_keybindings();
|
|
|
|
keybindings.add_binding(
|
|
|
|
KeyModifiers::SHIFT,
|
|
|
|
KeyCode::BackTab,
|
|
|
|
ReedlineEvent::Multiple(vec![
|
|
|
|
ReedlineEvent::Edit(vec![EditCommand::InsertChar('p')]),
|
|
|
|
ReedlineEvent::Enter,
|
|
|
|
]),
|
|
|
|
);
|
|
|
|
let edit_mode = Box::new(Emacs::new(keybindings));
|
|
|
|
|
2021-10-28 02:30:58 +02:00
|
|
|
let line_editor = Reedline::create()
|
2021-10-25 08:31:39 +02:00
|
|
|
.into_diagnostic()?
|
|
|
|
.with_completion_action_handler(Box::new(FuzzyCompletion {
|
2021-12-13 00:18:31 +01:00
|
|
|
completer: Box::new(NuCompleter::new(engine_state.clone())),
|
2021-10-25 08:31:39 +02:00
|
|
|
}))
|
2021-10-28 02:30:58 +02:00
|
|
|
.with_highlighter(Box::new(NuHighlighter {
|
|
|
|
engine_state: engine_state.clone(),
|
2021-12-16 13:17:29 +01:00
|
|
|
config: config.clone(),
|
2021-10-28 02:30:58 +02:00
|
|
|
}))
|
2021-12-02 08:10:40 +01:00
|
|
|
.with_animation(config.animate_prompt)
|
2021-10-25 08:31:39 +02:00
|
|
|
// .with_completion_action_handler(Box::new(
|
|
|
|
// ListCompletionHandler::default().with_completer(Box::new(completer)),
|
|
|
|
// ))
|
|
|
|
.with_validator(Box::new(NuValidator {
|
|
|
|
engine_state: engine_state.clone(),
|
2021-12-09 23:06:26 +01:00
|
|
|
}))
|
2022-01-09 23:17:58 +01:00
|
|
|
.with_edit_mode(edit_mode)
|
2022-01-11 22:53:42 +01:00
|
|
|
.with_ansi_colors(config.use_ansi_coloring)
|
|
|
|
.with_menu_completer(Box::new(NuCompleter::new(engine_state.clone())));
|
2021-12-09 23:06:26 +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.
|
2021-10-25 08:31:39 +02:00
|
|
|
|
2021-12-23 22:02:57 +01:00
|
|
|
let color_hm = get_color_config(&config);
|
|
|
|
|
2021-12-23 10:31:16 +01:00
|
|
|
let line_editor = if let Some(history_path) = history_path.clone() {
|
2021-12-20 20:03:18 +01:00
|
|
|
let history = std::fs::read_to_string(&history_path);
|
2021-12-22 10:12:24 +01:00
|
|
|
if history.is_ok() {
|
2021-12-20 20:03:18 +01:00
|
|
|
line_editor
|
|
|
|
.with_hinter(Box::new(
|
2021-12-23 22:02:57 +01:00
|
|
|
DefaultHinter::default()
|
|
|
|
.with_history()
|
|
|
|
.with_style(color_hm["hints"]),
|
2021-12-20 20:03:18 +01:00
|
|
|
))
|
|
|
|
.with_history(Box::new(
|
2021-12-23 20:59:00 +01:00
|
|
|
FileBackedHistory::with_file(
|
|
|
|
config.max_history_size as usize,
|
|
|
|
history_path.clone(),
|
|
|
|
)
|
|
|
|
.into_diagnostic()?,
|
2021-12-20 20:03:18 +01:00
|
|
|
))
|
|
|
|
.into_diagnostic()?
|
|
|
|
} else {
|
|
|
|
line_editor
|
|
|
|
}
|
2021-10-28 02:30:58 +02:00
|
|
|
} else {
|
2021-12-23 10:31:16 +01:00
|
|
|
line_editor
|
|
|
|
};
|
|
|
|
|
|
|
|
// The line editor default mode is emacs mode. For the moment we only
|
|
|
|
// need to check for vi mode
|
|
|
|
let mut line_editor = if config.edit_mode == "vi" {
|
|
|
|
let edit_mode = Box::new(Vi::default());
|
|
|
|
line_editor.with_edit_mode(edit_mode)
|
|
|
|
} else {
|
2021-10-28 02:30:58 +02:00
|
|
|
line_editor
|
|
|
|
};
|
|
|
|
|
2022-01-01 23:53:16 +01:00
|
|
|
let prompt = update_prompt(&config, &engine_state, &stack, &mut nu_prompt);
|
2021-10-02 15:10:28 +02:00
|
|
|
|
2021-09-22 05:14:57 +02:00
|
|
|
entry_num += 1;
|
|
|
|
|
2021-10-02 15:10:28 +02:00
|
|
|
let input = line_editor.read_line(prompt);
|
2021-07-22 08:04:50 +02:00
|
|
|
match input {
|
2022-01-01 22:42:50 +01:00
|
|
|
Ok(Signal::Success(s)) => {
|
|
|
|
let tokens = lex(s.as_bytes(), 0, &[], &[], false);
|
2021-12-27 04:04:22 +01:00
|
|
|
// Check if this is a single call to a directory, if so auto-cd
|
2022-01-04 23:30:34 +01:00
|
|
|
let cwd = nu_engine::env::current_dir_str(&engine_state, &stack)?;
|
|
|
|
let path = nu_path::expand_path_with(&s, &cwd);
|
2022-01-05 01:26:01 +01:00
|
|
|
|
2021-12-27 04:04:22 +01:00
|
|
|
let orig = s.clone();
|
|
|
|
|
|
|
|
if (orig.starts_with('.')
|
|
|
|
|| orig.starts_with('~')
|
|
|
|
|| orig.starts_with('/')
|
|
|
|
|| orig.starts_with('\\'))
|
|
|
|
&& path.is_dir()
|
2022-01-01 22:42:50 +01:00
|
|
|
&& tokens.0.len() == 1
|
2021-12-27 04:04:22 +01:00
|
|
|
{
|
|
|
|
// We have an auto-cd
|
2022-01-05 11:48:55 +01:00
|
|
|
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)
|
|
|
|
};
|
|
|
|
|
2021-12-27 04:04:22 +01:00
|
|
|
//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 {
|
2022-01-05 11:48:55 +01:00
|
|
|
val: path.clone(),
|
2021-12-27 04:04:22 +01:00
|
|
|
span: Span { start: 0, end: 0 },
|
|
|
|
},
|
|
|
|
);
|
2022-01-05 11:48:55 +01:00
|
|
|
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 },
|
|
|
|
);
|
2022-01-05 05:35:50 +01:00
|
|
|
} else {
|
|
|
|
trace!("eval source: {}", s);
|
|
|
|
|
|
|
|
eval_source(
|
|
|
|
&mut engine_state,
|
|
|
|
&mut stack,
|
|
|
|
&s,
|
|
|
|
&format!("entry #{}", entry_num),
|
|
|
|
);
|
2021-12-27 04:04:22 +01:00
|
|
|
}
|
2022-01-05 05:35:50 +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
|
2022-01-05 06:50:27 +01:00
|
|
|
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);
|
|
|
|
}
|
2021-07-22 08:04:50 +02:00
|
|
|
}
|
2021-08-09 07:29:25 +02:00
|
|
|
Ok(Signal::CtrlC) => {
|
2021-12-16 22:40:12 +01:00
|
|
|
// `Reedline` clears the line content. New prompt is shown
|
2021-07-22 08:04:50 +02:00
|
|
|
}
|
2021-08-09 07:29:25 +02:00
|
|
|
Ok(Signal::CtrlD) => {
|
2021-12-16 22:40:12 +01:00
|
|
|
// When exiting clear to a new line
|
|
|
|
println!();
|
2021-07-22 08:04:50 +02:00
|
|
|
break;
|
|
|
|
}
|
2021-08-09 07:29:25 +02:00
|
|
|
Ok(Signal::CtrlL) => {
|
2021-09-20 23:37:26 +02:00
|
|
|
line_editor.clear_screen().into_diagnostic()?;
|
2021-07-22 08:04:50 +02:00
|
|
|
}
|
2021-08-09 07:29:25 +02:00
|
|
|
Err(err) => {
|
2021-09-07 09:41:52 +02:00
|
|
|
let message = err.to_string();
|
|
|
|
if !message.contains("duration") {
|
|
|
|
println!("Error: {:?}", err);
|
|
|
|
}
|
2021-08-09 07:29:25 +02:00
|
|
|
}
|
2021-07-22 08:04:50 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-30 03:42:56 +02:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
2021-09-25 17:58:50 +02:00
|
|
|
|
2021-12-17 02:04:54 +01:00
|
|
|
// This fill collect environment variables from std::env and adds them to a stack.
|
|
|
|
//
|
|
|
|
// In order to ensure the values have spans, it first creates a dummy file, writes the collected
|
2021-12-21 22:31:30 +01:00
|
|
|
// env vars into it (in a "NAME"="value" format, quite similar to the output of the Unix 'env'
|
|
|
|
// tool), then uses the file to get the spans. The file stays in memory, no filesystem IO is done.
|
2022-01-04 23:30:34 +01:00
|
|
|
fn gather_parent_env_vars(engine_state: &mut EngineState) {
|
|
|
|
// Some helper functions
|
2021-12-21 22:31:30 +01:00
|
|
|
fn get_surround_char(s: &str) -> Option<char> {
|
|
|
|
if s.contains('"') {
|
|
|
|
if s.contains('\'') {
|
|
|
|
None
|
|
|
|
} else {
|
|
|
|
Some('\'')
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
Some('"')
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn report_capture_error(engine_state: &EngineState, env_str: &str, msg: &str) {
|
|
|
|
let working_set = StateWorkingSet::new(engine_state);
|
|
|
|
report_error(
|
|
|
|
&working_set,
|
|
|
|
&ShellError::LabeledError(
|
|
|
|
format!("Environment variable was not captured: {}", env_str),
|
|
|
|
msg.into(),
|
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2022-01-04 23:30:34 +01:00
|
|
|
fn put_env_to_fake_file(
|
|
|
|
name: &str,
|
|
|
|
val: &str,
|
|
|
|
fake_env_file: &mut String,
|
|
|
|
engine_state: &EngineState,
|
|
|
|
) {
|
2021-12-21 22:31:30 +01:00
|
|
|
let (c_name, c_val) =
|
2022-01-04 23:30:34 +01:00
|
|
|
if let (Some(cn), Some(cv)) = (get_surround_char(name), get_surround_char(val)) {
|
2021-12-21 22:31:30 +01:00
|
|
|
(cn, cv)
|
|
|
|
} else {
|
|
|
|
// environment variable with its name or value containing both ' and " is ignored
|
|
|
|
report_capture_error(
|
|
|
|
engine_state,
|
|
|
|
&format!("{}={}", name, val),
|
|
|
|
"Name or value should not contain both ' and \" at the same time.",
|
2021-12-20 22:19:43 +01:00
|
|
|
);
|
2022-01-04 23:30:34 +01:00
|
|
|
return;
|
2021-12-21 22:31:30 +01:00
|
|
|
};
|
2021-12-20 22:19:43 +01:00
|
|
|
|
2021-12-21 22:31:30 +01:00
|
|
|
fake_env_file.push(c_name);
|
2022-01-04 23:30:34 +01:00
|
|
|
fake_env_file.push_str(name);
|
2021-12-21 22:31:30 +01:00
|
|
|
fake_env_file.push(c_name);
|
2021-12-17 02:04:54 +01:00
|
|
|
fake_env_file.push('=');
|
2021-12-21 22:31:30 +01:00
|
|
|
fake_env_file.push(c_val);
|
2022-01-04 23:30:34 +01:00
|
|
|
fake_env_file.push_str(val);
|
2021-12-21 22:31:30 +01:00
|
|
|
fake_env_file.push(c_val);
|
2021-12-17 02:04:54 +01:00
|
|
|
fake_env_file.push('\n');
|
|
|
|
}
|
|
|
|
|
2022-01-04 23:30:34 +01:00
|
|
|
let mut fake_env_file = String::new();
|
|
|
|
|
|
|
|
// Make sure we always have PWD
|
|
|
|
if std::env::var("PWD").is_err() {
|
|
|
|
match std::env::current_dir() {
|
|
|
|
Ok(cwd) => {
|
|
|
|
put_env_to_fake_file(
|
|
|
|
"PWD",
|
|
|
|
&cwd.to_string_lossy(),
|
|
|
|
&mut fake_env_file,
|
|
|
|
engine_state,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
Err(e) => {
|
|
|
|
// Could not capture current working directory
|
|
|
|
let working_set = StateWorkingSet::new(engine_state);
|
|
|
|
report_error(
|
|
|
|
&working_set,
|
|
|
|
&ShellError::LabeledError(
|
|
|
|
"Current directory not found".to_string(),
|
|
|
|
format!("Retrieving current directory failed: {:?}", e),
|
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Write all the env vars into a fake file
|
|
|
|
for (name, val) in std::env::vars() {
|
|
|
|
put_env_to_fake_file(&name, &val, &mut fake_env_file, engine_state);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Lex the fake file, assign spans to all environment variables and add them
|
|
|
|
// to stack
|
2021-12-17 02:04:54 +01:00
|
|
|
let span_offset = engine_state.next_span_start();
|
2021-12-21 22:31:30 +01:00
|
|
|
|
2021-12-17 02:04:54 +01:00
|
|
|
engine_state.add_file(
|
|
|
|
"Host Environment Variables".to_string(),
|
|
|
|
fake_env_file.as_bytes().to_vec(),
|
|
|
|
);
|
2021-12-21 22:31:30 +01:00
|
|
|
|
2021-12-17 02:04:54 +01:00
|
|
|
let (tokens, _) = lex(fake_env_file.as_bytes(), span_offset, &[], &[], true);
|
2021-12-21 22:31:30 +01:00
|
|
|
|
2021-12-17 02:04:54 +01:00
|
|
|
for token in tokens {
|
|
|
|
if let Token {
|
|
|
|
contents: TokenContents::Item,
|
|
|
|
span: full_span,
|
|
|
|
} = token
|
|
|
|
{
|
|
|
|
let contents = engine_state.get_span_contents(&full_span);
|
|
|
|
let (parts, _) = lex(contents, full_span.start, &[], &[b'='], true);
|
|
|
|
|
|
|
|
let name = if let Some(Token {
|
|
|
|
contents: TokenContents::Item,
|
|
|
|
span,
|
|
|
|
}) = parts.get(0)
|
|
|
|
{
|
2021-12-21 22:31:30 +01:00
|
|
|
let bytes = engine_state.get_span_contents(span);
|
|
|
|
|
|
|
|
if bytes.len() < 2 {
|
|
|
|
report_capture_error(
|
|
|
|
engine_state,
|
|
|
|
&String::from_utf8_lossy(contents),
|
|
|
|
"Got empty name.",
|
|
|
|
);
|
|
|
|
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
let bytes = trim_quotes(bytes);
|
|
|
|
String::from_utf8_lossy(bytes).to_string()
|
2021-12-17 02:04:54 +01:00
|
|
|
} else {
|
2021-12-21 22:31:30 +01:00
|
|
|
report_capture_error(
|
|
|
|
engine_state,
|
|
|
|
&String::from_utf8_lossy(contents),
|
|
|
|
"Got empty name.",
|
|
|
|
);
|
|
|
|
|
2021-12-17 02:04:54 +01:00
|
|
|
continue;
|
|
|
|
};
|
|
|
|
|
|
|
|
let value = if let Some(Token {
|
|
|
|
contents: TokenContents::Item,
|
|
|
|
span,
|
|
|
|
}) = parts.get(2)
|
|
|
|
{
|
2021-12-17 22:09:44 +01:00
|
|
|
let bytes = engine_state.get_span_contents(span);
|
2021-12-20 22:19:43 +01:00
|
|
|
|
|
|
|
if bytes.len() < 2 {
|
2021-12-21 22:31:30 +01:00
|
|
|
report_capture_error(
|
|
|
|
engine_state,
|
|
|
|
&String::from_utf8_lossy(contents),
|
|
|
|
"Got empty value.",
|
2021-12-20 22:19:43 +01:00
|
|
|
);
|
2021-12-21 22:31:30 +01:00
|
|
|
|
|
|
|
continue;
|
2021-12-20 22:19:43 +01:00
|
|
|
}
|
|
|
|
|
2021-12-21 22:31:30 +01:00
|
|
|
let bytes = trim_quotes(bytes);
|
2021-12-17 22:09:44 +01:00
|
|
|
|
2021-12-17 02:04:54 +01:00
|
|
|
Value::String {
|
2021-12-17 22:09:44 +01:00
|
|
|
val: String::from_utf8_lossy(bytes).to_string(),
|
2021-12-17 02:04:54 +01:00
|
|
|
span: *span,
|
|
|
|
}
|
|
|
|
} else {
|
2021-12-21 22:31:30 +01:00
|
|
|
report_capture_error(
|
|
|
|
engine_state,
|
|
|
|
&String::from_utf8_lossy(contents),
|
|
|
|
"Got empty value.",
|
2021-12-20 22:19:43 +01:00
|
|
|
);
|
|
|
|
|
2021-12-21 22:31:30 +01:00
|
|
|
continue;
|
2021-12-17 02:04:54 +01:00
|
|
|
};
|
|
|
|
|
2022-01-04 23:30:34 +01:00
|
|
|
// stack.add_env_var(name, value);
|
|
|
|
engine_state.env_vars.insert(name, value);
|
2021-12-17 02:04:54 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-02 06:59:10 +01:00
|
|
|
fn print_pipeline_data(
|
|
|
|
input: PipelineData,
|
2021-11-14 20:25:57 +01:00
|
|
|
engine_state: &EngineState,
|
|
|
|
stack: &mut Stack,
|
|
|
|
) -> Result<(), ShellError> {
|
2021-09-25 17:58:50 +02:00
|
|
|
// If the table function is in the declarations, then we can use it
|
|
|
|
// to create the table value that will be printed in the terminal
|
2021-11-14 20:25:57 +01:00
|
|
|
|
2021-12-13 04:16:51 +01:00
|
|
|
let config = stack.get_config().unwrap_or_default();
|
2021-11-14 20:25:57 +01:00
|
|
|
|
2021-12-24 08:22:11 +01:00
|
|
|
match input {
|
|
|
|
PipelineData::StringStream(stream, _, _) => {
|
|
|
|
for s in stream {
|
|
|
|
print!("{}", s?);
|
|
|
|
let _ = std::io::stdout().flush();
|
|
|
|
}
|
|
|
|
return Ok(());
|
|
|
|
}
|
|
|
|
PipelineData::ByteStream(stream, _, _) => {
|
2022-01-01 11:42:15 +01:00
|
|
|
let mut address_offset = 0;
|
2021-12-24 08:22:11 +01:00
|
|
|
for v in stream {
|
2022-01-01 11:42:15 +01:00
|
|
|
let cfg = nu_pretty_hex::HexConfig {
|
|
|
|
title: false,
|
|
|
|
address_offset,
|
|
|
|
..Default::default()
|
|
|
|
};
|
|
|
|
|
|
|
|
let v = v?;
|
|
|
|
address_offset += v.len();
|
|
|
|
|
2021-12-24 08:22:11 +01:00
|
|
|
let s = if v.iter().all(|x| x.is_ascii()) {
|
2022-01-01 11:42:15 +01:00
|
|
|
format!("{}", String::from_utf8_lossy(&v))
|
2021-12-24 08:22:11 +01:00
|
|
|
} else {
|
2022-01-01 11:42:15 +01:00
|
|
|
nu_pretty_hex::config_hex(&v, cfg)
|
2021-12-24 08:22:11 +01:00
|
|
|
};
|
|
|
|
println!("{}", s);
|
|
|
|
}
|
|
|
|
return Ok(());
|
|
|
|
}
|
|
|
|
_ => {}
|
|
|
|
}
|
|
|
|
|
2021-12-03 07:15:23 +01:00
|
|
|
match engine_state.find_decl("table".as_bytes()) {
|
2021-09-25 17:58:50 +02:00
|
|
|
Some(decl_id) => {
|
2021-12-02 06:59:10 +01:00
|
|
|
let table =
|
|
|
|
engine_state
|
|
|
|
.get_decl(decl_id)
|
|
|
|
.run(engine_state, stack, &Call::new(), input)?;
|
2021-12-03 07:15:23 +01:00
|
|
|
|
|
|
|
for item in table {
|
|
|
|
let stdout = std::io::stdout();
|
|
|
|
|
|
|
|
if let Value::Error { error } = item {
|
|
|
|
return Err(error);
|
|
|
|
}
|
|
|
|
|
|
|
|
let mut out = item.into_string("\n", &config);
|
|
|
|
out.push('\n');
|
|
|
|
|
|
|
|
match stdout.lock().write_all(out.as_bytes()) {
|
|
|
|
Ok(_) => (),
|
|
|
|
Err(err) => eprintln!("{}", err),
|
|
|
|
};
|
|
|
|
}
|
2021-09-25 17:58:50 +02:00
|
|
|
}
|
2021-12-03 07:15:23 +01:00
|
|
|
None => {
|
|
|
|
for item in input {
|
|
|
|
let stdout = std::io::stdout();
|
|
|
|
|
|
|
|
if let Value::Error { error } = item {
|
|
|
|
return Err(error);
|
|
|
|
}
|
|
|
|
|
|
|
|
let mut out = item.into_string("\n", &config);
|
|
|
|
out.push('\n');
|
2021-09-25 17:58:50 +02:00
|
|
|
|
2021-12-03 07:15:23 +01:00
|
|
|
match stdout.lock().write_all(out.as_bytes()) {
|
|
|
|
Ok(_) => (),
|
|
|
|
Err(err) => eprintln!("{}", err),
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
2021-09-25 17:58:50 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
2021-10-02 15:10:28 +02:00
|
|
|
|
2022-01-04 23:30:34 +01:00
|
|
|
fn get_prompt_indicators(
|
|
|
|
config: &Config,
|
|
|
|
engine_state: &EngineState,
|
|
|
|
stack: &Stack,
|
|
|
|
) -> (String, String, String, String) {
|
|
|
|
let prompt_indicator = match stack.get_env_var(engine_state, PROMPT_INDICATOR) {
|
2022-01-01 23:53:16 +01:00
|
|
|
Some(pi) => pi.into_string("", config),
|
|
|
|
None => "〉".to_string(),
|
|
|
|
};
|
|
|
|
|
2022-01-04 23:30:34 +01:00
|
|
|
let prompt_vi_insert = match stack.get_env_var(engine_state, PROMPT_INDICATOR_VI_INSERT) {
|
2022-01-01 23:53:16 +01:00
|
|
|
Some(pvii) => pvii.into_string("", config),
|
|
|
|
None => ": ".to_string(),
|
|
|
|
};
|
|
|
|
|
2022-01-04 23:30:34 +01:00
|
|
|
let prompt_vi_visual = match stack.get_env_var(engine_state, PROMPT_INDICATOR_VI_VISUAL) {
|
2022-01-01 23:53:16 +01:00
|
|
|
Some(pviv) => pviv.into_string("", config),
|
|
|
|
None => "v ".to_string(),
|
|
|
|
};
|
|
|
|
|
2022-01-04 23:30:34 +01:00
|
|
|
let prompt_multiline = match stack.get_env_var(engine_state, PROMPT_MULTILINE_INDICATOR) {
|
2022-01-01 23:53:16 +01:00
|
|
|
Some(pm) => pm.into_string("", config),
|
|
|
|
None => "::: ".to_string(),
|
|
|
|
};
|
|
|
|
|
|
|
|
(
|
|
|
|
prompt_indicator,
|
|
|
|
prompt_vi_insert,
|
|
|
|
prompt_vi_visual,
|
|
|
|
prompt_multiline,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2021-10-02 15:10:28 +02:00
|
|
|
fn update_prompt<'prompt>(
|
2022-01-01 23:53:16 +01:00
|
|
|
config: &Config,
|
2021-10-25 08:31:39 +02:00
|
|
|
engine_state: &EngineState,
|
2021-10-02 15:10:28 +02:00
|
|
|
stack: &Stack,
|
|
|
|
nu_prompt: &'prompt mut NushellPrompt,
|
|
|
|
) -> &'prompt dyn Prompt {
|
2022-01-01 23:53:16 +01:00
|
|
|
// get the other indicators
|
|
|
|
let (
|
|
|
|
prompt_indicator_string,
|
|
|
|
prompt_vi_insert_string,
|
|
|
|
prompt_vi_visual_string,
|
|
|
|
prompt_multiline_string,
|
2022-01-04 23:30:34 +01:00
|
|
|
) = get_prompt_indicators(config, engine_state, stack);
|
2022-01-01 23:53:16 +01:00
|
|
|
|
2021-10-25 08:31:39 +02:00
|
|
|
let mut stack = stack.clone();
|
2021-10-02 15:10:28 +02:00
|
|
|
|
2022-01-06 13:57:55 +01:00
|
|
|
// apply the other indicators
|
|
|
|
nu_prompt.update_all_prompt_strings(
|
|
|
|
get_prompt_string(PROMPT_COMMAND, config, engine_state, &mut stack),
|
|
|
|
get_prompt_string(PROMPT_COMMAND_RIGHT, config, engine_state, &mut stack),
|
|
|
|
prompt_indicator_string,
|
|
|
|
prompt_vi_insert_string,
|
|
|
|
prompt_vi_visual_string,
|
|
|
|
prompt_multiline_string,
|
|
|
|
);
|
2021-10-02 15:10:28 +02:00
|
|
|
|
|
|
|
nu_prompt as &dyn Prompt
|
|
|
|
}
|
2021-10-13 05:57:05 +02:00
|
|
|
|
2022-01-06 13:57:55 +01:00
|
|
|
fn get_prompt_string(
|
|
|
|
prompt: &str,
|
|
|
|
config: &Config,
|
|
|
|
engine_state: &EngineState,
|
|
|
|
stack: &mut Stack,
|
|
|
|
) -> Option<String> {
|
|
|
|
stack
|
|
|
|
.get_env_var(engine_state, prompt)
|
|
|
|
.and_then(|v| v.as_block().ok())
|
|
|
|
.and_then(|block_id| {
|
|
|
|
let block = engine_state.get_block(block_id);
|
|
|
|
eval_block(
|
|
|
|
engine_state,
|
|
|
|
stack,
|
|
|
|
block,
|
|
|
|
PipelineData::new(Span::new(0, 0)), // Don't try this at home, 0 span is ignored
|
|
|
|
)
|
|
|
|
.ok()
|
|
|
|
})
|
|
|
|
.and_then(|pipeline_data| pipeline_data.collect_string("", config).ok())
|
|
|
|
}
|
|
|
|
|
2021-10-13 05:57:05 +02:00
|
|
|
fn eval_source(
|
2021-10-25 08:31:39 +02:00
|
|
|
engine_state: &mut EngineState,
|
|
|
|
stack: &mut Stack,
|
2021-10-13 05:57:05 +02:00
|
|
|
source: &str,
|
|
|
|
fname: &str,
|
|
|
|
) -> bool {
|
2022-01-01 22:42:50 +01:00
|
|
|
trace!("eval_source");
|
|
|
|
|
2021-10-13 05:57:05 +02:00
|
|
|
let (block, delta) = {
|
2021-10-25 08:42:38 +02:00
|
|
|
let mut working_set = StateWorkingSet::new(engine_state);
|
2021-10-13 05:57:05 +02:00
|
|
|
let (output, err) = parse(
|
|
|
|
&mut working_set,
|
|
|
|
Some(fname), // format!("entry #{}", entry_num)
|
|
|
|
source.as_bytes(),
|
|
|
|
false,
|
|
|
|
);
|
|
|
|
if let Some(err) = err {
|
|
|
|
report_error(&working_set, &err);
|
|
|
|
return false;
|
|
|
|
}
|
2021-12-02 06:42:56 +01:00
|
|
|
|
2021-10-13 05:57:05 +02:00
|
|
|
(output, working_set.render())
|
|
|
|
};
|
|
|
|
|
2022-01-04 23:30:34 +01:00
|
|
|
let cwd = match nu_engine::env::current_dir_str(engine_state, stack) {
|
|
|
|
Ok(p) => PathBuf::from(p),
|
|
|
|
Err(e) => {
|
|
|
|
let working_set = StateWorkingSet::new(engine_state);
|
|
|
|
report_error(&working_set, &e);
|
|
|
|
get_init_cwd()
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
if let Err(err) = engine_state.merge_delta(delta, Some(stack), &cwd) {
|
2021-11-19 03:51:42 +01:00
|
|
|
let working_set = StateWorkingSet::new(engine_state);
|
|
|
|
report_error(&working_set, &err);
|
|
|
|
}
|
2021-10-13 05:57:05 +02:00
|
|
|
|
2021-11-06 06:50:33 +01:00
|
|
|
match eval_block(
|
|
|
|
engine_state,
|
|
|
|
stack,
|
|
|
|
&block,
|
2021-12-19 08:46:13 +01:00
|
|
|
PipelineData::new(Span::new(0, 0)), // Don't try this at home, 0 span is ignored
|
2021-11-06 06:50:33 +01:00
|
|
|
) {
|
2021-10-25 08:31:39 +02:00
|
|
|
Ok(pipeline_data) => {
|
2021-12-02 06:59:10 +01:00
|
|
|
if let Err(err) = print_pipeline_data(pipeline_data, engine_state, stack) {
|
2021-10-25 08:42:38 +02:00
|
|
|
let working_set = StateWorkingSet::new(engine_state);
|
2021-10-13 05:57:05 +02:00
|
|
|
|
|
|
|
report_error(&working_set, &err);
|
2021-12-03 20:49:25 +01:00
|
|
|
|
2021-10-13 05:57:05 +02:00
|
|
|
return false;
|
|
|
|
}
|
2021-12-03 20:49:25 +01:00
|
|
|
|
|
|
|
// reset vt processing, aka ansi because illbehaved externals can break it
|
|
|
|
#[cfg(windows)]
|
|
|
|
{
|
|
|
|
let _ = enable_vt_processing();
|
|
|
|
}
|
2021-10-13 05:57:05 +02:00
|
|
|
}
|
|
|
|
Err(err) => {
|
2021-10-25 08:42:38 +02:00
|
|
|
let working_set = StateWorkingSet::new(engine_state);
|
2021-10-13 05:57:05 +02:00
|
|
|
|
|
|
|
report_error(&working_set, &err);
|
2021-12-03 20:49:25 +01:00
|
|
|
|
2021-10-13 05:57:05 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
true
|
|
|
|
}
|
2021-12-03 20:49:25 +01:00
|
|
|
|
|
|
|
#[cfg(windows)]
|
2021-12-04 06:02:57 +01:00
|
|
|
pub fn enable_vt_processing() -> Result<(), ShellError> {
|
2021-12-03 20:49:25 +01:00
|
|
|
pub const ENABLE_PROCESSED_OUTPUT: u32 = 0x0001;
|
|
|
|
pub const ENABLE_VIRTUAL_TERMINAL_PROCESSING: u32 = 0x0004;
|
|
|
|
// let mask = ENABLE_VIRTUAL_TERMINAL_PROCESSING;
|
|
|
|
|
|
|
|
let console_mode = ConsoleMode::from(Handle::current_out_handle()?);
|
|
|
|
let old_mode = console_mode.mode()?;
|
|
|
|
|
|
|
|
// researching odd ansi behavior in windows terminal repo revealed that
|
|
|
|
// enable_processed_output and enable_virtual_terminal_processing should be used
|
|
|
|
// also, instead of checking old_mode & mask, just set the mode already
|
|
|
|
|
|
|
|
// if old_mode & mask == 0 {
|
|
|
|
console_mode
|
|
|
|
.set_mode(old_mode | ENABLE_PROCESSED_OUTPUT | ENABLE_VIRTUAL_TERMINAL_PROCESSING)?;
|
|
|
|
// }
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
2021-12-04 06:02:57 +01:00
|
|
|
|
|
|
|
pub fn report_error(
|
|
|
|
working_set: &StateWorkingSet,
|
|
|
|
error: &(dyn miette::Diagnostic + Send + Sync + 'static),
|
|
|
|
) {
|
|
|
|
eprintln!("Error: {:?}", CliError(error, working_set));
|
|
|
|
// reset vt processing, aka ansi because illbehaved externals can break it
|
|
|
|
#[cfg(windows)]
|
|
|
|
{
|
|
|
|
let _ = enable_vt_processing();
|
|
|
|
}
|
|
|
|
}
|
2022-01-04 23:30:34 +01:00
|
|
|
|
|
|
|
fn get_init_cwd() -> PathBuf {
|
|
|
|
match std::env::current_dir() {
|
|
|
|
Ok(cwd) => cwd,
|
|
|
|
Err(_) => match std::env::var("PWD".to_string()) {
|
|
|
|
Ok(cwd) => PathBuf::from(cwd),
|
|
|
|
Err(_) => match nu_path::home_dir() {
|
|
|
|
Some(cwd) => cwd,
|
|
|
|
None => PathBuf::new(),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|