2020-10-16 19:15:40 +02:00
|
|
|
use crate::prelude::*;
|
2021-02-03 14:19:38 +01:00
|
|
|
use nu_data::base::shape::InlineShape;
|
2021-01-10 03:50:49 +01:00
|
|
|
use nu_engine::WholeStreamCommand;
|
2021-02-03 14:19:38 +01:00
|
|
|
use nu_errors::ShellError;
|
2020-10-16 19:15:40 +02:00
|
|
|
use nu_protocol::{
|
2021-02-03 14:19:38 +01:00
|
|
|
ColumnPath, Primitive::Filesize, ReturnSuccess, Signature, SyntaxShape, UntaggedValue, Value,
|
2020-10-16 19:15:40 +02:00
|
|
|
};
|
|
|
|
use nu_source::Tagged;
|
|
|
|
use nu_value_ext::get_data_by_column_path;
|
|
|
|
|
|
|
|
pub struct FileSize;
|
|
|
|
|
|
|
|
#[derive(Deserialize)]
|
|
|
|
pub struct Arguments {
|
|
|
|
field: ColumnPath,
|
|
|
|
format: Tagged<String>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl WholeStreamCommand for FileSize {
|
|
|
|
fn name(&self) -> &str {
|
|
|
|
"format filesize"
|
|
|
|
}
|
|
|
|
|
|
|
|
fn signature(&self) -> Signature {
|
|
|
|
Signature::build("format filesize")
|
|
|
|
.required(
|
|
|
|
"field",
|
|
|
|
SyntaxShape::ColumnPath,
|
|
|
|
"the name of the column to update",
|
|
|
|
)
|
|
|
|
.required(
|
|
|
|
"format value",
|
|
|
|
SyntaxShape::String,
|
|
|
|
"the format into which convert the filesizes",
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn usage(&self) -> &str {
|
|
|
|
"Converts a column of filesizes to some specified format"
|
|
|
|
}
|
|
|
|
|
2021-04-06 18:19:43 +02:00
|
|
|
fn run(&self, args: CommandArgs) -> Result<OutputStream, ShellError> {
|
|
|
|
filesize(args)
|
2020-10-16 19:15:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
fn examples(&self) -> Vec<Example> {
|
|
|
|
vec![
|
|
|
|
Example {
|
|
|
|
description: "Convert the size row to KB",
|
|
|
|
example: "ls | format filesize size KB",
|
|
|
|
result: None,
|
|
|
|
},
|
|
|
|
Example {
|
|
|
|
description: "Convert the apparent row to B",
|
|
|
|
example: "du | format filesize apparent B",
|
|
|
|
result: None,
|
|
|
|
},
|
|
|
|
]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-06 18:19:43 +02:00
|
|
|
fn process_row(
|
2020-10-16 19:15:40 +02:00
|
|
|
input: Value,
|
|
|
|
format: Tagged<String>,
|
|
|
|
field: Arc<ColumnPath>,
|
|
|
|
) -> Result<OutputStream, ShellError> {
|
|
|
|
Ok({
|
|
|
|
let replace_for = get_data_by_column_path(&input, &field, move |_, _, error| error);
|
|
|
|
match replace_for {
|
2021-02-03 14:19:38 +01:00
|
|
|
Ok(s) => {
|
|
|
|
if let Value {
|
|
|
|
value: UntaggedValue::Primitive(Filesize(fs)),
|
|
|
|
..
|
|
|
|
} = s
|
|
|
|
{
|
|
|
|
let byte_format = InlineShape::format_bytes(&fs, Some(&format.item));
|
|
|
|
let byte_value = Value::from(byte_format.1);
|
|
|
|
OutputStream::one(ReturnSuccess::value(
|
|
|
|
input.replace_data_at_column_path(&field, byte_value).expect("Given that the existence check was already done, this shouldn't trigger never"),
|
|
|
|
))
|
|
|
|
} else {
|
|
|
|
return Err(ShellError::labeled_error(
|
|
|
|
"the data in this row is not of the type filesize",
|
|
|
|
"invalid datatype in row",
|
|
|
|
input.tag(),
|
|
|
|
));
|
|
|
|
}
|
|
|
|
}
|
2020-10-16 19:15:40 +02:00
|
|
|
Err(e) => OutputStream::one(Err(e)),
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-04-06 18:19:43 +02:00
|
|
|
fn filesize(raw_args: CommandArgs) -> Result<OutputStream, ShellError> {
|
|
|
|
let (Arguments { field, format }, input) = raw_args.process()?;
|
2020-10-16 19:15:40 +02:00
|
|
|
let field = Arc::new(field);
|
|
|
|
|
|
|
|
Ok(input
|
2021-04-06 18:19:43 +02:00
|
|
|
.map(move |input| {
|
2020-10-16 19:15:40 +02:00
|
|
|
let format = format.clone();
|
|
|
|
let field = field.clone();
|
|
|
|
|
2021-04-06 18:19:43 +02:00
|
|
|
match process_row(input, format, field) {
|
|
|
|
Ok(s) => s,
|
|
|
|
Err(e) => OutputStream::one(Err(e)),
|
2020-10-16 19:15:40 +02:00
|
|
|
}
|
|
|
|
})
|
|
|
|
.flatten()
|
|
|
|
.to_output_stream())
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::FileSize;
|
|
|
|
use super::ShellError;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn examples_work_as_expected() -> Result<(), ShellError> {
|
|
|
|
use crate::examples::test as test_examples;
|
|
|
|
|
2021-02-12 11:13:14 +01:00
|
|
|
test_examples(FileSize {})
|
2020-10-16 19:15:40 +02:00
|
|
|
}
|
|
|
|
}
|