@@ -9,19 +9,20 @@ use super::{ArrayBase, Axis, Data, Dimension, Ix, NdProducer};
9
9
use crate :: aliases:: Ix1 ;
10
10
use std:: fmt;
11
11
12
- const PRINT_ELEMENTS_LIMIT : Ix = 3 ;
12
+ /// Maximum axis length before overflowing with an ellipsis.
13
+ const AXIS_LEN_LIMIT : Ix = 6 ;
13
14
15
+ /// The string used as an ellipsis.
14
16
const ELLIPSIS : & str = "..." ;
15
17
16
18
/// Formats the contents of a list of items, using an ellipsis to indicate when
17
- /// the length of the list is greater than `2 * limit`.
19
+ /// the ` length` of the list is greater than `limit`.
18
20
///
19
21
/// # Parameters
20
22
///
21
23
/// * `f`: The formatter.
22
24
/// * `length`: The length of the list.
23
- /// * `limit`: Half the maximum number of items before indicating overflow with
24
- /// an ellipsis. Also, the number of items on either side of the ellipsis.
25
+ /// * `limit`: The maximum number of items before overflow.
25
26
/// * `separator`: Separator to write between items.
26
27
/// * `ellipsis`: Ellipsis for indicating overflow.
27
28
/// * `fmt_elem`: A function that formats an element in the list, given the
@@ -39,21 +40,22 @@ where
39
40
{
40
41
if length == 0 {
41
42
// no-op
42
- } else if length <= 2 * limit {
43
+ } else if length <= limit {
43
44
fmt_elem ( f, 0 ) ?;
44
45
( 1 ..length) . try_for_each ( |i| {
45
46
f. write_str ( separator) ?;
46
47
fmt_elem ( f, i)
47
48
} ) ?;
48
49
} else {
50
+ let edge = limit / 2 ;
49
51
fmt_elem ( f, 0 ) ?;
50
- ( 1 ..limit ) . try_for_each ( |i| {
52
+ ( 1 ..edge ) . try_for_each ( |i| {
51
53
f. write_str ( separator) ?;
52
54
fmt_elem ( f, i)
53
55
} ) ?;
54
56
f. write_str ( separator) ?;
55
57
f. write_str ( ellipsis) ?;
56
- ( length - limit ..length) . try_for_each ( |i| {
58
+ ( length - edge ..length) . try_for_each ( |i| {
57
59
f. write_str ( separator) ?;
58
60
fmt_elem ( f, i)
59
61
} ) ?;
@@ -127,7 +129,7 @@ where
127
129
S : Data < Elem = A > ,
128
130
{
129
131
fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
130
- format_array ( self , f, <_ >:: fmt, PRINT_ELEMENTS_LIMIT , 0 )
132
+ format_array ( self , f, <_ >:: fmt, AXIS_LEN_LIMIT , 0 )
131
133
}
132
134
}
133
135
@@ -141,7 +143,7 @@ where
141
143
{
142
144
fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
143
145
// Add extra information for Debug
144
- format_array ( self , f, <_ >:: fmt, PRINT_ELEMENTS_LIMIT , 0 ) ?;
146
+ format_array ( self , f, <_ >:: fmt, AXIS_LEN_LIMIT , 0 ) ?;
145
147
write ! (
146
148
f,
147
149
", shape={:?}, strides={:?}, layout={:?}" ,
@@ -166,7 +168,7 @@ where
166
168
S : Data < Elem = A > ,
167
169
{
168
170
fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
169
- format_array ( self , f, <_ >:: fmt, PRINT_ELEMENTS_LIMIT , 0 )
171
+ format_array ( self , f, <_ >:: fmt, AXIS_LEN_LIMIT , 0 )
170
172
}
171
173
}
172
174
@@ -179,7 +181,7 @@ where
179
181
S : Data < Elem = A > ,
180
182
{
181
183
fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
182
- format_array ( self , f, <_ >:: fmt, PRINT_ELEMENTS_LIMIT , 0 )
184
+ format_array ( self , f, <_ >:: fmt, AXIS_LEN_LIMIT , 0 )
183
185
}
184
186
}
185
187
/// Format the array using `LowerHex` and apply the formatting parameters used
@@ -191,7 +193,7 @@ where
191
193
S : Data < Elem = A > ,
192
194
{
193
195
fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
194
- format_array ( self , f, <_ >:: fmt, PRINT_ELEMENTS_LIMIT , 0 )
196
+ format_array ( self , f, <_ >:: fmt, AXIS_LEN_LIMIT , 0 )
195
197
}
196
198
}
197
199
@@ -204,7 +206,7 @@ where
204
206
S : Data < Elem = A > ,
205
207
{
206
208
fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
207
- format_array ( self , f, <_ >:: fmt, PRINT_ELEMENTS_LIMIT , 0 )
209
+ format_array ( self , f, <_ >:: fmt, AXIS_LEN_LIMIT , 0 )
208
210
}
209
211
}
210
212
@@ -249,7 +251,7 @@ mod formatting_with_omit {
249
251
#[ test]
250
252
fn dim_1 ( ) {
251
253
let overflow: usize = 5 ;
252
- let a = Array1 :: from_elem ( ( PRINT_ELEMENTS_LIMIT * 2 + overflow, ) , 1 ) ;
254
+ let a = Array1 :: from_elem ( AXIS_LEN_LIMIT + overflow, 1 ) ;
253
255
let actual = format ! ( "{}" , a) ;
254
256
let expected = "[1, 1, 1, ..., 1, 1, 1]" ;
255
257
assert_str_eq ( expected, & actual) ;
@@ -258,13 +260,13 @@ mod formatting_with_omit {
258
260
#[ test]
259
261
fn dim_2_last_axis_overflow ( ) {
260
262
let overflow: usize = 3 ;
261
- let a = Array2 :: from_elem (
262
- ( PRINT_ELEMENTS_LIMIT , PRINT_ELEMENTS_LIMIT * 2 + overflow) ,
263
- 1 ,
264
- ) ;
263
+ let a = Array2 :: from_elem ( ( AXIS_LEN_LIMIT , AXIS_LEN_LIMIT + overflow) , 1 ) ;
265
264
let actual = format ! ( "{}" , a) ;
266
265
let expected = "\
267
266
[[1, 1, 1, ..., 1, 1, 1],
267
+ [1, 1, 1, ..., 1, 1, 1],
268
+ [1, 1, 1, ..., 1, 1, 1],
269
+ [1, 1, 1, ..., 1, 1, 1],
268
270
[1, 1, 1, ..., 1, 1, 1],
269
271
[1, 1, 1, ..., 1, 1, 1]]" ;
270
272
assert_str_eq ( expected, & actual) ;
@@ -273,32 +275,23 @@ mod formatting_with_omit {
273
275
#[ test]
274
276
fn dim_2_non_last_axis_overflow ( ) {
275
277
let overflow: usize = 5 ;
276
- let a = Array2 :: from_elem (
277
- ( PRINT_ELEMENTS_LIMIT * 2 + overflow, PRINT_ELEMENTS_LIMIT ) ,
278
- 1 ,
279
- ) ;
278
+ let a = Array2 :: from_elem ( ( AXIS_LEN_LIMIT + overflow, AXIS_LEN_LIMIT ) , 1 ) ;
280
279
let actual = format ! ( "{}" , a) ;
281
280
let expected = "\
282
- [[1, 1, 1],
283
- [1, 1, 1],
284
- [1, 1, 1],
281
+ [[1, 1, 1, 1, 1, 1 ],
282
+ [1, 1, 1, 1, 1, 1 ],
283
+ [1, 1, 1, 1, 1, 1 ],
285
284
...,
286
- [1, 1, 1],
287
- [1, 1, 1],
288
- [1, 1, 1]]" ;
285
+ [1, 1, 1, 1, 1, 1 ],
286
+ [1, 1, 1, 1, 1, 1 ],
287
+ [1, 1, 1, 1, 1, 1 ]]" ;
289
288
assert_str_eq ( expected, & actual) ;
290
289
}
291
290
292
291
#[ test]
293
292
fn dim_2_multi_directional_overflow ( ) {
294
293
let overflow: usize = 5 ;
295
- let a = Array2 :: from_elem (
296
- (
297
- PRINT_ELEMENTS_LIMIT * 2 + overflow,
298
- PRINT_ELEMENTS_LIMIT * 2 + overflow,
299
- ) ,
300
- 1 ,
301
- ) ;
294
+ let a = Array2 :: from_elem ( ( AXIS_LEN_LIMIT + overflow, AXIS_LEN_LIMIT + overflow) , 1 ) ;
302
295
let actual = format ! ( "{}" , a) ;
303
296
let expected = "\
304
297
[[1, 1, 1, ..., 1, 1, 1],
0 commit comments