1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135
use crate::prelude::*; use indexmap::{indexmap, IndexMap}; use nu_engine::WholeStreamCommand; use nu_errors::ShellError; use nu_protocol::{ReturnSuccess, Signature, SyntaxShape, UntaggedValue, Value}; use nu_source::Tagged; const DEFAULT_COLUMN_NAME: &str = "Column"; pub struct Wrap; impl WholeStreamCommand for Wrap { fn name(&self) -> &str { "wrap" } fn signature(&self) -> Signature { Signature::build("wrap").optional( "column", SyntaxShape::String, "the name of the new column", ) } fn usage(&self) -> &str { "Wraps the given data in a table." } fn run_with_actions(&self, args: CommandArgs) -> Result<ActionStream, ShellError> { wrap(args) } fn examples(&self) -> Vec<Example> { vec![ Example { description: "Wrap a list into a table with the default column name", example: "echo [1 2 3] | wrap", result: Some(vec![ UntaggedValue::row(indexmap! { DEFAULT_COLUMN_NAME.to_string() => UntaggedValue::int(1).into(), }) .into(), UntaggedValue::row(indexmap! { DEFAULT_COLUMN_NAME.to_string() => UntaggedValue::int(2).into(), }) .into(), UntaggedValue::row(indexmap! { DEFAULT_COLUMN_NAME.to_string() => UntaggedValue::int(3).into(), }) .into(), ]), }, Example { description: "Wrap a list into a table with a given column name", example: "echo [1 2 3] | wrap MyColumn", result: Some(vec![ UntaggedValue::row(indexmap! { "MyColumn".to_string() => UntaggedValue::int(1).into(), }) .into(), UntaggedValue::row(indexmap! { "MyColumn".to_string() => UntaggedValue::int(2).into(), }) .into(), UntaggedValue::row(indexmap! { "MyColumn".to_string() => UntaggedValue::int(3).into(), }) .into(), ]), }, ] } } fn wrap(args: CommandArgs) -> Result<ActionStream, ShellError> { let column: Option<Tagged<String>> = args.opt(0)?; let mut result_table = vec![]; let mut are_all_rows = true; for value in args.input { match value { Value { value: UntaggedValue::Row(_), .. } => { result_table.push(value); } _ => { are_all_rows = false; let mut index_map = IndexMap::new(); index_map.insert( match &column { Some(key) => key.item.clone(), None => DEFAULT_COLUMN_NAME.to_string(), }, value, ); result_table.push(UntaggedValue::row(index_map).into_value(Tag::unknown())); } } } if are_all_rows { let mut index_map = IndexMap::new(); index_map.insert( match &column { Some(key) => key.item.clone(), None => DEFAULT_COLUMN_NAME.to_string(), }, UntaggedValue::table(&result_table).into_value(Tag::unknown()), ); let row = UntaggedValue::row(index_map).into_untagged_value(); Ok(ActionStream::one(ReturnSuccess::value(row))) } else { Ok((result_table.into_iter().map(ReturnSuccess::value)).into_action_stream()) } } #[cfg(test)] mod tests { use super::ShellError; use super::Wrap; #[test] fn examples_work_as_expected() -> Result<(), ShellError> { use crate::examples::test as test_examples; test_examples(Wrap {}) } }