Skip to content

Commit 8d66c4f

Browse files
committed
fixing format_tools
1 parent e67220c commit 8d66c4f

File tree

6 files changed

+128
-3
lines changed

6 files changed

+128
-3
lines changed

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ mod private
8282

8383
mod table;
8484
mod records;
85+
mod keys;
8586

8687
#[ allow( unused_imports ) ]
8788
pub use own::*;
@@ -99,6 +100,7 @@ pub mod own
99100
{
100101
table::Table,
101102
records::Records,
103+
keys::Keys,
102104
};
103105

104106
#[ doc( inline ) ]
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
//! Implement keys list output format.
2+
//!
3+
//! # Example
4+
//!
5+
//! ```text
6+
//! ```
7+
//!
8+
9+
use crate::*;
10+
use print::
11+
{
12+
InputExtract,
13+
Context,
14+
};
15+
use core::
16+
{
17+
fmt,
18+
};
19+
use std::sync::OnceLock;
20+
21+
/// A struct representing the list of keys output format.
22+
#[derive( Debug )]
23+
pub struct Keys
24+
{
25+
// /// Prefix added to each row.
26+
// pub table_prefix : String,
27+
// /// Postfix added to each row.
28+
// pub table_postfix : String,
29+
// /// Separator used between rows.
30+
// pub table_separator : String,
31+
// /// Prefix added to each row.
32+
// pub row_prefix : String,
33+
// /// Postfix added to each row.
34+
// pub row_postfix : String,
35+
// /// Separator used between rows.
36+
// pub row_separator : String,
37+
// /// Prefix added to each cell.
38+
// pub cell_prefix : String,
39+
// /// Postfix added to each cell.
40+
// pub cell_postfix : String,
41+
// /// Separator used between table columns.
42+
// pub cell_separator : String,
43+
}
44+
45+
impl Keys
46+
{
47+
/// Returns a reference to a static instance of `Keys`.
48+
pub fn instance() -> &'static dyn TableOutputFormat
49+
{
50+
static INSTANCE : OnceLock< Keys > = OnceLock::new();
51+
INSTANCE.get_or_init( || Keys::default() )
52+
}
53+
}
54+
55+
impl Default for Keys
56+
{
57+
fn default() -> Self
58+
{
59+
60+
// let cell_prefix = "".to_string();
61+
// let cell_postfix = "".to_string();
62+
// let cell_separator = " │ ".to_string();
63+
// let row_prefix = "│ ".to_string();
64+
// let row_postfix = " │".to_string();
65+
// let row_separator = "\n".to_string();
66+
// let table_prefix = "".to_string();
67+
// let table_postfix = "".to_string();
68+
// let table_separator = "\n".to_string();
69+
70+
Self
71+
{
72+
// table_prefix,
73+
// table_postfix,
74+
// table_separator,
75+
// row_prefix,
76+
// row_postfix,
77+
// row_separator,
78+
// cell_prefix,
79+
// cell_postfix,
80+
// cell_separator,
81+
}
82+
}
83+
}
84+
85+
impl TableOutputFormat for Keys
86+
{
87+
88+
fn extract_write< 'buf, 'data >(
89+
&self,
90+
x : &InputExtract< 'data >,
91+
c : &mut Context< 'buf >,
92+
) -> fmt::Result
93+
{
94+
95+
// dbg!( &x );
96+
97+
for col in &x.col_descriptors
98+
{
99+
write!( c.buf, " - {}\n", col.label )?;
100+
}
101+
102+
write!( c.buf, " {} fields\n", x.col_descriptors.len() )?;
103+
104+
Ok(())
105+
}
106+
107+
}

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -583,7 +583,10 @@ mod private
583583
slices[ x.slices_dim.md_offset( md_index ) ] = s;
584584
})
585585
;
586-
x.col_descriptors[ icol ].label = cell.0.as_ref();
586+
if irow == 0
587+
{
588+
x.col_descriptors[ icol ].label = cell.0.as_ref();
589+
}
587590
}
588591

589592
}

module/core/format_tools/tests/inc/mod.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,10 @@ mod fundamental
1010

1111
mod table_test;
1212
mod tabe_foreign_test;
13-
mod format_ordinary_test;
13+
14+
mod format_table_test;
1415
mod format_records_test;
16+
// mod format_keys_test; // qqq : xxx : implement
1517

1618
mod collection_test;
1719
mod fields_test;

module/core/format_tools/tests/inc/tabe_foreign_test.rs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ fn iterator_over_objects_without_impl()
9393
assert!( got.contains( "│ 1627845583 │ [ │ │" ) );
9494

9595
let got = AsTable::new( &data ).table_to_string_with_format( &output_format::Table::default() );
96+
println!( "{}", &got );
9697
assert!( got.contains( "│ id │ created_at │ file_ids │ tools │" ) );
9798
assert!( got.contains( "│ 13 │ [ │ [ │" ) );
9899
assert!( got.contains( "│ 1627845583 │ [ │ │" ) );
@@ -106,14 +107,24 @@ fn iterator_over_objects_without_impl()
106107
let mut context = the_module::print::Context::new( &mut output, printer );
107108
let got = the_module::TableFormatter::fmt( &as_table, &mut context );
108109
assert!( got.is_ok() );
109-
println!( "{}", &output );
110110

111111
let got = AsTable::new( &data ).table_to_string_with_format( &output_format::Records::default() );
112+
println!( "{}", &got );
112113
assert!( got.contains( "│ id │ 1 │" ) );
113114
assert!( got.contains( "│ created_at │ 1627845583 │" ) );
114115
assert!( got.contains( "│ id │ 2 │" ) );
115116
assert!( got.contains( "│ created_at │ 13 │" ) );
116117

118+
// = output as keys
119+
120+
let got = AsTable::new( &data ).table_to_string_with_format( &output_format::Keys::default() );
121+
println!( "{}", &got );
122+
assert!( got.contains( "- id" ) );
123+
assert!( got.contains( "- created_at" ) );
124+
assert!( got.contains( "- file_ids" ) );
125+
assert!( got.contains( "- tools" ) );
126+
assert!( got.contains( "4 fields" ) );
127+
117128
// assert!( false );
118129

119130
}

0 commit comments

Comments
 (0)