2021-10-28 06:13:10 +02:00
|
|
|
use std::{
|
|
|
|
io::Write,
|
|
|
|
sync::{
|
|
|
|
atomic::{AtomicBool, Ordering},
|
|
|
|
Arc,
|
|
|
|
},
|
|
|
|
};
|
2021-09-25 16:56:33 +02:00
|
|
|
|
2021-10-15 22:42:36 +02:00
|
|
|
use dialoguer::{
|
|
|
|
console::{Style, Term},
|
|
|
|
theme::ColorfulTheme,
|
|
|
|
Select,
|
|
|
|
};
|
2021-09-20 23:37:26 +02:00
|
|
|
use miette::{IntoDiagnostic, Result};
|
2021-10-02 15:10:28 +02:00
|
|
|
use nu_cli::{report_error, NuCompleter, NuHighlighter, NuValidator, NushellPrompt};
|
2021-09-03 00:58:15 +02:00
|
|
|
use nu_command::create_default_context;
|
2021-08-10 20:51:08 +02:00
|
|
|
use nu_engine::eval_block;
|
2021-09-10 00:09:40 +02:00
|
|
|
use nu_parser::parse;
|
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-10-25 08:31:39 +02:00
|
|
|
IntoPipelineData, PipelineData, ShellError, Value,
|
2021-09-03 04:15:01 +02:00
|
|
|
};
|
2021-10-15 20:37:58 +02:00
|
|
|
use reedline::{Completer, CompletionActionHandler, DefaultPrompt, LineBuffer, Prompt};
|
2021-06-30 03:42:56 +02:00
|
|
|
|
2021-08-10 20:57:08 +02:00
|
|
|
#[cfg(test)]
|
|
|
|
mod tests;
|
|
|
|
|
2021-10-02 15:10:28 +02:00
|
|
|
// Name of environment variable where the prompt could be stored
|
|
|
|
const PROMPT_COMMAND: &str = "PROMPT_COMMAND";
|
|
|
|
|
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-10-15 20:37:58 +02:00
|
|
|
.unwrap();
|
|
|
|
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-09-21 21:37:16 +02: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| {
|
|
|
|
crossterm::terminal::disable_raw_mode().unwrap();
|
|
|
|
miette_hook(x);
|
|
|
|
}));
|
2021-09-21 21:37:16 +02:00
|
|
|
|
2021-10-25 08:31:39 +02:00
|
|
|
let mut engine_state = create_default_context();
|
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) {
|
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);
|
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
|
|
|
|
2021-11-02 20:53:48 +01:00
|
|
|
engine_state.merge_delta(delta);
|
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-10-29 20:15:17 +02:00
|
|
|
for (k, v) in std::env::vars() {
|
|
|
|
stack.env_vars.insert(k, v);
|
|
|
|
}
|
|
|
|
|
2021-10-25 08:31:39 +02:00
|
|
|
match eval_block(&engine_state, &mut stack, &block, PipelineData::new()) {
|
|
|
|
Ok(pipeline_data) => {
|
|
|
|
println!("{}", pipeline_data.collect_string());
|
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-10 00:09:40 +02:00
|
|
|
let completer = NuCompleter::new(engine_state.clone());
|
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 default_prompt = DefaultPrompt::new(1);
|
|
|
|
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-10-29 20:15:17 +02:00
|
|
|
for (k, v) in std::env::vars() {
|
|
|
|
stack.env_vars.insert(k, v);
|
|
|
|
}
|
|
|
|
|
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");
|
|
|
|
config_path.push("config.nu");
|
|
|
|
|
|
|
|
// FIXME: remove this message when we're ready
|
|
|
|
println!("Loading config from: {:?}", config_path);
|
|
|
|
|
|
|
|
if config_path.exists() {
|
|
|
|
let config_filename = config_path.to_string_lossy().to_owned();
|
|
|
|
|
|
|
|
if let Ok(contents) = std::fs::read_to_string(&config_path) {
|
2021-10-25 08:31:39 +02:00
|
|
|
eval_source(&mut engine_state, &mut stack, &contents, &config_filename);
|
2021-10-13 05:57:05 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-28 02:30:58 +02:00
|
|
|
let history_path = if let Some(mut history_path) = nu_path::config_dir() {
|
|
|
|
history_path.push("nushell");
|
|
|
|
history_path.push("history.txt");
|
|
|
|
|
|
|
|
Some(history_path)
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
};
|
|
|
|
|
2021-07-22 08:04:50 +02:00
|
|
|
loop {
|
2021-10-28 06:13:10 +02:00
|
|
|
//Reset the ctrl-c handler
|
|
|
|
ctrlc.store(false, Ordering::SeqCst);
|
|
|
|
|
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 {
|
|
|
|
completer: Box::new(completer.clone()),
|
|
|
|
}))
|
2021-10-28 02:30:58 +02:00
|
|
|
.with_highlighter(Box::new(NuHighlighter {
|
|
|
|
engine_state: engine_state.clone(),
|
|
|
|
}))
|
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-10-28 02:30:58 +02:00
|
|
|
let mut line_editor = if let Some(history_path) = history_path.clone() {
|
|
|
|
line_editor
|
|
|
|
.with_history(Box::new(
|
|
|
|
FileBackedHistory::with_file(1000, history_path.clone())
|
|
|
|
.into_diagnostic()?,
|
|
|
|
))
|
|
|
|
.into_diagnostic()?
|
|
|
|
} else {
|
|
|
|
line_editor
|
|
|
|
};
|
|
|
|
|
2021-10-02 15:10:28 +02:00
|
|
|
let prompt = update_prompt(
|
|
|
|
PROMPT_COMMAND,
|
2021-10-25 08:31:39 +02:00
|
|
|
&engine_state,
|
2021-10-02 15:10:28 +02:00
|
|
|
&stack,
|
|
|
|
&mut nu_prompt,
|
|
|
|
&default_prompt,
|
|
|
|
);
|
|
|
|
|
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 {
|
2021-08-09 07:29:25 +02:00
|
|
|
Ok(Signal::Success(s)) => {
|
2021-07-22 08:04:50 +02:00
|
|
|
if s.trim() == "exit" {
|
|
|
|
break;
|
|
|
|
}
|
2021-07-22 09:33:38 +02:00
|
|
|
|
2021-10-13 05:57:05 +02:00
|
|
|
eval_source(
|
2021-10-25 08:31:39 +02:00
|
|
|
&mut engine_state,
|
|
|
|
&mut stack,
|
2021-10-13 05:57:05 +02:00
|
|
|
&s,
|
|
|
|
&format!("entry #{}", entry_num),
|
|
|
|
);
|
2021-07-22 08:04:50 +02:00
|
|
|
}
|
2021-08-09 07:29:25 +02:00
|
|
|
Ok(Signal::CtrlC) => {
|
2021-07-22 08:04:50 +02:00
|
|
|
println!("Ctrl-c");
|
|
|
|
}
|
2021-08-09 07:29:25 +02:00
|
|
|
Ok(Signal::CtrlD) => {
|
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-10-25 08:31:39 +02:00
|
|
|
fn print_value(value: Value, engine_state: &EngineState) -> 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
|
|
|
|
let output = match engine_state.find_decl("table".as_bytes()) {
|
|
|
|
Some(decl_id) => {
|
2021-10-25 08:31:39 +02:00
|
|
|
let mut stack = Stack::new();
|
|
|
|
let table = engine_state.get_decl(decl_id).run(
|
|
|
|
engine_state,
|
|
|
|
&mut stack,
|
|
|
|
&Call::new(),
|
|
|
|
value.into_pipeline_data(),
|
|
|
|
)?;
|
|
|
|
table.collect_string()
|
2021-09-25 17:58:50 +02:00
|
|
|
}
|
|
|
|
None => value.into_string(),
|
|
|
|
};
|
|
|
|
let stdout = std::io::stdout();
|
|
|
|
|
|
|
|
match stdout.lock().write_all(output.as_bytes()) {
|
|
|
|
Ok(_) => (),
|
|
|
|
Err(err) => eprintln!("{}", err),
|
|
|
|
};
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
2021-10-02 15:10:28 +02:00
|
|
|
|
|
|
|
fn update_prompt<'prompt>(
|
|
|
|
env_variable: &str,
|
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,
|
|
|
|
default_prompt: &'prompt DefaultPrompt,
|
|
|
|
) -> &'prompt dyn Prompt {
|
|
|
|
let prompt_command = match stack.get_env_var(env_variable) {
|
|
|
|
Some(prompt) => prompt,
|
|
|
|
None => return default_prompt as &dyn Prompt,
|
|
|
|
};
|
|
|
|
|
|
|
|
// Checking if the PROMPT_COMMAND is the same to avoid evaluating constantly
|
|
|
|
// the same command, thus saturating the contents in the EngineState
|
|
|
|
if !nu_prompt.is_new_prompt(prompt_command.as_str()) {
|
|
|
|
return nu_prompt as &dyn Prompt;
|
|
|
|
}
|
|
|
|
|
2021-10-25 18:58:58 +02:00
|
|
|
let block = {
|
|
|
|
let mut working_set = StateWorkingSet::new(engine_state);
|
2021-10-02 15:10:28 +02:00
|
|
|
let (output, err) = parse(&mut working_set, None, prompt_command.as_bytes(), false);
|
|
|
|
if let Some(err) = err {
|
|
|
|
report_error(&working_set, &err);
|
|
|
|
return default_prompt as &dyn Prompt;
|
|
|
|
}
|
2021-10-25 18:58:58 +02:00
|
|
|
output
|
2021-10-02 15:10:28 +02:00
|
|
|
};
|
|
|
|
|
2021-10-25 08:31:39 +02:00
|
|
|
let mut stack = stack.clone();
|
2021-10-02 15:10:28 +02:00
|
|
|
|
2021-10-25 18:58:58 +02:00
|
|
|
let evaluated_prompt = match eval_block(engine_state, &mut stack, &block, PipelineData::new()) {
|
2021-10-25 08:31:39 +02:00
|
|
|
Ok(pipeline_data) => pipeline_data.collect_string(),
|
2021-10-02 15:10:28 +02:00
|
|
|
Err(err) => {
|
2021-10-25 18:58:58 +02:00
|
|
|
let working_set = StateWorkingSet::new(engine_state);
|
2021-10-02 15:10:28 +02:00
|
|
|
report_error(&working_set, &err);
|
|
|
|
return default_prompt as &dyn Prompt;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
nu_prompt.update_prompt(prompt_command, evaluated_prompt);
|
|
|
|
|
|
|
|
nu_prompt as &dyn Prompt
|
|
|
|
}
|
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 {
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
(output, working_set.render())
|
|
|
|
};
|
|
|
|
|
2021-11-02 20:53:48 +01:00
|
|
|
engine_state.merge_delta(delta);
|
2021-10-13 05:57:05 +02:00
|
|
|
|
2021-10-25 08:42:38 +02:00
|
|
|
match eval_block(engine_state, stack, &block, PipelineData::new()) {
|
2021-10-25 08:31:39 +02:00
|
|
|
Ok(pipeline_data) => {
|
2021-10-25 08:42:38 +02:00
|
|
|
if let Err(err) = print_value(pipeline_data.into_value(), engine_state) {
|
|
|
|
let working_set = StateWorkingSet::new(engine_state);
|
2021-10-13 05:57:05 +02:00
|
|
|
|
|
|
|
report_error(&working_set, &err);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
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);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
true
|
|
|
|
}
|