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 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361
use nu_engine::CallExt;
use nu_protocol::{
ast::Call,
engine::{Command, EngineState, Stack},
Category, Example, IntoPipelineData, PipelineData, ShellError, Signature, Span, SyntaxShape,
Type, Value,
};
#[derive(Clone)]
pub struct Rotate;
impl Command for Rotate {
fn name(&self) -> &str {
"rotate"
}
fn signature(&self) -> Signature {
Signature::build("rotate")
.input_output_types(vec![
(Type::Record(vec![]), Type::Table(vec![])),
(Type::Table(vec![]), Type::Table(vec![])),
])
.switch("ccw", "rotate counter clockwise", None)
.rest(
"rest",
SyntaxShape::String,
"the names to give columns once rotated",
)
.category(Category::Filters)
}
fn usage(&self) -> &str {
"Rotates a table or record clockwise (default) or counter-clockwise (use --ccw flag)."
}
fn examples(&self) -> Vec<Example> {
vec![
Example {
description: "Rotate a record clockwise, producing a table (like `transpose` but with column order reversed)",
example: "{a:1, b:2} | rotate",
result: Some(Value::List {
vals: vec![
Value::Record {
cols: vec!["column0".to_string(), "column1".to_string()],
vals: vec![Value::test_int(1), Value::test_string("a")],
span: Span::test_data(),
},
Value::Record {
cols: vec!["column0".to_string(), "column1".to_string()],
vals: vec![Value::test_int(2), Value::test_string("b")],
span: Span::test_data(),
},
],
span: Span::test_data(),
}),
},
Example {
description: "Rotate 2x3 table clockwise",
example: "[[a b]; [1 2] [3 4] [5 6]] | rotate",
result: Some(Value::List {
vals: vec![
Value::Record {
cols: vec![
"column0".to_string(),
"column1".to_string(),
"column2".to_string(),
"column3".to_string(),
],
vals: vec![
Value::test_int(5),
Value::test_int(3),
Value::test_int(1),
Value::test_string("a"),
],
span: Span::test_data(),
},
Value::Record {
cols: vec![
"column0".to_string(),
"column1".to_string(),
"column2".to_string(),
"column3".to_string(),
],
vals: vec![
Value::test_int(6),
Value::test_int(4),
Value::test_int(2),
Value::test_string("b"),
],
span: Span::test_data(),
},
],
span: Span::test_data(),
}),
},
Example {
description: "Rotate table clockwise and change columns names",
example: "[[a b]; [1 2]] | rotate col_a col_b",
result: Some(Value::List {
vals: vec![
Value::Record {
cols: vec!["col_a".to_string(), "col_b".to_string()],
vals: vec![Value::test_int(1), Value::test_string("a")],
span: Span::test_data(),
},
Value::Record {
cols: vec!["col_a".to_string(), "col_b".to_string()],
vals: vec![Value::test_int(2), Value::test_string("b")],
span: Span::test_data(),
},
],
span: Span::test_data(),
}),
},
Example {
description: "Rotate table counter clockwise",
example: "[[a b]; [1 2]] | rotate --ccw",
result: Some(Value::List {
vals: vec![
Value::Record {
cols: vec!["column0".to_string(), "column1".to_string()],
vals: vec![Value::test_string("b"), Value::test_int(2)],
span: Span::test_data(),
},
Value::Record {
cols: vec!["column0".to_string(), "column1".to_string()],
vals: vec![Value::test_string("a"), Value::test_int(1)],
span: Span::test_data(),
},
],
span: Span::test_data(),
}),
},
Example {
description: "Rotate table counter-clockwise",
example: "[[a b]; [1 2] [3 4] [5 6]] | rotate --ccw",
result: Some(Value::List {
vals: vec![
Value::Record {
cols: vec![
"column0".to_string(),
"column1".to_string(),
"column2".to_string(),
"column3".to_string(),
],
vals: vec![
Value::test_string("b"),
Value::test_int(2),
Value::test_int(4),
Value::test_int(6),
],
span: Span::test_data(),
},
Value::Record {
cols: vec![
"column0".to_string(),
"column1".to_string(),
"column2".to_string(),
"column3".to_string(),
],
vals: vec![
Value::test_string("a"),
Value::test_int(1),
Value::test_int(3),
Value::test_int(5),
],
span: Span::test_data(),
},
],
span: Span::test_data(),
}),
},
Example {
description: "Rotate table counter-clockwise and change columns names",
example: "[[a b]; [1 2]] | rotate --ccw col_a col_b",
result: Some(Value::List {
vals: vec![
Value::Record {
cols: vec!["col_a".to_string(), "col_b".to_string()],
vals: vec![Value::test_string("b"), Value::test_int(2)],
span: Span::test_data(),
},
Value::Record {
cols: vec!["col_a".to_string(), "col_b".to_string()],
vals: vec![Value::test_string("a"), Value::test_int(1)],
span: Span::test_data(),
},
],
span: Span::test_data(),
}),
},
]
}
fn run(
&self,
engine_state: &EngineState,
stack: &mut Stack,
call: &Call,
input: PipelineData,
) -> Result<PipelineData, ShellError> {
rotate(engine_state, stack, call, input)
}
}
pub fn rotate(
engine_state: &EngineState,
stack: &mut Stack,
call: &Call,
input: PipelineData,
) -> Result<PipelineData, ShellError> {
let metadata = input.metadata();
let col_given_names: Vec<String> = call.rest(engine_state, stack, 0)?;
let span = input.span();
let mut values = input.into_iter().collect::<Vec<_>>();
let mut old_column_names = vec![];
let mut new_values = vec![];
let mut not_a_record = false;
let total_rows = &mut values.len();
let ccw: bool = call.has_flag("ccw");
if !ccw {
values.reverse();
}
if !values.is_empty() {
for val in values.into_iter() {
match val {
Value::Record { cols, vals, .. } => {
old_column_names = cols;
for v in vals {
new_values.push(v)
}
}
Value::List { vals, .. } => {
not_a_record = true;
for v in vals {
new_values.push(v);
}
}
Value::String { val, span } => {
not_a_record = true;
new_values.push(Value::String { val, span })
}
x => {
not_a_record = true;
new_values.push(x)
}
}
}
} else {
return Err(ShellError::UnsupportedInput(
"list input is empty".to_string(),
"value originates from here".into(),
call.head,
// TODO: Maybe make all Pipelines have spans, so that this doesn't need to be unwrapped.
span.unwrap_or(call.head),
));
}
let total_columns = &old_column_names.len();
// we use this for building columns names, but for non-records we get an extra row so we remove it
if *total_columns == 0 {
*total_rows -= 1;
}
// holder for the new column names, particularly if none are provided by the user we create names as column0, column1, etc.
let mut new_column_names = {
let mut res = vec![];
for idx in 0..(*total_rows + 1) {
res.push(format!("column{idx}"));
}
res.to_vec()
};
// we got new names for columns from the input, so we need to swap those we already made
if !col_given_names.is_empty() {
for (idx, val) in col_given_names.into_iter().enumerate() {
if idx > new_column_names.len() - 1 {
break;
}
new_column_names[idx] = val;
}
}
if not_a_record {
return Ok(Value::List {
vals: vec![Value::Record {
cols: new_column_names,
vals: new_values,
span: call.head,
}],
span: call.head,
}
.into_pipeline_data()
.set_metadata(metadata));
}
// holder for the new records
let mut final_values = vec![];
// the number of initial columns will be our number of rows, so we iterate through that to get the new number of rows that we need to make
// for counter clockwise, we're iterating from right to left and have a pair of (index, value)
let columns_iter = if ccw {
old_column_names
.iter()
.enumerate()
.rev()
.collect::<Vec<_>>()
} else {
// as we're rotating clockwise, we're iterating from left to right and have a pair of (index, value)
old_column_names.iter().enumerate().collect::<Vec<_>>()
};
for (idx, val) in columns_iter {
// when rotating counter clockwise, the old columns names become the first column's values
let mut res = if ccw {
vec![Value::string(val, call.head)]
} else {
vec![]
};
let new_vals = {
// move through the array with a step, which is every new_values size / total rows, starting from our old column's index
// so if initial data was like this [[a b]; [1 2] [3 4]] - we basically iterate on this [3 4 1 2] array, so we pick 3, then 1, and then when idx increases, we pick 4 and 2
for i in (idx..new_values.len()).step_by(new_values.len() / *total_rows) {
res.push(new_values[i].clone());
}
// when rotating clockwise, the old column names become the last column's values
if !ccw {
res.push(Value::string(val, call.head));
}
res.to_vec()
};
final_values.push(Value::Record {
cols: new_column_names.clone(),
vals: new_vals,
span: call.head,
})
}
Ok(Value::List {
vals: final_values,
span: call.head,
}
.into_pipeline_data()
.set_metadata(metadata))
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_examples() {
use crate::test_examples;
test_examples(Rotate)
}
}