2023-02-24 21:39:52 +01:00
|
|
|
use std::io::{self, BufRead, Read, Write};
|
2022-02-02 21:59:01 +01:00
|
|
|
|
2022-07-29 22:42:00 +02:00
|
|
|
use nu_cli::{eval_env_change_hook, eval_hook};
|
|
|
|
use nu_command::create_default_context;
|
|
|
|
use nu_engine::eval_block;
|
|
|
|
use nu_parser::parse;
|
|
|
|
use nu_protocol::engine::{EngineState, Stack, StateWorkingSet};
|
2022-12-24 14:41:57 +01:00
|
|
|
use nu_protocol::{CliError, PipelineData, Value};
|
2022-07-29 22:42:00 +02:00
|
|
|
// use nu_test_support::fs::in_directory;
|
|
|
|
|
2022-02-02 21:59:01 +01:00
|
|
|
/// Echo's value of env keys from args
|
|
|
|
/// Example: nu --testbin env_echo FOO BAR
|
|
|
|
/// If it it's not present echo's nothing
|
2022-10-20 14:56:44 +02:00
|
|
|
pub fn echo_env(to_stdout: bool) {
|
2022-02-02 21:59:01 +01:00
|
|
|
let args = args();
|
|
|
|
for arg in args {
|
|
|
|
if let Ok(v) = std::env::var(arg) {
|
2022-10-20 14:56:44 +02:00
|
|
|
if to_stdout {
|
2023-01-30 02:37:54 +01:00
|
|
|
println!("{v}");
|
2022-10-20 14:56:44 +02:00
|
|
|
} else {
|
2023-01-30 02:37:54 +01:00
|
|
|
eprintln!("{v}");
|
2022-10-20 14:56:44 +02:00
|
|
|
}
|
2022-02-02 21:59:01 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn cococo() {
|
|
|
|
let args: Vec<String> = args();
|
|
|
|
|
|
|
|
if args.len() > 1 {
|
|
|
|
// Write back out all the arguments passed
|
|
|
|
// if given at least 1 instead of chickens
|
|
|
|
// speaking co co co.
|
|
|
|
println!("{}", &args[1..].join(" "));
|
|
|
|
} else {
|
|
|
|
println!("cococo");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn meow() {
|
|
|
|
let args: Vec<String> = args();
|
|
|
|
|
|
|
|
for arg in args.iter().skip(1) {
|
|
|
|
let contents = std::fs::read_to_string(arg).expect("Expected a filepath");
|
2023-01-30 02:37:54 +01:00
|
|
|
println!("{contents}");
|
2022-02-02 21:59:01 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-27 04:35:59 +02:00
|
|
|
// A binary version of meow
|
|
|
|
pub fn meowb() {
|
|
|
|
let args: Vec<String> = args();
|
|
|
|
|
|
|
|
let stdout = io::stdout();
|
|
|
|
let mut handle = stdout.lock();
|
|
|
|
|
|
|
|
for arg in args.iter().skip(1) {
|
|
|
|
let buf = std::fs::read(arg).expect("Expected a filepath");
|
|
|
|
handle.write_all(&buf).expect("failed to write to stdout");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Relays anything received on stdin to stdout
|
|
|
|
pub fn relay() {
|
|
|
|
io::copy(&mut io::stdin().lock(), &mut io::stdout().lock())
|
|
|
|
.expect("failed to copy stdin to stdout");
|
|
|
|
}
|
|
|
|
|
2022-02-02 21:59:01 +01:00
|
|
|
pub fn nonu() {
|
2023-01-30 02:37:54 +01:00
|
|
|
args().iter().skip(1).for_each(|arg| print!("{arg}"));
|
2022-02-02 21:59:01 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn repeater() {
|
|
|
|
let mut stdout = io::stdout();
|
|
|
|
let args = args();
|
|
|
|
let mut args = args.iter().skip(1);
|
|
|
|
let letter = args.next().expect("needs a character to iterate");
|
|
|
|
let count = args.next().expect("need the number of times to iterate");
|
|
|
|
|
|
|
|
let count: u64 = count.parse().expect("can't convert count to number");
|
|
|
|
|
|
|
|
for _ in 0..count {
|
2023-01-30 02:37:54 +01:00
|
|
|
let _ = write!(stdout, "{letter}");
|
2022-02-02 21:59:01 +01:00
|
|
|
}
|
|
|
|
let _ = stdout.flush();
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn iecho() {
|
|
|
|
// println! panics if stdout gets closed, whereas writeln gives us an error
|
|
|
|
let mut stdout = io::stdout();
|
|
|
|
let _ = args()
|
|
|
|
.iter()
|
|
|
|
.skip(1)
|
|
|
|
.cycle()
|
2023-01-30 02:37:54 +01:00
|
|
|
.try_for_each(|v| writeln!(stdout, "{v}"));
|
2022-02-02 21:59:01 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn fail() {
|
|
|
|
std::process::exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn chop() {
|
|
|
|
if did_chop_arguments() {
|
|
|
|
// we are done and don't care about standard input.
|
|
|
|
std::process::exit(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
// if no arguments given, chop from standard input and exit.
|
|
|
|
let stdin = io::stdin();
|
|
|
|
let mut stdout = io::stdout();
|
|
|
|
|
|
|
|
for given in stdin.lock().lines().flatten() {
|
|
|
|
let chopped = if given.is_empty() {
|
|
|
|
&given
|
|
|
|
} else {
|
|
|
|
let to = given.len() - 1;
|
|
|
|
&given[..to]
|
|
|
|
};
|
|
|
|
|
2023-01-30 02:37:54 +01:00
|
|
|
if let Err(_e) = writeln!(stdout, "{chopped}") {
|
2022-02-02 21:59:01 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
std::process::exit(0);
|
|
|
|
}
|
|
|
|
|
2022-07-29 22:42:00 +02:00
|
|
|
fn outcome_err(
|
|
|
|
engine_state: &EngineState,
|
|
|
|
error: &(dyn miette::Diagnostic + Send + Sync + 'static),
|
|
|
|
) -> ! {
|
|
|
|
let working_set = StateWorkingSet::new(engine_state);
|
|
|
|
|
|
|
|
eprintln!("Error: {:?}", CliError(error, &working_set));
|
|
|
|
|
|
|
|
std::process::exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
fn outcome_ok(msg: String) -> ! {
|
2023-01-30 02:37:54 +01:00
|
|
|
println!("{msg}");
|
2022-07-29 22:42:00 +02:00
|
|
|
|
|
|
|
std::process::exit(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn nu_repl() {
|
|
|
|
//cwd: &str, source_lines: &[&str]) {
|
|
|
|
let cwd = std::env::current_dir().expect("Could not get current working directory.");
|
|
|
|
let source_lines = args();
|
|
|
|
|
|
|
|
let mut engine_state = create_default_context();
|
|
|
|
let mut stack = Stack::new();
|
|
|
|
|
2022-12-24 14:41:57 +01:00
|
|
|
stack.add_env_var("PWD".to_string(), Value::test_string(cwd.to_string_lossy()));
|
2022-07-29 22:42:00 +02:00
|
|
|
|
|
|
|
let mut last_output = String::new();
|
|
|
|
|
|
|
|
for (i, line) in source_lines.iter().enumerate() {
|
2023-01-24 12:23:42 +01:00
|
|
|
let cwd = nu_engine::env::current_dir(&engine_state, &stack)
|
|
|
|
.unwrap_or_else(|err| outcome_err(&engine_state, &err));
|
2022-07-29 22:42:00 +02:00
|
|
|
|
|
|
|
// Before doing anything, merge the environment from the previous REPL iteration into the
|
|
|
|
// permanent state.
|
|
|
|
if let Err(err) = engine_state.merge_env(&mut stack, &cwd) {
|
|
|
|
outcome_err(&engine_state, &err);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check for pre_prompt hook
|
|
|
|
let config = engine_state.get_config();
|
|
|
|
if let Some(hook) = config.hooks.pre_prompt.clone() {
|
2022-11-06 01:46:40 +01:00
|
|
|
if let Err(err) = eval_hook(&mut engine_state, &mut stack, None, vec![], &hook) {
|
2022-07-29 22:42:00 +02:00
|
|
|
outcome_err(&engine_state, &err);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check for env change hook
|
|
|
|
let config = engine_state.get_config();
|
|
|
|
if let Err(err) = eval_env_change_hook(
|
|
|
|
config.hooks.env_change.clone(),
|
|
|
|
&mut engine_state,
|
|
|
|
&mut stack,
|
|
|
|
) {
|
|
|
|
outcome_err(&engine_state, &err);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check for pre_execution hook
|
|
|
|
let config = engine_state.get_config();
|
|
|
|
if let Some(hook) = config.hooks.pre_execution.clone() {
|
2022-11-06 01:46:40 +01:00
|
|
|
if let Err(err) = eval_hook(&mut engine_state, &mut stack, None, vec![], &hook) {
|
2022-07-29 22:42:00 +02:00
|
|
|
outcome_err(&engine_state, &err);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Eval the REPL line
|
|
|
|
let (block, delta) = {
|
|
|
|
let mut working_set = StateWorkingSet::new(&engine_state);
|
|
|
|
let (block, err) = parse(
|
|
|
|
&mut working_set,
|
2023-01-30 02:37:54 +01:00
|
|
|
Some(&format!("line{i}")),
|
2022-07-29 22:42:00 +02:00
|
|
|
line.as_bytes(),
|
|
|
|
false,
|
|
|
|
&[],
|
|
|
|
);
|
|
|
|
|
|
|
|
if let Some(err) = err {
|
|
|
|
outcome_err(&engine_state, &err);
|
|
|
|
}
|
|
|
|
(block, working_set.render())
|
|
|
|
};
|
|
|
|
|
|
|
|
if let Err(err) = engine_state.merge_delta(delta) {
|
|
|
|
outcome_err(&engine_state, &err);
|
|
|
|
}
|
|
|
|
|
2022-12-07 19:31:57 +01:00
|
|
|
let input = PipelineData::empty();
|
2022-07-29 22:42:00 +02:00
|
|
|
let config = engine_state.get_config();
|
|
|
|
|
|
|
|
match eval_block(&engine_state, &mut stack, &block, input, false, false) {
|
|
|
|
Ok(pipeline_data) => match pipeline_data.collect_string("", config) {
|
|
|
|
Ok(s) => last_output = s,
|
|
|
|
Err(err) => outcome_err(&engine_state, &err),
|
|
|
|
},
|
|
|
|
Err(err) => outcome_err(&engine_state, &err),
|
|
|
|
}
|
|
|
|
|
|
|
|
if let Some(cwd) = stack.get_env_var(&engine_state, "PWD") {
|
2023-01-24 12:23:42 +01:00
|
|
|
let path = cwd
|
|
|
|
.as_string()
|
|
|
|
.unwrap_or_else(|err| outcome_err(&engine_state, &err));
|
2022-07-29 22:42:00 +02:00
|
|
|
let _ = std::env::set_current_dir(path);
|
|
|
|
engine_state.add_env_var("PWD".into(), cwd);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
outcome_ok(last_output)
|
|
|
|
}
|
|
|
|
|
2022-02-02 21:59:01 +01:00
|
|
|
fn did_chop_arguments() -> bool {
|
|
|
|
let args: Vec<String> = args();
|
|
|
|
|
|
|
|
if args.len() > 1 {
|
|
|
|
let mut arguments = args.iter();
|
|
|
|
arguments.next();
|
|
|
|
|
|
|
|
for arg in arguments {
|
|
|
|
let chopped = if arg.is_empty() {
|
2022-02-24 20:02:28 +01:00
|
|
|
arg
|
2022-02-02 21:59:01 +01:00
|
|
|
} else {
|
|
|
|
let to = arg.len() - 1;
|
|
|
|
&arg[..to]
|
|
|
|
};
|
|
|
|
|
2023-01-30 02:37:54 +01:00
|
|
|
println!("{chopped}");
|
2022-02-02 21:59:01 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
false
|
|
|
|
}
|
|
|
|
|
2023-02-24 21:39:52 +01:00
|
|
|
pub fn input_bytes_length() {
|
|
|
|
let stdin = io::stdin();
|
|
|
|
let count = stdin.lock().bytes().count();
|
|
|
|
|
|
|
|
println!("{}", count);
|
|
|
|
}
|
|
|
|
|
2022-02-02 21:59:01 +01:00
|
|
|
fn args() -> Vec<String> {
|
|
|
|
// skip (--testbin bin_name args)
|
|
|
|
std::env::args().skip(2).collect()
|
|
|
|
}
|