Skip to content

Commit a4f79e6

Browse files
authored
Merge pull request #1 from Barsik-sus/conflicts
Conflicts
2 parents 8f12f23 + 0567562 commit a4f79e6

File tree

163 files changed

+6771
-982
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

163 files changed

+6771
-982
lines changed

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -492,7 +492,7 @@ version = "~0.2.0"
492492
path = "module/move/sqlx_query"
493493

494494
[workspace.dependencies.deterministic_rand]
495-
version = "~0.5.0"
495+
version = "~0.6.0"
496496
path = "module/move/deterministic_rand"
497497

498498
[workspace.dependencies.crates_tools]

module/core/diagnostics_tools/src/diag/rta.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -289,4 +289,3 @@ pub mod prelude
289289
};
290290

291291
}
292-

module/core/diagnostics_tools/src/lib.rs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@ pub mod own
3131
#[ doc( inline ) ]
3232
pub use orphan::*;
3333
#[ doc( inline ) ]
34-
#[ allow( unused_imports ) ]
3534
pub use super::diag::orphan::*;
3635
}
3736

@@ -54,7 +53,6 @@ pub mod exposed
5453
#[ doc( inline ) ]
5554
pub use prelude::*;
5655
#[ doc( inline ) ]
57-
#[ allow( unused_imports ) ]
5856
pub use super::diag::exposed::*;
5957
}
6058

@@ -65,6 +63,5 @@ pub mod prelude
6563
{
6664
use super::*;
6765
#[ doc( inline ) ]
68-
#[ allow( unused_imports ) ]
6966
pub use super::diag::prelude::*;
7067
}

module/core/format_tools/src/format.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -289,6 +289,7 @@ pub mod string;
289289
pub mod table;
290290
pub mod to_string;
291291
pub mod to_string_with_fallback;
292+
pub mod text_wrap;
292293

293294
/// A strucutre for diagnostic and demonstration purpose.
294295
#[ doc( hidden ) ]
@@ -317,6 +318,7 @@ pub mod own
317318
table::orphan::*,
318319
to_string::orphan::*,
319320
to_string_with_fallback::orphan::*,
321+
text_wrap::orphan::*,
320322
};
321323

322324
}
@@ -369,6 +371,7 @@ pub mod exposed
369371
table::exposed::*,
370372
to_string::exposed::*,
371373
to_string_with_fallback::exposed::*,
374+
text_wrap::exposed::*,
372375
};
373376

374377
}
@@ -391,6 +394,7 @@ pub mod prelude
391394
table::prelude::*,
392395
to_string::prelude::*,
393396
to_string_with_fallback::prelude::*,
397+
text_wrap::prelude::*,
394398
};
395399

396400
}

module/core/format_tools/src/format/as_table.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ mod private
3232
)
3333
where
3434
RowKey : table::RowKey,
35-
Row : Cells< CellKey>,
35+
Row : Cells< CellKey >,
3636
CellKey : table::CellKey + ?Sized,
3737
// CellRepr : table::CellRepr
3838
;
@@ -41,7 +41,7 @@ mod private
4141
AsTable< 'table, Table, RowKey, Row, CellKey>
4242
where
4343
RowKey : table::RowKey,
44-
Row : Cells< CellKey>,
44+
Row : Cells< CellKey >,
4545
CellKey : table::CellKey + ?Sized,
4646
// CellRepr : table::CellRepr,
4747
{
@@ -56,7 +56,7 @@ mod private
5656
for AsTable< 'table, Table, RowKey, Row, CellKey>
5757
where
5858
RowKey : table::RowKey,
59-
Row : Cells< CellKey>,
59+
Row : Cells< CellKey >,
6060
CellKey : table::CellKey + ?Sized,
6161
// CellRepr : table::CellRepr,
6262
{
@@ -70,7 +70,7 @@ mod private
7070
for AsTable< 'table, Table, RowKey, Row, CellKey>
7171
where
7272
RowKey : table::RowKey,
73-
Row : Cells< CellKey>,
73+
Row : Cells< CellKey >,
7474
CellKey : table::CellKey + ?Sized,
7575
// CellRepr : table::CellRepr,
7676
{
@@ -86,7 +86,7 @@ mod private
8686
for AsTable< 'table, Table, RowKey, Row, CellKey>
8787
where
8888
RowKey : table::RowKey,
89-
Row : Cells< CellKey>,
89+
Row : Cells< CellKey >,
9090
CellKey : table::CellKey + ?Sized,
9191
// CellRepr : table::CellRepr,
9292
{
@@ -101,7 +101,7 @@ mod private
101101
where
102102
Table : fmt::Debug,
103103
RowKey : table::RowKey,
104-
Row : Cells< CellKey>,
104+
Row : Cells< CellKey >,
105105
CellKey : table::CellKey + ?Sized,
106106
// CellRepr : table::CellRepr,
107107
{
@@ -146,7 +146,7 @@ mod private
146146
for AsTable< 'table, Table, RowKey, Row, CellKey>
147147
where
148148
RowKey : table::RowKey,
149-
Row : Cells< CellKey>,
149+
Row : Cells< CellKey >,
150150
CellKey : table::CellKey + ?Sized,
151151
// CellRepr : table::CellRepr,
152152
Self : Copy,

module/core/format_tools/src/format/output_format.rs

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@
3232
mod private
3333
{
3434

35+
use std::borrow::Cow;
36+
3537
use crate::*;
3638
use print::
3739
{
@@ -78,6 +80,36 @@ mod private
7880
}
7981
}
8082

83+
/// Print table, which is constructed with vectors and `Cow`s, with the
84+
/// specified output formatter.
85+
///
86+
/// This function is useful when you do not want to use `AsTable`, or implement `Fields`, and
87+
/// other traits, but you just have string slices in vectors.
88+
///
89+
/// `rows` should not contain header of the table, it will be automatically added if `has_header`
90+
/// is true.
91+
pub fn vector_table_write< 'data, 'context >
92+
(
93+
column_names : Vec< Cow< 'data, str > >,
94+
has_header : bool,
95+
rows : Vec< Vec< Cow< 'data, str > > >,
96+
c : &mut Context< 'context >,
97+
) -> fmt::Result
98+
{
99+
InputExtract::extract_from_raw_table
100+
(
101+
column_names,
102+
has_header,
103+
rows,
104+
c.printer.filter_col,
105+
c.printer.filter_row,
106+
| x |
107+
{
108+
c.printer.output_format.extract_write( x, c )
109+
}
110+
)
111+
}
112+
81113
}
82114

83115
mod table;
@@ -106,6 +138,7 @@ pub mod own
106138
#[ doc( inline ) ]
107139
pub use private::
108140
{
141+
vector_table_write,
109142
};
110143

111144
}

module/core/format_tools/src/format/output_format/records.rs

Lines changed: 82 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,12 @@
2222
//!
2323
2424
use crate::*;
25-
use md_math::MdOffset;
2625
use print::
2726
{
2827
InputExtract,
2928
Context,
3029
};
30+
use std::borrow::Cow;
3131
use core::
3232
{
3333
fmt,
@@ -59,6 +59,8 @@ pub struct Records
5959
pub cell_postfix : String,
6060
/// Separator used between table columns.
6161
pub cell_separator : String,
62+
/// Limit table width. If the value is zero, then no limitation.
63+
pub max_width: usize,
6264
// /// Horizontal line character.
6365
// pub h : char,
6466
// /// Vertical line character.
@@ -91,6 +93,25 @@ impl Records
9193
static INSTANCE : OnceLock< Records > = OnceLock::new();
9294
INSTANCE.get_or_init( || Records::default() )
9395
}
96+
97+
/// Calculate how much space is minimally needed in order to generate an output with this output formatter.
98+
/// It will be impossible to render tables smaller than the result of `min_width()`.
99+
///
100+
/// This function is similar to `output_format::Table::min_width`, but it does not contain a
101+
/// `column_count` as it always equal to 2, and it aslo uses the `output_format::Records`
102+
/// style parameters.
103+
pub fn min_width
104+
(
105+
&self,
106+
) -> usize
107+
{
108+
// 2 is used here, because `Records` displays 2 columns: keys and values.
109+
self.row_prefix.chars().count()
110+
+ self.row_postfix.chars().count()
111+
+ 2 * ( self.cell_postfix.chars().count() + self.cell_prefix.chars().count() )
112+
+ self.cell_separator.chars().count()
113+
+ 2
114+
}
94115
}
95116

96117
impl Default for Records
@@ -108,6 +129,8 @@ impl Default for Records
108129
let table_postfix = "".to_string();
109130
let table_separator = "\n".to_string();
110131

132+
let max_width = 0;
133+
111134
// let h = '─';
112135
// let v = '|';
113136
// let t_l = '├';
@@ -131,6 +154,7 @@ impl Default for Records
131154
cell_prefix,
132155
cell_postfix,
133156
cell_separator,
157+
max_width,
134158
// h,
135159
// v,
136160
// t_l,
@@ -155,70 +179,88 @@ impl TableOutputFormat for Records
155179
c : & mut Context< 'buf >,
156180
) -> fmt::Result
157181
{
182+
use format::text_wrap::{ text_wrap, width_calculate };
183+
184+
if self.max_width != 0 && self.max_width < self.min_width()
185+
{
186+
return Err( fmt::Error );
187+
}
188+
189+
// 2 because there are only 2 columns: key and value.
190+
let columns_max_width = if self.max_width == 0 { 0 } else { self.max_width - self.min_width() + 2 };
158191

159-
let label_width = x.header().fold( 0, | acc, cell | acc.max( cell.1[ 0 ] ) );
192+
let keys : Vec< ( Cow< 'data, str >, [ usize; 2 ] ) > = x.header().collect();
193+
let keys_width = width_calculate( &keys );
160194

161195
write!( c.buf, "{}", self.table_prefix )?;
162196

163-
let mut first = true;
164-
// Write each record
165-
for ( irow, row ) in x.rows()
166-
{
197+
let mut printed_tables_count = 0;
167198

168-
if !row.vis
199+
for ( itable_descriptor, table_descriptor ) in x.row_descriptors.iter().enumerate()
200+
{
201+
if !table_descriptor.vis || ( x.has_header && itable_descriptor == 0 )
169202
{
170203
continue;
171204
}
172205

173-
if first
174-
{
175-
first = false;
176-
}
177-
else
206+
if printed_tables_count > 0
178207
{
179208
write!( c.buf, "{}", self.table_separator )?;
180209
}
181210

182-
let slice_width = x.data[ irow ].iter().fold( 0, | acc, cell | acc.max( cell.1[ 0 ] ) );
211+
printed_tables_count += 1;
212+
213+
writeln!( c.buf, " = {}", table_descriptor.irow )?;
183214

184-
writeln!( c.buf, " = {}", irow )?;
215+
let values = &x.data[ itable_descriptor ];
216+
let values_width = width_calculate( &values );
185217

186-
for ( icol, _col ) in x.col_descriptors.iter().enumerate()
218+
let table_for_wrapping : Vec< Vec< ( Cow< 'data, str >, [ usize; 2] ) > > =
219+
keys.iter().enumerate().map( | ( ikey, key ) |
187220
{
188-
let cell = &x.data[ irow ][ icol ];
189-
let height = cell.1[ 1 ];
221+
vec![ key.clone(), values[ ikey ].clone() ]
222+
}).collect();
190223

191-
for islice in 0..height
224+
let wrapped_text = text_wrap
225+
(
226+
table_for_wrapping.iter(),
227+
&[ keys_width, values_width ],
228+
columns_max_width,
229+
keys_width + values_width,
230+
);
231+
232+
for ( irow, cols ) in wrapped_text.data.into_iter().enumerate()
233+
{
234+
if irow != 0
192235
{
193-
let label = x.header_slice( islice, icol );
194-
let md_index = [ islice, icol, irow ];
195-
let slice = x.slices[ x.slices_dim.md_offset( md_index ) ];
196-
197-
if icol > 0 || islice > 0
198-
{
199-
write!( c.buf, "{}", self.row_separator )?;
200-
}
201-
202-
write!( c.buf, "{}", self.row_prefix )?;
203-
204-
write!( c.buf, "{}", self.cell_prefix )?;
205-
write!( c.buf, "{:<label_width$}", label )?;
206-
write!( c.buf, "{}", self.cell_postfix )?;
207-
write!( c.buf, "{}", self.cell_separator )?;
208-
write!( c.buf, "{}", self.cell_prefix )?;
209-
write!( c.buf, "{:<slice_width$}", slice )?;
210-
write!( c.buf, "{}", self.cell_postfix )?;
211-
212-
write!( c.buf, "{}", self.row_postfix )?;
236+
write!( c.buf, "{}", self.row_separator )?;
213237
}
214238

215-
}
239+
let key = &cols[ 0 ];
240+
let value = &cols[ 1 ];
241+
242+
let key_width = wrapped_text.column_widthes[ 0 ];
243+
let value_width = wrapped_text.column_widthes[ 1 ];
244+
245+
write!( c.buf, "{}", self.row_prefix )?;
216246

247+
write!( c.buf, "{}", self.cell_prefix )?;
248+
write!( c.buf, "{:<key_width$}", key.content )?;
249+
write!( c.buf, "{}", self.cell_postfix )?;
250+
write!( c.buf, "{}", self.cell_separator )?;
251+
write!( c.buf, "{}", self.cell_prefix )?;
252+
// No need to use `wrap_width` of `WrappedCell`, as `output_format::Records`
253+
// does not center values in cells (they are always left aligned).
254+
write!( c.buf, "{:<value_width$}", value.content )?;
255+
write!( c.buf, "{}", self.cell_postfix )?;
256+
257+
write!( c.buf, "{}", self.row_postfix )?;
258+
}
217259
}
218260

219261
write!( c.buf, "{}", self.table_postfix )?;
220262

221-
Ok(())
263+
Ok( () )
222264
}
223265

224266
}

0 commit comments

Comments
 (0)