1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
//!
//! Print data as table.
//!
/// Define a private namespace for all its items.
mod private
{
use crate::*;
use std::
{
borrow::{ Cow, Borrow },
collections::HashMap,
};
use core::
{
fmt,
};
// use former::Former;
//=
/// A struct to configure options for printing data as a table.
///
/// The `Printer` struct provides customizable delimiters for formatting table data. It allows
/// you to define how table data should be separated and formatted, making it adaptable to
/// various needs.
///
/// # Fields
///
/// - `cell_separator`: A `String` that specifies the delimiter used to separate columns
/// within a table. This is the character or string that separates each column.
///
/// - `row_prefix`: A `String` that specifies the prefix added to each row. This can be
/// used to add a consistent start to each row.
///
/// - `row_postfix`: A `String` that specifies the postfix added to each row. This can be
/// used to add a consistent end to each row.
///
/// - `row_postfix`: A `String` that specifies the postfix added to each row. This can be
/// used to add a consistent end to each row.
///
/// ```
// xxx : enable
// #[ derive( Debug, Former ) ]
// #[ derive( Debug ) ]
pub struct Printer< 'callback >
{
/// Convert extract into a string, writing it into destination buffer.
pub output_format : &'callback dyn TableOutputFormat,
/// Filter out columns.
pub filter_col : &'callback ( dyn FilterCol + 'callback ),
/// Filter out rows.
pub filter_row : &'callback ( dyn FilterRow + 'callback ),
}
impl< 'callback > Printer< 'callback >
{
/// Constructor accepting styles/foramt.
pub fn with_format( output_format : &'callback dyn TableOutputFormat ) -> Self
{
let filter_col = Default::default();
let filter_row = Default::default();
Self
{
output_format,
filter_col,
filter_row
}
}
}
impl< 'callback > fmt::Debug for Printer< 'callback >
{
fn fmt( & self, f : & mut fmt::Formatter< '_ > ) -> fmt::Result
{
f.debug_struct( "Printer" )
// .field( "cell_prefix", & self.cell_prefix )
// .field( "cell_postfix", & self.cell_postfix )
// .field( "cell_separator", & self.cell_separator )
// .field( "row_prefix", & self.row_prefix )
// .field( "row_postfix", & self.row_postfix )
// .field( "row_separator", & self.row_separator )
// .field( "output_format", & format_args!( "{:?}", self.output_format ) )
// .field( "filter_col", & format_args!( "{:?}", self.filter_col ) )
.finish()
}
}
impl< 'callback > Default for Printer< 'callback >
{
fn default() -> Self
{
let output_format = Default::default();
let filter_col = Default::default();
let filter_row = Default::default();
Self
{
output_format,
filter_col,
filter_row
}
}
}
/// Struct for managing table formatting context.
///
/// `Context` holds the buffer and styling options used during table
/// formatting, facilitating the writing of formatted table data.
///
pub struct Context< 'context >
{
///
/// A mutable reference to a buffer implementing `fmt::Write`,
/// used to collect the formatted output.
pub buf : &'context mut dyn fmt::Write,
///
/// An instance of `Printer` that defines the formatting
/// options, such as delimiters and prefixes.
pub printer : Printer< 'context >,
}
impl< 'context > Context< 'context >
{
/// Just constructr.
pub fn new( buf : &'context mut dyn fmt::Write, printer : Printer< 'context > ) -> Self
{
Self { buf, printer }
}
}
impl fmt::Debug for Context< '_ >
{
fn fmt( &self, c : &mut fmt::Formatter< '_ > ) -> fmt::Result
{
c
.debug_struct( "Context" )
.field( "buf", &"dyn fmt::Write" )
.field( "printer", &self.printer )
.finish()
}
}
/// Trait for defining table formatting logic.
///
/// `TableFormatter` allows implementations to specify how tables are formatted
/// and displayed, providing flexibility in presentation.
///
/// # Type Parameters
///
/// - `'data`: The lifetime of the data being formatted.
///
pub trait TableFormatter< 'data >
{
/// Formats the table and writes the result to the provided context.
fn fmt< 'context >( &'data self, c : & mut Context< 'context > ) -> fmt::Result;
/// Converts the table to a string representation.
///
/// # Returns
///
/// A `String` containing the formatted table.
fn table_to_string( &'data self ) -> String
{
self.table_to_string_with_format( &output_format::Table::default() )
}
/// Converts the table to a string representation specifying printer.
///
/// # Returns
///
/// A `String` containing the formatted table.
fn table_to_string_with_format< 'context, Styles >( &'data self, styles : &'context Styles ) -> String
where
Styles : TableOutputFormat,
{
let mut output = String::new();
let printer = Printer
{
output_format : styles,
filter_col : Default::default(),
filter_row : Default::default(),
};
let mut context = Context
{
buf : &mut output,
printer,
};
Self::fmt( self, &mut context ).expect( "Table formatting failed" );
output
}
}
/// A trait for formatting tables.
impl< 'data, T, RowKey, Row, CellKey> TableFormatter< 'data >
for AsTable< 'data, T, RowKey, Row, CellKey>
where
Self : TableRows< CellKey = CellKey, RowKey = RowKey, Row = Row >,
Self : TableHeader< CellKey = CellKey >,
RowKey : table::RowKey,
Row : Cells< CellKey>,
CellKey : table::CellKey + ?Sized,
// CellRepr : table::CellRepr,
{
fn fmt< 'a >( &'data self, c : &mut Context< 'a > ) -> fmt::Result
{
InputExtract::extract
(
self,
c.printer.filter_col,
c.printer.filter_row,
| x |
{
c.printer.output_format.extract_write( x, c )
}
)
}
}
/// A struct for extracting and organizing row of table data for formatting.
#[ derive( Debug, Default ) ]
pub struct RowDescriptor
{
/// Index of the row.
pub irow : usize,
/// Height of the row.
pub height : usize,
/// Type of the line: header or regular.
pub typ : LineType,
/// Visibility of the row.
pub vis : bool,
}
/// A struct for extracting and organizing row of table data for formatting.
#[ derive( Debug, Default ) ]
pub struct ColDescriptor< 'label >
{
/// Index of the column.
pub icol : usize,
/// Column width.
pub width : usize,
/// Label of the column.
pub label : &'label str,
}
/// A struct for extracting and organizing table data for formatting.
///
/// `InputExtract` holds metadata and content necessary for formatting tables,
/// including dimensions, column order, and data slices. It facilitates the
/// transformation of raw table data into a structured format suitable for
/// rendering as a table.
///
#[ allow( dead_code ) ]
#[ derive( Debug ) ]
pub struct InputExtract< 'data >
{
/// Multidimensional size in number of columns per table and number of rows per table.
pub mcells : [ usize ; 2 ],
/// Multidimensional size in number of visible columns per table and number of visible rows per table.
pub mcells_vis : [ usize ; 2 ],
/// Multidimensional size in number of character without taking into account grids.
pub mchars : [ usize ; 2 ],
/// Indicates if the table has a header.
pub has_header : bool,
/// Descriptors for each column, including optional title, width, and index.
// width, index
pub col_descriptors : Vec< ColDescriptor< 'data > >,
/// Descriptors for each row, including height.
pub row_descriptors : Vec< RowDescriptor >,
/// Extracted data for each cell, including string content and size.
// string, size,
pub data : Vec< Vec< ( Cow< 'data, str >, [ usize ; 2 ] ) > >, // xxx : use maybe flat vector
}
//
impl< 'data > InputExtract< 'data >
{
/// Returns an iterator over the row descriptors, skipping the header if present.
///
/// This function provides an iterator that yields each row descriptor along with its index.
/// If the table has a header, the first row is skipped, ensuring that iteration starts from
/// the first data row.
///
/// # Returns
///
/// An iterator over tuples containing:
/// - `usize`: The index of the row.
/// - `&RowDescriptor`: A reference to the row descriptor.
///
pub fn rows( & self ) -> impl _IteratorTrait< Item = ( usize, &RowDescriptor ) >
{
self.row_descriptors
.iter()
.enumerate()
.skip( if self.has_header { 1 } else { 0 } )
}
/// Returns an iterator over the header cells, or a default value if no header is present.
///
/// This function provides an iterator that yields each cell in the header row. If the table
/// does not have a header, it returns an iterator over default values, which are empty strings
/// with a size of `[0, 1]`.
///
/// # Returns
///
/// A boxed iterator yielding tuples containing:
/// - `Cow<'data, str>`: A clone-on-write string representing the cell content.
/// - `[usize; 2]`: An array representing the size of the cell.
///
pub fn header( & self ) -> Box< dyn Iterator< Item = ( Cow< 'data, str >, [ usize ; 2 ] ) > + '_ >
{
if self.has_header
{
Box::new( self.data[ 0 ].iter().cloned() )
}
else
{
Box::new( std::iter::repeat( ( Cow::Borrowed( "" ), [ 0, 1 ] ) ).take( self.mcells[ 0 ] ) )
}
}
/// Returns a slice from the header, or an empty string if no header is present.
///
/// # Arguments
///
/// - `icol`: The column index within the header row.
///
/// # Returns
///
/// A string slice representing the header content.
///
pub fn header_slice( & self, icol : usize ) -> & str
{
if self.has_header
{
self.data[ 0 ][ icol ].0.borrow()
}
else
{
""
}
}
/// Extract input data from and collect it in a format consumable by output formatter.
pub fn extract< 'context, Table, RowKey, Row, CellKey>
(
table : &'data Table,
filter_col : &'context ( dyn FilterCol + 'context ),
filter_row : &'context ( dyn FilterRow + 'context ),
callback : impl for< 'a2 > FnOnce( &'a2 InputExtract< 'a2 > ) -> fmt::Result,
)
-> fmt::Result
where
Table : TableRows< RowKey = RowKey, Row = Row, CellKey = CellKey >,
Table : TableHeader< CellKey = CellKey >,
RowKey : table::RowKey,
Row : Cells< CellKey > + 'data,
Row : Cells< CellKey > + 'data,
CellKey : table::CellKey + ?Sized + 'data,
// CellRepr : table::CellRepr,
{
let mut key_to_ikey : HashMap< Cow< 'data, str >, usize > = HashMap::new();
let mut keys_count = 0;
let rows = table.rows().map( | r |
{
let mut unsorted : Vec< ( usize, Cow< 'data, str > ) > = r.cells().map( | ( key, c ) |
{
if !key_to_ikey.contains_key( key.borrow() )
{
key_to_ikey.insert( key.borrow().into(), keys_count );
keys_count += 1;
}
( key_to_ikey[ key.borrow() ], c.unwrap_or( Cow::from( "" ) ) )
} ).collect();
unsorted.sort_by( | ( i1, _ ), ( i2, _ ) | i1.cmp(i2) );
unsorted.into_iter().map( | ( _, c ) | c).collect()
} ).collect();
let has_header = table.header().is_some();
let column_names = match table.header()
{
Some( header ) => header.map( | ( k, _ ) | Cow::from( k.borrow() ) ).collect(),
None => match table.rows().next()
{
Some( r ) => r.cells().map( | ( k, _ ) | Cow::from( k.borrow() ) ).collect(),
None => Vec::new()
}
};
Self::extract_from_raw_table
(
column_names,
has_header,
rows,
filter_col,
filter_row,
callback,
)
}
/// Extract input data from a table that is constructed with vectors and `Cow`s and collect
/// it in a format consumable by output formatter.
///
/// `rows` should not contain header of the table, it will be automatically added if `has_header`
/// is true.
pub fn extract_from_raw_table< 'context >
(
column_names : Vec< Cow< 'data, str > >,
has_header : bool,
rows : Vec< Vec< Cow< 'data, str > > >,
filter_col : &'context ( dyn FilterCol + 'context ),
filter_row : &'context ( dyn FilterRow + 'context ),
callback : impl for< 'a2 > FnOnce( &'a2 InputExtract< 'a2 > ) -> fmt::Result,
) -> fmt::Result
{
// let mcells = table.mcells();
let mut mcells_vis = [ 0 ; 2 ];
let mut mcells = [ 0 ; 2 ];
let mut mchars = [ 0 ; 2 ];
// key width, index
let mut key_to_ikey : HashMap< Cow< 'data, str >, usize > = HashMap::new();
let mut col_descriptors : Vec< ColDescriptor< '_ > > = Vec::with_capacity( mcells[ 0 ] );
let mut row_descriptors : Vec< RowDescriptor > = Vec::with_capacity( mcells[ 1 ] );
let mut data : Vec< Vec< ( Cow< 'data, str >, [ usize ; 2 ] ) > > = Vec::new();
let mut irow : usize = 0;
let filter_col_need_args = filter_col.need_args();
// let filter_row_need_args = filter_row.need_args();
let mut row_add = | row_data : Vec< Cow< 'data, str > >, typ : LineType |
{
irow = row_descriptors.len();
let vis = true;
let height = 1;
let mut row = RowDescriptor { height, typ, vis, irow };
let mut ncol = 0;
let mut ncol_vis = 0;
let fields : Vec< ( Cow< 'data, str >, [ usize ; 2 ] ) > = row_data
.into_iter()
.enumerate()
.filter_map
(
| ( ikey, val ) |
{
let key = &column_names[ ikey ];
let l = col_descriptors.len();
ncol += 1;
if filter_col_need_args
{
if !filter_col.filter_col( key.as_ref() )
{
return None;
}
}
else
{
if !filter_col.filter_col( "" )
{
return None;
}
}
ncol_vis += 1;
let sz = string::size( &val );
key_to_ikey
.entry( key.clone() )
.and_modify( | icol |
{
let col = &mut col_descriptors[ *icol ];
col.width = col.width.max( sz[ 0 ] );
col.label = "";
})
.or_insert_with( ||
{
let icol = l;
let width = sz[ 0 ];
let col = ColDescriptor { width, icol, label : "" };
col_descriptors.push( col );
icol
});
row.height = row.height.max( sz[ 1 ] );
return Some( ( val, sz ) );
}
)
.collect();
mcells[ 0 ] = mcells[ 0 ].max( ncol );
mcells_vis[ 0 ] = mcells_vis[ 0 ].max( ncol_vis );
row.vis = filter_row.filter_row( typ, irow, &fields );
if row.vis
{
mcells_vis[ 1 ] += 1;
}
mcells[ 1 ] += 1;
row_descriptors.push( row );
data.push( fields );
};
// process header first
if has_header
{
row_add( column_names.clone(), LineType::Header );
}
// Collect rows
// key, string, size,
for row in rows
{
// assert!( row.cells().len() <= usize::MAX, "Row of a table has too many cells" );
row_add( row, LineType::Regular );
}
// calculate size in chars
mchars[ 0 ] = col_descriptors.iter().fold( 0, | acc, col | acc + col.width );
mchars[ 1 ] = row_descriptors.iter().fold( 0, | acc, row | acc + if row.vis { row.height } else { 0 } );
let mut x = InputExtract::< '_ >
{
mcells,
mcells_vis,
mchars,
col_descriptors,
row_descriptors,
data,
has_header,
};
if x.data.len() > 0
{
for icol in 0 .. x.col_descriptors.len()
{
x.col_descriptors[ icol ].label = x.data[ 0 ][ icol ].0.as_ref();
}
}
return callback( &x );
}
}
}
#[ allow( unused_imports ) ]
pub use own::*;
/// Own namespace of the module.
#[ allow( unused_imports ) ]
pub mod own
{
use super::*;
#[ doc( inline ) ]
pub use orphan::*;
#[ doc( inline ) ]
pub use private::
{
Context,
Printer,
InputExtract,
RowDescriptor,
ColDescriptor,
};
}
/// Orphan namespace of the module.
#[ allow( unused_imports ) ]
pub mod orphan
{
use super::*;
#[ doc( inline ) ]
pub use exposed::*;
#[ doc( inline ) ]
pub use private::
{
};
}
/// Exposed namespace of the module.
#[ allow( unused_imports ) ]
pub mod exposed
{
use super::*;
pub use super::super::print;
#[ doc( inline ) ]
pub use private::
{
TableFormatter,
};
}
/// Prelude to use essentials: `use my_module::prelude::*`.
#[ allow( unused_imports ) ]
pub mod prelude
{
use super::*;
}
//