2021-08-10 18:51:08 +00:00
|
|
|
use nu_cli::{create_default_context, report_parsing_error, report_shell_error, NuHighlighter};
|
|
|
|
use nu_engine::eval_block;
|
|
|
|
use nu_parser::{ParserState, ParserWorkingSet};
|
2021-06-30 01:42:56 +00:00
|
|
|
|
|
|
|
fn main() -> std::io::Result<()> {
|
2021-08-10 18:51:08 +00:00
|
|
|
let parser_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-07-23 05:14:49 +00:00
|
|
|
let file = std::fs::read(&path)?;
|
|
|
|
|
2021-07-30 21:26:05 +00:00
|
|
|
let (block, delta) = {
|
|
|
|
let parser_state = parser_state.borrow();
|
|
|
|
let mut working_set = ParserWorkingSet::new(&*parser_state);
|
|
|
|
let (output, err) = working_set.parse_file(&path, &file, false);
|
|
|
|
if let Some(err) = err {
|
2021-08-10 05:08:10 +00:00
|
|
|
let _ = report_parsing_error(&working_set, &err);
|
|
|
|
|
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-07-30 21:26:05 +00:00
|
|
|
ParserState::merge_delta(&mut *parser_state.borrow_mut(), delta);
|
2021-07-03 03:35:15 +00:00
|
|
|
|
2021-08-10 18:51:08 +00:00
|
|
|
let state = nu_engine::State {
|
2021-07-30 21:26:05 +00:00
|
|
|
parser_state: &*parser_state.borrow(),
|
2021-07-30 20:02:16 +00:00
|
|
|
};
|
2021-07-16 01:10:22 +00:00
|
|
|
|
2021-08-10 18:51:08 +00:00
|
|
|
let stack = nu_engine::Stack::new();
|
2021-07-03 03:35:15 +00:00
|
|
|
|
2021-07-30 20:02:16 +00:00
|
|
|
match eval_block(&state, stack, &block) {
|
|
|
|
Ok(value) => {
|
|
|
|
println!("{}", value);
|
|
|
|
}
|
|
|
|
Err(err) => {
|
2021-08-10 05:08:10 +00:00
|
|
|
let parser_state = parser_state.borrow();
|
|
|
|
let working_set = ParserWorkingSet::new(&*parser_state);
|
|
|
|
|
2021-08-10 18:51:08 +00:00
|
|
|
let _ = report_shell_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-07-22 19:50:59 +00:00
|
|
|
let mut line_editor = Reedline::new()
|
2021-07-22 20:45:23 +00:00
|
|
|
.with_history(Box::new(FileBackedHistory::with_file(
|
|
|
|
1000,
|
|
|
|
"history.txt".into(),
|
|
|
|
)?))?
|
2021-07-22 19:50:59 +00:00
|
|
|
.with_highlighter(Box::new(NuHighlighter {
|
|
|
|
parser_state: parser_state.clone(),
|
|
|
|
}));
|
2021-07-22 06:04:50 +00:00
|
|
|
|
|
|
|
let prompt = DefaultPrompt::new(1);
|
|
|
|
let mut current_line = 1;
|
2021-08-10 18:51:08 +00:00
|
|
|
let stack = nu_engine::Stack::new();
|
2021-07-22 06:04:50 +00:00
|
|
|
|
|
|
|
loop {
|
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-07-23 05:14:49 +00:00
|
|
|
// println!("input: '{}'", s);
|
2021-07-22 07:33:38 +00:00
|
|
|
|
2021-07-23 05:14:49 +00:00
|
|
|
let (block, delta) = {
|
2021-07-22 07:33:38 +00:00
|
|
|
let parser_state = parser_state.borrow();
|
|
|
|
let mut working_set = ParserWorkingSet::new(&*parser_state);
|
|
|
|
let (output, err) = working_set.parse_file(
|
|
|
|
&format!("line_{}", current_line),
|
|
|
|
s.as_bytes(),
|
|
|
|
false,
|
|
|
|
);
|
2021-07-23 05:14:49 +00:00
|
|
|
if let Some(err) = err {
|
2021-08-10 05:08:10 +00:00
|
|
|
let _ = report_parsing_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
|
|
|
};
|
|
|
|
|
|
|
|
ParserState::merge_delta(&mut *parser_state.borrow_mut(), delta);
|
2021-07-23 05:14:49 +00:00
|
|
|
|
2021-08-10 18:51:08 +00:00
|
|
|
let state = nu_engine::State {
|
2021-07-23 05:14:49 +00:00
|
|
|
parser_state: &*parser_state.borrow(),
|
|
|
|
};
|
|
|
|
|
2021-07-30 20:02:16 +00:00
|
|
|
match eval_block(&state, stack.clone(), &block) {
|
|
|
|
Ok(value) => {
|
|
|
|
println!("{}", value);
|
|
|
|
}
|
|
|
|
Err(err) => {
|
2021-08-10 05:08:10 +00:00
|
|
|
let parser_state = parser_state.borrow();
|
|
|
|
let working_set = ParserWorkingSet::new(&*parser_state);
|
|
|
|
|
|
|
|
let _ = report_shell_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-07-22 06:04:50 +00:00
|
|
|
line_editor.clear_screen()?;
|
|
|
|
}
|
2021-08-09 05:29:25 +00:00
|
|
|
Err(err) => {
|
|
|
|
println!("Error: {:?}", err);
|
|
|
|
}
|
2021-07-22 06:04:50 +00:00
|
|
|
}
|
|
|
|
current_line += 1;
|
|
|
|
}
|
|
|
|
|
2021-06-30 01:42:56 +00:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|