2021-09-25 16:56:33 +02:00
|
|
|
use std::io::Write;
|
|
|
|
|
2021-09-20 23:37:26 +02:00
|
|
|
use miette::{IntoDiagnostic, Result};
|
2021-09-22 07:29:53 +02:00
|
|
|
use nu_cli::{report_error, NuCompleter, NuHighlighter, NuValidator};
|
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-09-03 04:15:01 +02:00
|
|
|
engine::{EngineState, EvaluationContext, StateWorkingSet},
|
2021-09-25 17:58:50 +02:00
|
|
|
ShellError, Value,
|
2021-09-03 04:15:01 +02:00
|
|
|
};
|
2021-09-10 00:09:40 +02:00
|
|
|
use reedline::DefaultCompletionActionHandler;
|
2021-06-30 03:42:56 +02:00
|
|
|
|
2021-08-10 20:57:08 +02:00
|
|
|
#[cfg(test)]
|
|
|
|
mod tests;
|
|
|
|
|
2021-09-20 23:37:26 +02:00
|
|
|
fn main() -> Result<()> {
|
2021-09-21 21:37:16 +02:00
|
|
|
miette::set_panic_hook();
|
|
|
|
|
2021-09-02 10:25:22 +02:00
|
|
|
let engine_state = create_default_context();
|
2021-07-17 08:31:34 +02:00
|
|
|
|
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-09-02 10:25:22 +02:00
|
|
|
let engine_state = engine_state.borrow();
|
|
|
|
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-09-02 10:25:22 +02:00
|
|
|
EngineState::merge_delta(&mut *engine_state.borrow_mut(), delta);
|
2021-07-03 05:35:15 +02:00
|
|
|
|
2021-09-02 20:21:37 +02:00
|
|
|
let state = EvaluationContext {
|
2021-09-02 10:25:22 +02:00
|
|
|
engine_state: engine_state.clone(),
|
2021-09-02 20:21:37 +02:00
|
|
|
stack: nu_protocol::engine::Stack::new(),
|
2021-07-30 22:02:16 +02:00
|
|
|
};
|
2021-07-16 03:10:22 +02:00
|
|
|
|
2021-09-03 04:15:01 +02:00
|
|
|
match eval_block(&state, &block, Value::nothing()) {
|
2021-07-30 22:02:16 +02:00
|
|
|
Ok(value) => {
|
2021-09-01 23:20:53 +02:00
|
|
|
println!("{}", value.into_string());
|
2021-07-30 22:02:16 +02:00
|
|
|
}
|
|
|
|
Err(err) => {
|
2021-09-02 10:25:22 +02:00
|
|
|
let engine_state = engine_state.borrow();
|
|
|
|
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-07-22 08:04:50 +02:00
|
|
|
use reedline::{DefaultPrompt, FileBackedHistory, Reedline, Signal};
|
|
|
|
|
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-09-20 23:37:26 +02: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 21:50:59 +02:00
|
|
|
.with_highlighter(Box::new(NuHighlighter {
|
2021-09-02 10:25:22 +02:00
|
|
|
engine_state: engine_state.clone(),
|
2021-09-09 11:06:55 +02:00
|
|
|
}))
|
|
|
|
.with_completion_action_handler(Box::new(
|
|
|
|
DefaultCompletionActionHandler::default().with_completer(Box::new(completer)),
|
2021-09-22 07:29:53 +02:00
|
|
|
))
|
|
|
|
.with_validator(Box::new(NuValidator {
|
|
|
|
engine_state: engine_state.clone(),
|
|
|
|
}));
|
2021-07-22 08:04:50 +02:00
|
|
|
|
|
|
|
let prompt = DefaultPrompt::new(1);
|
2021-09-02 20:21:37 +02:00
|
|
|
let stack = nu_protocol::engine::Stack::new();
|
2021-07-22 08:04:50 +02:00
|
|
|
|
|
|
|
loop {
|
2021-09-22 05:14:57 +02:00
|
|
|
entry_num += 1;
|
|
|
|
|
2021-08-09 07:29:25 +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-09-03 00:58:15 +02: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-07-22 08:04:50 +02:00
|
|
|
}
|
2021-07-22 09:33:38 +02:00
|
|
|
|
2021-07-23 07:14:49 +02:00
|
|
|
let (block, delta) = {
|
2021-09-02 10:25:22 +02:00
|
|
|
let engine_state = engine_state.borrow();
|
|
|
|
let mut working_set = StateWorkingSet::new(&*engine_state);
|
2021-09-22 05:14:57 +02:00
|
|
|
let (output, err) = parse(
|
|
|
|
&mut working_set,
|
|
|
|
Some(&format!("entry #{}", entry_num)),
|
|
|
|
s.as_bytes(),
|
|
|
|
false,
|
|
|
|
);
|
2021-07-23 07:14:49 +02:00
|
|
|
if let Some(err) = err {
|
2021-09-20 23:37:26 +02:00
|
|
|
report_error(&working_set, &err);
|
2021-07-31 07:20:40 +02:00
|
|
|
continue;
|
2021-07-23 07:14:49 +02:00
|
|
|
}
|
|
|
|
(output, working_set.render())
|
2021-07-22 09:33:38 +02:00
|
|
|
};
|
|
|
|
|
2021-09-02 10:25:22 +02:00
|
|
|
EngineState::merge_delta(&mut *engine_state.borrow_mut(), delta);
|
2021-07-23 07:14:49 +02:00
|
|
|
|
2021-09-02 20:21:37 +02:00
|
|
|
let state = nu_protocol::engine::EvaluationContext {
|
2021-09-02 10:25:22 +02:00
|
|
|
engine_state: engine_state.clone(),
|
2021-08-16 00:33:34 +02:00
|
|
|
stack: stack.clone(),
|
2021-07-23 07:14:49 +02:00
|
|
|
};
|
|
|
|
|
2021-09-03 04:15:01 +02:00
|
|
|
match eval_block(&state, &block, Value::nothing()) {
|
2021-09-25 17:58:50 +02:00
|
|
|
Ok(value) => print_value(value, &state)?,
|
2021-07-30 22:02:16 +02:00
|
|
|
Err(err) => {
|
2021-09-02 10:25:22 +02:00
|
|
|
let engine_state = engine_state.borrow();
|
|
|
|
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-07-30 22:02:16 +02:00
|
|
|
}
|
|
|
|
}
|
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
|
|
|
|
|
|
|
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)
|
|
|
|
.run(&state, &Call::new(), value)?;
|
|
|
|
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(())
|
|
|
|
}
|