1
- use std:: { fmt, io} ;
2
1
use std:: cell:: RefCell ;
3
2
use std:: rc:: Rc ;
3
+ use std:: { fmt, io} ;
4
4
5
- use crate :: column:: { TPrintColumn , TPrintAlign } ;
6
- use crate :: output:: { TPrintOutput , TPrintOutputStdout } ;
7
5
use crate :: borders:: { TPrintBorders , TPrintBordersASCII , TPrintBordersType } ;
6
+ use crate :: column:: { TPrintAlign , TPrintColumn } ;
7
+ use crate :: output:: { TPrintOutput , TPrintOutputStdout } ;
8
8
use crate :: utils:: get_string_width;
9
9
10
10
type TPrintOutputRef = Rc < RefCell < dyn TPrintOutput > > ;
@@ -24,7 +24,12 @@ pub struct TPrint {
24
24
25
25
impl TPrint {
26
26
/// Creates a new TPrint object with the default ASCII borders and default output to stdout.
27
- pub fn new ( show_borders : bool , show_headers : bool , spaces_left : usize , extra_spaces_between : usize ) -> Self {
27
+ pub fn new (
28
+ show_borders : bool ,
29
+ show_headers : bool ,
30
+ spaces_left : usize ,
31
+ extra_spaces_between : usize ,
32
+ ) -> Self {
28
33
TPrint {
29
34
output : Rc :: new ( RefCell :: new ( TPrintOutputStdout { } ) ) ,
30
35
borders : Rc :: new ( RefCell :: new ( TPrintBordersASCII { } ) ) ,
@@ -37,7 +42,13 @@ impl TPrint {
37
42
}
38
43
}
39
44
/// Creates a new TPrint object with the default ASCII borders and the specified output.
40
- pub fn new_with_output ( output : TPrintOutputRef , show_borders : bool , show_headers : bool , spaces_left : usize , extra_spaces_between : usize ) -> Self {
45
+ pub fn new_with_output (
46
+ output : TPrintOutputRef ,
47
+ show_borders : bool ,
48
+ show_headers : bool ,
49
+ spaces_left : usize ,
50
+ extra_spaces_between : usize ,
51
+ ) -> Self {
41
52
TPrint {
42
53
output,
43
54
borders : Rc :: new ( RefCell :: new ( TPrintBordersASCII { } ) ) ,
@@ -51,7 +62,13 @@ impl TPrint {
51
62
}
52
63
53
64
/// Creates a new TPrint object with the specified borders and default output to stdout.
54
- pub fn new_with_borders ( borders : TPrintBordersRef , show_borders : bool , show_headers : bool , spaces_left : usize , extra_spaces_between : usize ) -> Self {
65
+ pub fn new_with_borders (
66
+ borders : TPrintBordersRef ,
67
+ show_borders : bool ,
68
+ show_headers : bool ,
69
+ spaces_left : usize ,
70
+ extra_spaces_between : usize ,
71
+ ) -> Self {
55
72
TPrint {
56
73
output : Rc :: new ( RefCell :: new ( TPrintOutputStdout { } ) ) ,
57
74
borders,
@@ -65,7 +82,14 @@ impl TPrint {
65
82
}
66
83
67
84
/// Creates a new TPrint object with the specified borders and output.
68
- pub fn new_with_borders_output ( borders : TPrintBordersRef , output : TPrintOutputRef , show_borders : bool , show_headers : bool , spaces_left : usize , extra_spaces_between : usize ) -> Self {
85
+ pub fn new_with_borders_output (
86
+ borders : TPrintBordersRef ,
87
+ output : TPrintOutputRef ,
88
+ show_borders : bool ,
89
+ show_headers : bool ,
90
+ spaces_left : usize ,
91
+ extra_spaces_between : usize ,
92
+ ) -> Self {
69
93
TPrint {
70
94
output,
71
95
borders,
@@ -80,8 +104,14 @@ impl TPrint {
80
104
81
105
/// Adds a new column with the specified caption text, caption alignment and cells alignment.
82
106
/// Columns must be defined before adding cell data to the table.
83
- pub fn column_add ( & mut self , caption : & str , caption_align : TPrintAlign , cell_align : TPrintAlign ) -> & mut Self {
84
- self . columns . push ( TPrintColumn :: new ( caption, caption_align, cell_align) ) ;
107
+ pub fn column_add (
108
+ & mut self ,
109
+ caption : & str ,
110
+ caption_align : TPrintAlign ,
111
+ cell_align : TPrintAlign ,
112
+ ) -> & mut Self {
113
+ self . columns
114
+ . push ( TPrintColumn :: new ( caption, caption_align, cell_align) ) ;
85
115
self
86
116
}
87
117
@@ -98,7 +128,8 @@ impl TPrint {
98
128
/// tp.add_data("test");
99
129
/// tp.add_data(42);
100
130
/// tp.print()?;
101
- /// ```
131
+ /// # Ok::<(), std::io::Error>(())
132
+ ///```
102
133
///
103
134
/// ## Chained Usage
104
135
/// ```
@@ -108,6 +139,7 @@ impl TPrint {
108
139
/// column_add("Count", TPrintAlign::Center, TPrintAlign::Left).
109
140
/// add_data("test").add_data(42).
110
141
/// print()?;
142
+ /// # Ok::<(), std::io::Error>(())
111
143
/// ```
112
144
pub fn add_data < T : fmt:: Display > ( & mut self , value : T ) -> & mut Self {
113
145
let column_id = self . current_column_id ;
@@ -123,30 +155,62 @@ impl TPrint {
123
155
self
124
156
}
125
157
126
- fn print_horizonal_border ( & self , left : & TPrintBordersType , right : & TPrintBordersType , middle : & TPrintBordersType , line : & TPrintBordersType ) -> io:: Result < ( ) > {
158
+ fn print_horizonal_border (
159
+ & self ,
160
+ left : & TPrintBordersType ,
161
+ right : & TPrintBordersType ,
162
+ middle : & TPrintBordersType ,
163
+ line : & TPrintBordersType ,
164
+ ) -> io:: Result < ( ) > {
127
165
if !self . show_borders {
128
- self . output . borrow_mut ( ) . print_str ( self . borders . borrow ( ) . get_border ( & TPrintBordersType :: NewLine ) ) ?;
166
+ self . output . borrow_mut ( ) . print_str (
167
+ self . borders
168
+ . borrow ( )
169
+ . get_border ( & TPrintBordersType :: NewLine ) ,
170
+ ) ?;
129
171
return Ok ( ( ) ) ;
130
172
}
131
173
132
174
if self . spaces_left > 0 {
133
- self . output . borrow_mut ( ) . print_str ( & self . borders . borrow ( ) . get_border ( & TPrintBordersType :: WhiteSpace ) . repeat ( self . spaces_left ) ) ?;
175
+ self . output . borrow_mut ( ) . print_str (
176
+ & self
177
+ . borders
178
+ . borrow ( )
179
+ . get_border ( & TPrintBordersType :: WhiteSpace )
180
+ . repeat ( self . spaces_left ) ,
181
+ ) ?;
134
182
}
135
183
136
- self . output . borrow_mut ( ) . print_str ( self . borders . borrow ( ) . get_border ( left) ) ?;
184
+ self . output
185
+ . borrow_mut ( )
186
+ . print_str ( self . borders . borrow ( ) . get_border ( left) ) ?;
137
187
138
188
for i in 0 ..self . columns . len ( ) {
139
189
let c = & self . columns [ i] ;
140
190
141
- self . output . borrow_mut ( ) . print_str ( & self . borders . borrow ( ) . get_border ( line) . repeat ( c. get_max_width ( ) + 2 * self . spaces_between ) ) ?;
191
+ self . output . borrow_mut ( ) . print_str (
192
+ & self
193
+ . borders
194
+ . borrow ( )
195
+ . get_border ( line)
196
+ . repeat ( c. get_max_width ( ) + 2 * self . spaces_between ) ,
197
+ ) ?;
142
198
143
199
if i < self . columns . len ( ) - 1 {
144
- self . output . borrow_mut ( ) . print_str ( self . borders . borrow ( ) . get_border ( middle) ) ?;
200
+ self . output
201
+ . borrow_mut ( )
202
+ . print_str ( self . borders . borrow ( ) . get_border ( middle) ) ?;
145
203
}
146
204
}
147
205
148
- self . output . borrow_mut ( ) . print_str ( self . borders . borrow ( ) . get_border ( right) ) ?;
149
- self . output . borrow_mut ( ) . print_str ( self . borders . borrow ( ) . get_border ( & TPrintBordersType :: NewLine ) ) ?;
206
+ self . output
207
+ . borrow_mut ( )
208
+ . print_str ( self . borders . borrow ( ) . get_border ( right) ) ?;
209
+ self . output . borrow_mut ( ) . print_str (
210
+ self . borders
211
+ . borrow ( )
212
+ . get_border ( & TPrintBordersType :: NewLine ) ,
213
+ ) ?;
150
214
151
215
Ok ( ( ) )
152
216
}
@@ -155,28 +219,44 @@ impl TPrint {
155
219
if !self . show_borders {
156
220
return Ok ( ( ) ) ;
157
221
}
158
- self . output . borrow_mut ( ) . print_str ( self . borders . borrow ( ) . get_border ( left_border) ) ?;
222
+ self . output
223
+ . borrow_mut ( )
224
+ . print_str ( self . borders . borrow ( ) . get_border ( left_border) ) ?;
159
225
Ok ( ( ) )
160
226
}
161
227
162
228
fn print_right_border ( & self , right_border : & TPrintBordersType ) -> io:: Result < ( ) > {
163
229
if !self . show_borders {
164
230
return Ok ( ( ) ) ;
165
231
}
166
- self . output . borrow_mut ( ) . print_str ( self . borders . borrow ( ) . get_border ( right_border) ) ?;
167
- self . output . borrow_mut ( ) . print_str ( self . borders . borrow ( ) . get_border ( & TPrintBordersType :: NewLine ) ) ?;
232
+ self . output
233
+ . borrow_mut ( )
234
+ . print_str ( self . borders . borrow ( ) . get_border ( right_border) ) ?;
235
+ self . output . borrow_mut ( ) . print_str (
236
+ self . borders
237
+ . borrow ( )
238
+ . get_border ( & TPrintBordersType :: NewLine ) ,
239
+ ) ?;
168
240
Ok ( ( ) )
169
241
}
170
242
171
243
fn print_internal_border ( & self , v_border : & TPrintBordersType ) -> io:: Result < ( ) > {
172
244
if !self . show_borders {
173
245
return Ok ( ( ) ) ;
174
246
}
175
- self . output . borrow_mut ( ) . print_str ( self . borders . borrow ( ) . get_border ( v_border) ) ?;
247
+ self . output
248
+ . borrow_mut ( )
249
+ . print_str ( self . borders . borrow ( ) . get_border ( v_border) ) ?;
176
250
Ok ( ( ) )
177
251
}
178
252
179
- fn print_cell ( & self , value : & str , max_width : usize , align : & TPrintAlign , whitespace : & str ) -> io:: Result < ( ) > {
253
+ fn print_cell (
254
+ & self ,
255
+ value : & str ,
256
+ max_width : usize ,
257
+ align : & TPrintAlign ,
258
+ whitespace : & str ,
259
+ ) -> io:: Result < ( ) > {
180
260
let left_spaces;
181
261
let right_spaces;
182
262
@@ -197,26 +277,44 @@ impl TPrint {
197
277
}
198
278
}
199
279
200
- self . output . borrow_mut ( ) . print_str ( & whitespace. repeat ( left_spaces) ) ?;
280
+ self . output
281
+ . borrow_mut ( )
282
+ . print_str ( & whitespace. repeat ( left_spaces) ) ?;
201
283
self . output . borrow_mut ( ) . print_str ( value) ?;
202
- self . output . borrow_mut ( ) . print_str ( & whitespace. repeat ( right_spaces) ) ?;
284
+ self . output
285
+ . borrow_mut ( )
286
+ . print_str ( & whitespace. repeat ( right_spaces) ) ?;
203
287
Ok ( ( ) )
204
288
}
205
289
206
290
/// Prints the table to the output.
207
291
pub fn print ( & self ) -> io:: Result < ( ) > {
208
- let total_rows: usize = self . columns . iter ( ) . map ( |c| c. get_rows_count ( ) ) . max ( ) . unwrap_or ( 0 ) ;
292
+ let total_rows: usize = self
293
+ . columns
294
+ . iter ( )
295
+ . map ( |c| c. get_rows_count ( ) )
296
+ . max ( )
297
+ . unwrap_or ( 0 ) ;
209
298
210
299
let border = self . borders . borrow ( ) ;
211
300
let whitespace = border. get_border ( & TPrintBordersType :: WhiteSpace ) ;
212
301
213
- self . output . borrow_mut ( ) . print_str ( border. get_intro ( self . show_borders , self . show_headers ) ) ?;
302
+ self . output
303
+ . borrow_mut ( )
304
+ . print_str ( border. get_intro ( self . show_borders , self . show_headers ) ) ?;
214
305
215
306
if self . show_headers {
216
- self . print_horizonal_border ( & TPrintBordersType :: HeaderTopLeft , & TPrintBordersType :: HeaderTopRight , & TPrintBordersType :: HeaderTopMiddle , & TPrintBordersType :: HeaderHLine ) ?;
307
+ self . print_horizonal_border (
308
+ & TPrintBordersType :: HeaderTopLeft ,
309
+ & TPrintBordersType :: HeaderTopRight ,
310
+ & TPrintBordersType :: HeaderTopMiddle ,
311
+ & TPrintBordersType :: HeaderHLine ,
312
+ ) ?;
217
313
218
314
if self . spaces_left > 0 {
219
- self . output . borrow_mut ( ) . print_str ( & whitespace. repeat ( self . spaces_left ) ) ?;
315
+ self . output
316
+ . borrow_mut ( )
317
+ . print_str ( & whitespace. repeat ( self . spaces_left ) ) ?;
220
318
}
221
319
222
320
self . print_left_border ( & TPrintBordersType :: HeaderLeftVLine ) ?;
@@ -228,20 +326,36 @@ impl TPrint {
228
326
self . print_internal_border ( & TPrintBordersType :: HeaderMiddleVLine ) ?;
229
327
}
230
328
231
- self . print_cell ( c. get_caption ( ) , c. get_max_width ( ) , c. get_caption_align ( ) , whitespace) ?;
329
+ self . print_cell (
330
+ c. get_caption ( ) ,
331
+ c. get_max_width ( ) ,
332
+ c. get_caption_align ( ) ,
333
+ whitespace,
334
+ ) ?;
232
335
}
233
336
234
337
self . print_right_border ( & TPrintBordersType :: HeaderRightVLine ) ?;
235
338
236
- self . print_horizonal_border ( & TPrintBordersType :: HeaderBottomLeft , & TPrintBordersType :: HeaderBottomRight , & TPrintBordersType :: HeaderBottomMiddle , & TPrintBordersType :: HeaderHLine ) ?;
237
-
339
+ self . print_horizonal_border (
340
+ & TPrintBordersType :: HeaderBottomLeft ,
341
+ & TPrintBordersType :: HeaderBottomRight ,
342
+ & TPrintBordersType :: HeaderBottomMiddle ,
343
+ & TPrintBordersType :: HeaderHLine ,
344
+ ) ?;
238
345
} else {
239
- self . print_horizonal_border ( & TPrintBordersType :: TopLeft , & TPrintBordersType :: TopRight , & TPrintBordersType :: TopMiddle , & TPrintBordersType :: TopHLine ) ?;
346
+ self . print_horizonal_border (
347
+ & TPrintBordersType :: TopLeft ,
348
+ & TPrintBordersType :: TopRight ,
349
+ & TPrintBordersType :: TopMiddle ,
350
+ & TPrintBordersType :: TopHLine ,
351
+ ) ?;
240
352
}
241
353
242
354
for r in 0 ..total_rows {
243
355
if self . spaces_left > 0 {
244
- self . output . borrow_mut ( ) . print_str ( & whitespace. repeat ( self . spaces_left ) ) ?;
356
+ self . output
357
+ . borrow_mut ( )
358
+ . print_str ( & whitespace. repeat ( self . spaces_left ) ) ?;
245
359
}
246
360
247
361
self . print_left_border ( & TPrintBordersType :: MiddleLeftVLine ) ?;
@@ -252,17 +366,32 @@ impl TPrint {
252
366
self . print_internal_border ( & TPrintBordersType :: MiddleMiddleVLine ) ?;
253
367
}
254
368
255
- self . print_cell ( c. get_str ( r) , c. get_max_width ( ) , c. get_cell_align ( ) , whitespace) ?;
369
+ self . print_cell (
370
+ c. get_str ( r) ,
371
+ c. get_max_width ( ) ,
372
+ c. get_cell_align ( ) ,
373
+ whitespace,
374
+ ) ?;
256
375
}
257
376
self . print_right_border ( & TPrintBordersType :: MiddleRightVLine ) ?;
258
377
259
378
if r < total_rows - 1 {
260
- self . print_horizonal_border ( & TPrintBordersType :: MiddleLeft , & TPrintBordersType :: MiddleRight , & TPrintBordersType :: MiddleMiddle , & TPrintBordersType :: MiddleHLine ) ?;
379
+ self . print_horizonal_border (
380
+ & TPrintBordersType :: MiddleLeft ,
381
+ & TPrintBordersType :: MiddleRight ,
382
+ & TPrintBordersType :: MiddleMiddle ,
383
+ & TPrintBordersType :: MiddleHLine ,
384
+ ) ?;
261
385
}
262
386
}
263
387
264
- self . print_horizonal_border ( & TPrintBordersType :: BottomLeft , & TPrintBordersType :: BottomRight , & TPrintBordersType :: BottomMiddle , & TPrintBordersType :: BottomHLine ) ?;
388
+ self . print_horizonal_border (
389
+ & TPrintBordersType :: BottomLeft ,
390
+ & TPrintBordersType :: BottomRight ,
391
+ & TPrintBordersType :: BottomMiddle ,
392
+ & TPrintBordersType :: BottomHLine ,
393
+ ) ?;
265
394
266
395
self . output . borrow_mut ( ) . print_str ( border. get_closing ( ) )
267
396
}
268
- }
397
+ }
0 commit comments