Skip to content

Commit 1d77e93

Browse files
committed
Fix indentation for formatting higher-dim arrays
1 parent cedbdf9 commit 1d77e93

File tree

1 file changed

+18
-10
lines changed

1 file changed

+18
-10
lines changed

src/arrayformat.rs

+18-10
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,9 @@ fn format_array<A, S, D, F>(
7070
view: &ArrayBase<S, D>,
7171
f: &mut fmt::Formatter,
7272
mut format: F,
73-
limit: Ix) -> fmt::Result
73+
limit: Ix,
74+
depth: usize,
75+
) -> fmt::Result
7476
where
7577
F: FnMut(&A, &mut fmt::Formatter) -> fmt::Result + Clone,
7678
D: Dimension,
@@ -98,21 +100,27 @@ where
98100

99101
let n_to_be_printed = to_be_printed.len();
100102

103+
let indent = " ".repeat(depth + 1);
104+
101105
write!(f, "[")?;
102106
for (j, index) in to_be_printed.into_iter().enumerate() {
103107
match index {
104108
PrintableCell::ElementIndex(i) => {
109+
// Indent all but the first line.
110+
if j != 0 {
111+
write!(f, "{}", indent)?;
112+
}
105113
// Proceed recursively with the (n-1)-dimensional slice
106114
format_array(
107-
&view.index_axis(Axis(0), i), f, format.clone(), limit
115+
&view.index_axis(Axis(0), i), f, format.clone(), limit, depth + 1
108116
)?;
109117
// We need to add a separator after each slice,
110118
// apart from the last one
111119
if j != n_to_be_printed - 1 {
112-
write!(f, ",\n ")?
120+
write!(f, ",\n")?
113121
}
114122
},
115-
PrintableCell::Ellipses => write!(f, "...,\n ")?
123+
PrintableCell::Ellipses => write!(f, "{}...,\n", indent)?,
116124
}
117125
}
118126
write!(f, "]")?;
@@ -130,7 +138,7 @@ impl<'a, A: fmt::Display, S, D: Dimension> fmt::Display for ArrayBase<S, D>
130138
where S: Data<Elem=A>,
131139
{
132140
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
133-
format_array(self, f, <_>::fmt, PRINT_ELEMENTS_LIMIT)
141+
format_array(self, f, <_>::fmt, PRINT_ELEMENTS_LIMIT, 0)
134142
}
135143
}
136144

@@ -143,7 +151,7 @@ impl<'a, A: fmt::Debug, S, D: Dimension> fmt::Debug for ArrayBase<S, D>
143151
{
144152
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
145153
// Add extra information for Debug
146-
format_array(self, f, <_>::fmt, PRINT_ELEMENTS_LIMIT)?;
154+
format_array(self, f, <_>::fmt, PRINT_ELEMENTS_LIMIT, 0)?;
147155
write!(f, " shape={:?}, strides={:?}, layout={:?}",
148156
self.shape(), self.strides(), layout=self.view().layout())?;
149157
match D::NDIM {
@@ -162,7 +170,7 @@ impl<'a, A: fmt::LowerExp, S, D: Dimension> fmt::LowerExp for ArrayBase<S, D>
162170
where S: Data<Elem=A>,
163171
{
164172
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
165-
format_array(self, f, <_>::fmt, PRINT_ELEMENTS_LIMIT)
173+
format_array(self, f, <_>::fmt, PRINT_ELEMENTS_LIMIT, 0)
166174
}
167175
}
168176

@@ -174,7 +182,7 @@ impl<'a, A: fmt::UpperExp, S, D: Dimension> fmt::UpperExp for ArrayBase<S, D>
174182
where S: Data<Elem=A>,
175183
{
176184
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
177-
format_array(self, f, <_>::fmt, PRINT_ELEMENTS_LIMIT)
185+
format_array(self, f, <_>::fmt, PRINT_ELEMENTS_LIMIT, 0)
178186
}
179187
}
180188
/// Format the array using `LowerHex` and apply the formatting parameters used
@@ -185,7 +193,7 @@ impl<'a, A: fmt::LowerHex, S, D: Dimension> fmt::LowerHex for ArrayBase<S, D>
185193
where S: Data<Elem=A>,
186194
{
187195
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
188-
format_array(self, f, <_>::fmt, PRINT_ELEMENTS_LIMIT)
196+
format_array(self, f, <_>::fmt, PRINT_ELEMENTS_LIMIT, 0)
189197
}
190198
}
191199

@@ -197,7 +205,7 @@ impl<'a, A: fmt::Binary, S, D: Dimension> fmt::Binary for ArrayBase<S, D>
197205
where S: Data<Elem=A>,
198206
{
199207
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
200-
format_array(self, f, <_>::fmt, PRINT_ELEMENTS_LIMIT)
208+
format_array(self, f, <_>::fmt, PRINT_ELEMENTS_LIMIT, 0)
201209
}
202210
}
203211

0 commit comments

Comments
 (0)