Skip to content

Commit 526e332

Browse files
committed
Fix indentation for formatting higher-dim arrays
1 parent c9c6112 commit 526e332

File tree

1 file changed

+22
-9
lines changed

1 file changed

+22
-9
lines changed

src/arrayformat.rs

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ fn format_array<A, S, D, F>(
6565
f: &mut fmt::Formatter<'_>,
6666
mut format: F,
6767
limit: Ix,
68+
depth: usize,
6869
) -> fmt::Result
6970
where
7071
F: FnMut(&A, &mut fmt::Formatter<'_>) -> fmt::Result + Clone,
@@ -98,19 +99,31 @@ where
9899

99100
let n_to_be_printed = to_be_printed.len();
100101

102+
let indent = " ".repeat(depth + 1);
103+
101104
write!(f, "[")?;
102105
for (j, index) in to_be_printed.into_iter().enumerate() {
103106
match index {
104107
PrintableCell::ElementIndex(i) => {
108+
// Indent all but the first line.
109+
if j != 0 {
110+
write!(f, "{}", indent)?;
111+
}
105112
// Proceed recursively with the (n-1)-dimensional slice
106-
format_array(&view.index_axis(Axis(0), i), f, format.clone(), limit)?;
113+
format_array(
114+
&view.index_axis(Axis(0), i),
115+
f,
116+
format.clone(),
117+
limit,
118+
depth + 1,
119+
)?;
107120
// We need to add a separator after each slice,
108121
// apart from the last one
109122
if j != n_to_be_printed - 1 {
110-
write!(f, ",\n ")?
123+
write!(f, ",\n")?
111124
}
112125
}
113-
PrintableCell::Ellipses => write!(f, "...,\n ")?,
126+
PrintableCell::Ellipses => write!(f, "{}...,\n", indent)?,
114127
}
115128
}
116129
write!(f, "]")?;
@@ -129,7 +142,7 @@ where
129142
S: Data<Elem = A>,
130143
{
131144
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
132-
format_array(self, f, <_>::fmt, PRINT_ELEMENTS_LIMIT)
145+
format_array(self, f, <_>::fmt, PRINT_ELEMENTS_LIMIT, 0)
133146
}
134147
}
135148

@@ -143,7 +156,7 @@ where
143156
{
144157
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
145158
// Add extra information for Debug
146-
format_array(self, f, <_>::fmt, PRINT_ELEMENTS_LIMIT)?;
159+
format_array(self, f, <_>::fmt, PRINT_ELEMENTS_LIMIT, 0)?;
147160
write!(
148161
f,
149162
" shape={:?}, strides={:?}, layout={:?}",
@@ -168,7 +181,7 @@ where
168181
S: Data<Elem = A>,
169182
{
170183
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
171-
format_array(self, f, <_>::fmt, PRINT_ELEMENTS_LIMIT)
184+
format_array(self, f, <_>::fmt, PRINT_ELEMENTS_LIMIT, 0)
172185
}
173186
}
174187

@@ -181,7 +194,7 @@ where
181194
S: Data<Elem = A>,
182195
{
183196
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
184-
format_array(self, f, <_>::fmt, PRINT_ELEMENTS_LIMIT)
197+
format_array(self, f, <_>::fmt, PRINT_ELEMENTS_LIMIT, 0)
185198
}
186199
}
187200
/// Format the array using `LowerHex` and apply the formatting parameters used
@@ -193,7 +206,7 @@ where
193206
S: Data<Elem = A>,
194207
{
195208
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
196-
format_array(self, f, <_>::fmt, PRINT_ELEMENTS_LIMIT)
209+
format_array(self, f, <_>::fmt, PRINT_ELEMENTS_LIMIT, 0)
197210
}
198211
}
199212

@@ -206,7 +219,7 @@ where
206219
S: Data<Elem = A>,
207220
{
208221
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
209-
format_array(self, f, <_>::fmt, PRINT_ELEMENTS_LIMIT)
222+
format_array(self, f, <_>::fmt, PRINT_ELEMENTS_LIMIT, 0)
210223
}
211224
}
212225

0 commit comments

Comments
 (0)