Fix indentation for formatting higher-dim arrays
@@ -65,6 +65,7 @@ fn format_array<A, S, D, F>(
f: &mut fmt::Formatter<'_>,
mut format: F,
limit: Ix,
+ depth: usize,
) -> fmt::Result
where
F: FnMut(&A, &mut fmt::Formatter<'_>) -> fmt::Result + Clone,
@@ -98,19 +99,31 @@ where
let n_to_be_printed = to_be_printed.len();
+ let indent = " ".repeat(depth + 1);
+
write!(f, "[")?;
for (j, index) in to_be_printed.into_iter().enumerate() {
match index {
PrintableCell::ElementIndex(i) => {
+ // Indent all but the first line.
+ if j != 0 {
+ write!(f, "{}", indent)?;
+ }
// Proceed recursively with the (n-1)-dimensional slice
- format_array(&view.index_axis(Axis(0), i), f, format.clone(), limit)?;
+ format_array(
+ &view.index_axis(Axis(0), i),
+ f,
+ format.clone(),
+ limit,
+ depth + 1,
+ )?;
// We need to add a separator after each slice,
// apart from the last one
if j != n_to_be_printed - 1 {
write!(f, ",\n")?
}
}
- PrintableCell::Ellipses => write!(f, "...,\n ")?,
+ PrintableCell::Ellipses => write!(f, "{}...,\n", indent)?,
}
}
write!(f, "]")?;
@@ -129,7 +142,7 @@ where
S: Data<Elem = A>,
{
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
- format_array(self, f, <_>::fmt, PRINT_ELEMENTS_LIMIT)
+ format_array(self, f, <_>::fmt, PRINT_ELEMENTS_LIMIT, 0)
}
}
@@ -143,7 +156,7 @@ where
{
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
// Add extra information for Debug
- format_array(self, f, <_>::fmt, PRINT_ELEMENTS_LIMIT)?;
+ format_array(self, f, <_>::fmt, PRINT_ELEMENTS_LIMIT, 0)?;
write!(
f,
" shape={:?}, strides={:?}, layout={:?}",
@@ -168,7 +181,7 @@ where
S: Data<Elem = A>,
{
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
- format_array(self, f, <_>::fmt, PRINT_ELEMENTS_LIMIT)
+ format_array(self, f, <_>::fmt, PRINT_ELEMENTS_LIMIT, 0)
}
}
@@ -181,7 +194,7 @@ where
S: Data<Elem = A>,
{
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
- format_array(self, f, <_>::fmt, PRINT_ELEMENTS_LIMIT)
+ format_array(self, f, <_>::fmt, PRINT_ELEMENTS_LIMIT, 0)
}
}
/// Format the array using `LowerHex` and apply the formatting parameters used
@@ -193,7 +206,7 @@ where
S: Data<Elem = A>,
{
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
- format_array(self, f, <_>::fmt, PRINT_ELEMENTS_LIMIT)
+ format_array(self, f, <_>::fmt, PRINT_ELEMENTS_LIMIT, 0)
}
}
@@ -206,7 +219,7 @@ where
S: Data<Elem = A>,
{
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
- format_array(self, f, <_>::fmt, PRINT_ELEMENTS_LIMIT)
+ format_array(self, f, <_>::fmt, PRINT_ELEMENTS_LIMIT, 0)
}
}
