2021-09-25 14:56:33 +00:00
|
|
|
use std::io::Write;
|
|
|
|
|
2021-09-20 21:37:26 +00:00
|
|
|
use miette::{IntoDiagnostic, Result};
|
2021-09-22 05:29:53 +00:00
|
|
|
use nu_cli::{report_error, NuCompleter, NuHighlighter, NuValidator};
|
2021-09-02 22:58:15 +00:00
|
|
|
use nu_command::create_default_context;
|
2021-08-10 18:51:08 +00:00
|
|
|
use nu_engine::eval_block;
|
2021-09-09 22:09:40 +00:00
|
|
|
use nu_parser::parse;
|
2021-09-03 02:15:01 +00:00
|
|
|
use nu_protocol::{
|
2021-09-25 14:47:23 +00:00
|
|
|
ast::Call,
|
2021-09-03 02:15:01 +00:00
|
|
|
engine::{EngineState, EvaluationContext, StateWorkingSet},
|
2021-09-25 15:58:50 +00:00
|
|
|
ShellError, Value,
|
2021-09-03 02:15:01 +00:00
|
|
|
};
|
2021-09-09 22:09:40 +00:00
|
|
|
use reedline::DefaultCompletionActionHandler;
|
2021-06-30 01:42:56 +00:00
|
|
|
|
2021-08-10 18:57:08 +00:00
|
|
|
#[cfg(test)]
|
|
|
|
mod tests;
|
|
|
|
|
2021-09-20 21:37:26 +00:00
|
|
|
fn main() -> Result<()> {
|
2021-09-21 19:37:16 +00:00
|
|
|
miette::set_panic_hook();
|
|
|
|
|
2021-09-02 08:25:22 +00:00
|
|
|
let engine_state = create_default_context();
|
2021-07-17 06:31:34 +00:00
|
|
|
|
2021-07-22 06:04:50 +00:00
|
|
|
if let Some(path) = std::env::args().nth(1) {
|
2021-09-20 21:37:26 +00:00
|
|
|
let file = std::fs::read(&path).into_diagnostic()?;
|
2021-07-23 05:14:49 +00:00
|
|
|
|
2021-07-30 21:26:05 +00:00
|
|
|
let (block, delta) = {
|
2021-09-02 08:25:22 +00:00
|
|
|
let engine_state = engine_state.borrow();
|
|
|
|
let mut working_set = StateWorkingSet::new(&*engine_state);
|
2021-09-06 20:41:30 +00:00
|
|
|
let (output, err) = parse(&mut working_set, Some(&path), &file, false);
|
2021-07-30 21:26:05 +00:00
|
|
|
if let Some(err) = err {
|
2021-09-20 21:37:26 +00:00
|
|
|
report_error(&working_set, &err);
|
2021-08-10 05:08:10 +00:00
|
|
|
|
2021-07-30 21:26:05 +00:00
|
|
|
std::process::exit(1);
|
|
|
|
}
|
|
|
|
(output, working_set.render())
|
|
|
|
};
|
2021-07-16 06:24:46 +00:00
|
|
|
|
2021-09-02 08:25:22 +00:00
|
|
|
EngineState::merge_delta(&mut *engine_state.borrow_mut(), delta);
|
2021-07-03 03:35:15 +00:00
|
|
|
|
2021-09-02 18:21:37 +00:00
|
|
|
let state = EvaluationContext {
|
2021-09-02 08:25:22 +00:00
|
|
|
engine_state: engine_state.clone(),
|
2021-09-02 18:21:37 +00:00
|
|
|
stack: nu_protocol::engine::Stack::new(),
|
2021-07-30 20:02:16 +00:00
|
|
|
};
|
2021-07-16 01:10:22 +00:00
|
|
|
|
2021-09-03 02:15:01 +00:00
|
|
|
match eval_block(&state, &block, Value::nothing()) {
|
2021-07-30 20:02:16 +00:00
|
|
|
Ok(value) => {
|
2021-09-01 21:20:53 +00:00
|
|
|
println!("{}", value.into_string());
|
2021-07-30 20:02:16 +00:00
|
|
|
}
|
|
|
|
Err(err) => {
|
2021-09-02 08:25:22 +00:00
|
|
|
let engine_state = engine_state.borrow();
|
|
|
|
let working_set = StateWorkingSet::new(&*engine_state);
|
2021-08-10 05:08:10 +00:00
|
|
|
|
2021-09-20 21:37:26 +00:00
|
|
|
report_error(&working_set, &err);
|
2021-08-10 05:08:10 +00:00
|
|
|
|
2021-07-30 20:02:16 +00:00
|
|
|
std::process::exit(1);
|
|
|
|
}
|
|
|
|
}
|
2021-06-30 01:42:56 +00:00
|
|
|
|
|
|
|
Ok(())
|
|
|
|
} else {
|
2021-07-22 06:04:50 +00:00
|
|
|
use reedline::{DefaultPrompt, FileBackedHistory, Reedline, Signal};
|
|
|
|
|
2021-09-09 22:09:40 +00:00
|
|
|
let completer = NuCompleter::new(engine_state.clone());
|
2021-09-22 03:14:57 +00:00
|
|
|
let mut entry_num = 0;
|
2021-09-09 09:06:55 +00:00
|
|
|
|
2021-09-20 21:37:26 +00:00
|
|
|
let mut line_editor = Reedline::create()
|
|
|
|
.into_diagnostic()?
|
|
|
|
.with_history(Box::new(
|
|
|
|
FileBackedHistory::with_file(1000, "history.txt".into()).into_diagnostic()?,
|
|
|
|
))
|
|
|
|
.into_diagnostic()?
|
2021-07-22 19:50:59 +00:00
|
|
|
.with_highlighter(Box::new(NuHighlighter {
|
2021-09-02 08:25:22 +00:00
|
|
|
engine_state: engine_state.clone(),
|
2021-09-09 09:06:55 +00:00
|
|
|
}))
|
|
|
|
.with_completion_action_handler(Box::new(
|
|
|
|
DefaultCompletionActionHandler::default().with_completer(Box::new(completer)),
|
2021-09-22 05:29:53 +00:00
|
|
|
))
|
|
|
|
.with_validator(Box::new(NuValidator {
|
|
|
|
engine_state: engine_state.clone(),
|
|
|
|
}));
|
2021-07-22 06:04:50 +00:00
|
|
|
|
|
|
|
let prompt = DefaultPrompt::new(1);
|
2021-09-02 18:21:37 +00:00
|
|
|
let stack = nu_protocol::engine::Stack::new();
|
2021-07-22 06:04:50 +00:00
|
|
|
|
|
|
|
loop {
|
2021-09-22 03:14:57 +00:00
|
|
|
entry_num += 1;
|
|
|
|
|
2021-08-09 05:29:25 +00:00
|
|
|
let input = line_editor.read_line(&prompt);
|
2021-07-22 06:04:50 +00:00
|
|
|
match input {
|
2021-08-09 05:29:25 +00:00
|
|
|
Ok(Signal::Success(s)) => {
|
2021-07-22 06:04:50 +00:00
|
|
|
if s.trim() == "exit" {
|
|
|
|
break;
|
2021-09-02 22:58:15 +00:00
|
|
|
} else if s.trim() == "vars" {
|
|
|
|
engine_state.borrow().print_vars();
|
|
|
|
continue;
|
|
|
|
} else if s.trim() == "decls" {
|
|
|
|
engine_state.borrow().print_decls();
|
|
|
|
continue;
|
|
|
|
} else if s.trim() == "blocks" {
|
|
|
|
engine_state.borrow().print_blocks();
|
|
|
|
continue;
|
|
|
|
} else if s.trim() == "stack" {
|
|
|
|
stack.print_stack();
|
|
|
|
continue;
|
2021-09-25 16:28:15 +00:00
|
|
|
} else if s.trim() == "contents" {
|
|
|
|
engine_state.borrow().print_contents();
|
|
|
|
continue;
|
2021-07-22 06:04:50 +00:00
|
|
|
}
|
2021-07-22 07:33:38 +00:00
|
|
|
|
2021-07-23 05:14:49 +00:00
|
|
|
let (block, delta) = {
|
2021-09-02 08:25:22 +00:00
|
|
|
let engine_state = engine_state.borrow();
|
|
|
|
let mut working_set = StateWorkingSet::new(&*engine_state);
|
2021-09-22 03:14:57 +00:00
|
|
|
let (output, err) = parse(
|
|
|
|
&mut working_set,
|
|
|
|
Some(&format!("entry #{}", entry_num)),
|
|
|
|
s.as_bytes(),
|
|
|
|
false,
|
|
|
|
);
|
2021-07-23 05:14:49 +00:00
|
|
|
if let Some(err) = err {
|
2021-09-20 21:37:26 +00:00
|
|
|
report_error(&working_set, &err);
|
2021-07-31 05:20:40 +00:00
|
|
|
continue;
|
2021-07-23 05:14:49 +00:00
|
|
|
}
|
|
|
|
(output, working_set.render())
|
2021-07-22 07:33:38 +00:00
|
|
|
};
|
|
|
|
|
2021-09-02 08:25:22 +00:00
|
|
|
EngineState::merge_delta(&mut *engine_state.borrow_mut(), delta);
|
2021-07-23 05:14:49 +00:00
|
|
|
|
2021-09-02 18:21:37 +00:00
|
|
|
let state = nu_protocol::engine::EvaluationContext {
|
2021-09-02 08:25:22 +00:00
|
|
|
engine_state: engine_state.clone(),
|
2021-08-15 22:33:34 +00:00
|
|
|
stack: stack.clone(),
|
2021-07-23 05:14:49 +00:00
|
|
|
};
|
|
|
|
|
2021-09-03 02:15:01 +00:00
|
|
|
match eval_block(&state, &block, Value::nothing()) {
|
2021-09-25 15:58:50 +00:00
|
|
|
Ok(value) => print_value(value, &state)?,
|
2021-07-30 20:02:16 +00:00
|
|
|
Err(err) => {
|
2021-09-02 08:25:22 +00:00
|
|
|
let engine_state = engine_state.borrow();
|
|
|
|
let working_set = StateWorkingSet::new(&*engine_state);
|
2021-08-10 05:08:10 +00:00
|
|
|
|
2021-09-20 21:37:26 +00:00
|
|
|
report_error(&working_set, &err);
|
2021-07-30 20:02:16 +00:00
|
|
|
}
|
|
|
|
}
|
2021-07-22 06:04:50 +00:00
|
|
|
}
|
2021-08-09 05:29:25 +00:00
|
|
|
Ok(Signal::CtrlC) => {
|
2021-07-22 06:04:50 +00:00
|
|
|
println!("Ctrl-c");
|
|
|
|
}
|
2021-08-09 05:29:25 +00:00
|
|
|
Ok(Signal::CtrlD) => {
|
2021-07-22 06:04:50 +00:00
|
|
|
break;
|
|
|
|
}
|
2021-08-09 05:29:25 +00:00
|
|
|
Ok(Signal::CtrlL) => {
|
2021-09-20 21:37:26 +00:00
|
|
|
line_editor.clear_screen().into_diagnostic()?;
|
2021-07-22 06:04:50 +00:00
|
|
|
}
|
2021-08-09 05:29:25 +00:00
|
|
|
Err(err) => {
|
2021-09-07 07:41:52 +00:00
|
|
|
let message = err.to_string();
|
|
|
|
if !message.contains("duration") {
|
|
|
|
println!("Error: {:?}", err);
|
|
|
|
}
|
2021-08-09 05:29:25 +00:00
|
|
|
}
|
2021-07-22 06:04:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-30 01:42:56 +00:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
2021-09-25 15:58:50 +00:00
|
|
|
|
|
|
|
fn print_value(value: Value, state: &EvaluationContext) -> Result<(), ShellError> {
|
|
|
|
// 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 engine_state = state.engine_state.borrow();
|
|
|
|
let output = match engine_state.find_decl("table".as_bytes()) {
|
|
|
|
Some(decl_id) => {
|
|
|
|
let table = engine_state
|
|
|
|
.get_decl(decl_id)
|
2021-09-25 16:03:25 +00:00
|
|
|
.run(state, &Call::new(), value)?;
|
2021-09-25 15:58:50 +00:00
|
|
|
table.into_string()
|
|
|
|
}
|
|
|
|
None => value.into_string(),
|
|
|
|
};
|
|
|
|
let stdout = std::io::stdout();
|
|
|
|
|
|
|
|
match stdout.lock().write_all(output.as_bytes()) {
|
|
|
|
Ok(_) => (),
|
|
|
|
Err(err) => eprintln!("{}", err),
|
|
|
|
};
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|