mirror of
https://github.com/nushell/nushell.git
synced 2025-08-10 05:38:19 +02:00
nu-cli refactor moving commands into their own crate nu-command (#2910)
* move commands, futures.rs, script.rs, utils * move over maybe_print_errors * add nu_command crate references to nu_cli * in commands.rs open up to pub mod from pub(crate) * nu-cli, nu-command, and nu tests are now passing * cargo fmt * clean up nu-cli/src/prelude.rs * code cleanup * for some reason lex.rs was not formatted, may be causing my error * remove mod completion from lib.rs which was not being used along with quickcheck macros * add in allow unused imports * comment out one failing external test; comment out one failing internal test * revert commenting out failing tests; something else might be going on; someone with a windows machine should check and see what is going on with these failing windows tests * Update Cargo.toml Extend the optional features to nu-command Co-authored-by: Jonathan Turner <jonathandturner@users.noreply.github.com>
This commit is contained in:
21
crates/nu-command/src/utils/suggestions.rs
Normal file
21
crates/nu-command/src/utils/suggestions.rs
Normal file
@ -0,0 +1,21 @@
|
||||
use crate::did_you_mean;
|
||||
use nu_errors::ShellError;
|
||||
use nu_protocol::Value;
|
||||
use nu_source::Tagged;
|
||||
|
||||
pub fn suggestions(tried: Tagged<&str>, for_value: &Value) -> ShellError {
|
||||
let possibilities = did_you_mean(for_value, tried.to_string());
|
||||
|
||||
match possibilities {
|
||||
Some(p) => ShellError::labeled_error(
|
||||
"Unknown column",
|
||||
format!("did you mean '{}'?", p[0]),
|
||||
tried.tag(),
|
||||
),
|
||||
None => ShellError::labeled_error(
|
||||
"Unknown column",
|
||||
"row does not contain this column",
|
||||
tried.tag(),
|
||||
),
|
||||
}
|
||||
}
|
89
crates/nu-command/src/utils/test_bins.rs
Normal file
89
crates/nu-command/src/utils/test_bins.rs
Normal file
@ -0,0 +1,89 @@
|
||||
use std::io::{self, BufRead, Write};
|
||||
|
||||
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 nonu() {
|
||||
args().iter().skip(1).for_each(|arg| print!("{}", arg));
|
||||
}
|
||||
|
||||
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()
|
||||
.try_for_each(|v| writeln!(stdout, "{}", v));
|
||||
}
|
||||
|
||||
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 line in stdin.lock().lines() {
|
||||
if let Ok(given) = line {
|
||||
let chopped = if given.is_empty() {
|
||||
&given
|
||||
} else {
|
||||
let to = given.len() - 1;
|
||||
&given[..to]
|
||||
};
|
||||
|
||||
if let Err(_e) = writeln!(stdout, "{}", chopped) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
std::process::exit(0);
|
||||
}
|
||||
|
||||
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() {
|
||||
&arg
|
||||
} else {
|
||||
let to = arg.len() - 1;
|
||||
&arg[..to]
|
||||
};
|
||||
|
||||
println!("{}", chopped);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
false
|
||||
}
|
||||
|
||||
fn args() -> Vec<String> {
|
||||
// skip (--testbin bin_name args)
|
||||
std::env::args().skip(2).collect()
|
||||
}
|
Reference in New Issue
Block a user