mirror of
https://github.com/nushell/nushell.git
synced 2024-11-16 05:23:44 +01:00
commit
0694245ccd
7
Cargo.lock
generated
7
Cargo.lock
generated
@ -164,6 +164,12 @@ dependencies = [
|
||||
"wasi",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "glob"
|
||||
version = "0.3.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "9b919933a397b79c37e33b77bb2aa3dc8eb6e165ad809e58ff75bc7db2e34574"
|
||||
|
||||
[[package]]
|
||||
name = "instant"
|
||||
version = "0.1.10"
|
||||
@ -276,6 +282,7 @@ dependencies = [
|
||||
name = "nu-command"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"glob",
|
||||
"nu-engine",
|
||||
"nu-protocol",
|
||||
]
|
||||
|
@ -7,4 +7,7 @@ edition = "2018"
|
||||
|
||||
[dependencies]
|
||||
nu-protocol = { path = "../nu-protocol" }
|
||||
nu-engine = { path = "../nu-engine" }
|
||||
nu-engine = { path = "../nu-engine" }
|
||||
|
||||
# Potential dependencies for extras
|
||||
glob = "0.3.0"
|
@ -6,7 +6,7 @@ use nu_protocol::{
|
||||
};
|
||||
|
||||
use crate::{
|
||||
where_::Where, Alias, Benchmark, BuildString, Def, Do, Each, For, If, Length, Let, LetEnv,
|
||||
where_::Where, Alias, Benchmark, BuildString, Def, Do, Each, For, If, Length, Let, LetEnv, Ls,
|
||||
};
|
||||
|
||||
pub fn create_default_context() -> Rc<RefCell<EngineState>> {
|
||||
@ -43,6 +43,8 @@ pub fn create_default_context() -> Rc<RefCell<EngineState>> {
|
||||
|
||||
working_set.add_decl(Box::new(Length));
|
||||
|
||||
working_set.add_decl(Box::new(Ls));
|
||||
|
||||
let sig = Signature::build("exit");
|
||||
working_set.add_decl(sig.predeclare());
|
||||
let sig = Signature::build("vars");
|
||||
|
@ -10,6 +10,7 @@ mod if_;
|
||||
mod length;
|
||||
mod let_;
|
||||
mod let_env;
|
||||
mod ls;
|
||||
mod where_;
|
||||
|
||||
pub use alias::Alias;
|
||||
@ -24,3 +25,4 @@ pub use if_::If;
|
||||
pub use length::Length;
|
||||
pub use let_::Let;
|
||||
pub use let_env::LetEnv;
|
||||
pub use ls::Ls;
|
||||
|
93
crates/nu-command/src/ls.rs
Normal file
93
crates/nu-command/src/ls.rs
Normal file
@ -0,0 +1,93 @@
|
||||
use nu_engine::eval_expression;
|
||||
use nu_protocol::ast::Call;
|
||||
use nu_protocol::engine::{Command, EvaluationContext};
|
||||
use nu_protocol::{IntoValueStream, Signature, SyntaxShape, Value};
|
||||
|
||||
pub struct Ls;
|
||||
|
||||
//NOTE: this is not a real implementation :D. It's just a simple one to test with until we port the real one.
|
||||
impl Command for Ls {
|
||||
fn name(&self) -> &str {
|
||||
"ls"
|
||||
}
|
||||
|
||||
fn usage(&self) -> &str {
|
||||
"List the files in a directory."
|
||||
}
|
||||
|
||||
fn signature(&self) -> nu_protocol::Signature {
|
||||
Signature::build("ls").optional(
|
||||
"pattern",
|
||||
SyntaxShape::GlobPattern,
|
||||
"the glob pattern to use",
|
||||
)
|
||||
}
|
||||
|
||||
fn run(
|
||||
&self,
|
||||
context: &EvaluationContext,
|
||||
call: &Call,
|
||||
_input: Value,
|
||||
) -> Result<nu_protocol::Value, nu_protocol::ShellError> {
|
||||
let pattern = if let Some(expr) = call.positional.get(0) {
|
||||
let result = eval_expression(context, expr)?;
|
||||
result.as_string()?
|
||||
} else {
|
||||
"*".into()
|
||||
};
|
||||
|
||||
let call_span = call.head;
|
||||
let glob = glob::glob(&pattern).unwrap();
|
||||
|
||||
Ok(Value::Stream {
|
||||
stream: glob
|
||||
.into_iter()
|
||||
.map(move |x| match x {
|
||||
Ok(path) => match std::fs::symlink_metadata(&path) {
|
||||
Ok(metadata) => {
|
||||
let is_file = metadata.is_file();
|
||||
let is_dir = metadata.is_dir();
|
||||
let filesize = metadata.len();
|
||||
|
||||
Value::Record {
|
||||
cols: vec!["name".into(), "type".into(), "size".into()],
|
||||
vals: vec![
|
||||
Value::String {
|
||||
val: path.to_string_lossy().to_string(),
|
||||
span: call_span,
|
||||
},
|
||||
if is_file {
|
||||
Value::string("file", call_span)
|
||||
} else if is_dir {
|
||||
Value::string("dir", call_span)
|
||||
} else {
|
||||
Value::Nothing { span: call_span }
|
||||
},
|
||||
Value::Int {
|
||||
val: filesize as i64,
|
||||
span: call_span,
|
||||
},
|
||||
],
|
||||
span: call_span,
|
||||
}
|
||||
}
|
||||
Err(_) => Value::Record {
|
||||
cols: vec!["name".into(), "type".into(), "size".into()],
|
||||
vals: vec![
|
||||
Value::String {
|
||||
val: path.to_string_lossy().to_string(),
|
||||
span: call_span,
|
||||
},
|
||||
Value::Nothing { span: call_span },
|
||||
Value::Nothing { span: call_span },
|
||||
],
|
||||
span: call_span,
|
||||
},
|
||||
},
|
||||
_ => Value::Nothing { span: call_span },
|
||||
})
|
||||
.into_value_stream(),
|
||||
span: call_span,
|
||||
})
|
||||
}
|
||||
}
|
@ -278,6 +278,13 @@ impl Value {
|
||||
Ok(current)
|
||||
}
|
||||
|
||||
pub fn string(s: &str, span: Span) -> Value {
|
||||
Value::String {
|
||||
val: s.into(),
|
||||
span,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn is_true(&self) -> bool {
|
||||
matches!(self, Value::Bool { val: true, .. })
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user