Skip to content

Commit 5e97f45

Browse files
author
InAnYan
committed
Refactor table formatting
1 parent c055473 commit 5e97f45

File tree

12 files changed

+65
-43
lines changed

12 files changed

+65
-43
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -247,7 +247,7 @@ impl TableOutputFormat for Table
247247

248248
write!( c.buf, "{}", cell_prefix )?;
249249

250-
println!( "icol : {icol} | irow : {irow} | width : {width} | cell_width : {cell_width} | slice.len() : {}", slice.len() );
250+
// println!( "icol : {icol} | irow : {irow} | width : {width} | cell_width : {cell_width} | slice.len() : {}", slice.len() );
251251

252252
let lspaces = if cell_width > width {
253253
0

module/move/assistant/src/actions/openai_assistants_list.rs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,14 @@ mod private
1818
use client::Client;
1919
use debug::AssistantObjectWrap;
2020
use actions::openai::Result;
21+
use commands::TableConfig;
2122

2223
/// Report for `openai assistants list`.
2324
#[ derive( Debug ) ]
2425
pub struct ListReport
2526
{
26-
/// Show records as separate tables.
27-
pub show_records_as_tables : bool,
27+
/// Configure table formatting.
28+
pub table_config : TableConfig,
2829

2930
/// OpenAI assistants.
3031
pub assistants: Vec< AssistantObjectWrap >
@@ -38,7 +39,7 @@ mod private
3839
f : &mut fmt::Formatter< '_ >
3940
) -> fmt::Result
4041
{
41-
if self.show_records_as_tables
42+
if self.table_config.as_records
4243
{
4344
writeln!(f, "{}", AsTable::new( &self.assistants ).table_to_string_with_format( &output_format::Records::default() ) )
4445
}
@@ -53,12 +54,12 @@ mod private
5354
pub async fn action
5455
(
5556
client : &Client,
56-
show_records_as_tables : bool,
57+
table_config : TableConfig,
5758
) -> Result < ListReport >
5859
{
5960
let response = client.list_assistant( None, None, None, None ).await?;
6061
let assistants = response.data.into_iter().map( AssistantObjectWrap ).collect();
61-
Ok( ListReport { show_records_as_tables, assistants } )
62+
Ok( ListReport { table_config, assistants } )
6263
}
6364
}
6465

module/move/assistant/src/actions/openai_files_list.rs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,14 @@ mod private
1818
use client::Client;
1919
use debug::FileDataWrap;
2020
use actions::openai::Result;
21+
use commands::TableConfig;
2122

2223
/// Report for `openai files list`.
2324
#[ derive( Debug ) ]
2425
pub struct ListReport
2526
{
26-
/// Show records as separate tables.
27-
pub show_records_as_tables : bool,
27+
/// Configure table formatting.
28+
pub table_config : TableConfig,
2829

2930
/// Files in OpenAI.
3031
pub files : Vec< FileDataWrap >
@@ -38,7 +39,7 @@ mod private
3839
f : &mut fmt::Formatter< '_ >
3940
) -> fmt::Result
4041
{
41-
if self.show_records_as_tables
42+
if self.table_config.as_records
4243
{
4344
writeln!(f, "{}", AsTable::new( &self.files ).table_to_string_with_format( &output_format::Records::default() ) )
4445
}
@@ -53,12 +54,12 @@ mod private
5354
pub async fn action
5455
(
5556
client : &Client,
56-
show_records_as_tables : bool,
57+
table_config : TableConfig,
5758
) -> Result < ListReport >
5859
{
5960
let response = client.file_list().await?;
6061
let files = response.data.into_iter().map( FileDataWrap ).collect();
61-
Ok( ListReport { show_records_as_tables, files } )
62+
Ok( ListReport { table_config, files } )
6263
}
6364

6465
}

module/move/assistant/src/actions/openai_runs_list.rs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,17 @@ mod private
1818
use client::Client;
1919
use debug::RunObjectWrap;
2020
use actions::openai::Result;
21+
use commands::TableConfig;
2122

2223
/// Report for `openai runs list`.
2324
#[ derive( Debug ) ]
2425
pub struct ListReport
2526
{
26-
/// Show records as separate tables.
27-
pub show_records_as_tables : bool,
27+
/// Configure table formatting.
28+
pub table_config : TableConfig,
2829

2930
/// Current OpenAI runs.
30-
pub runs : Vec< RunObjectWrap >
31+
pub runs : Vec< RunObjectWrap >,
3132
}
3233

3334
impl fmt::Display for ListReport
@@ -38,7 +39,7 @@ mod private
3839
f : &mut fmt::Formatter< '_ >
3940
) -> fmt::Result
4041
{
41-
if self.show_records_as_tables
42+
if self.table_config.as_records
4243
{
4344
writeln!(f, "{}", AsTable::new( &self.runs ).table_to_string_with_format( &output_format::Records::default() ) )
4445
}
@@ -54,12 +55,12 @@ mod private
5455
(
5556
client : &Client,
5657
thread_id : String,
57-
show_records_as_tables : bool,
58+
table_config : TableConfig,
5859
) -> Result < ListReport >
5960
{
6061
let response = client.list_run( thread_id, None, None, None, None ).await?;
6162
let runs = response.data.into_iter().map( RunObjectWrap ).collect();
62-
Ok( ListReport { show_records_as_tables, runs } )
63+
Ok( ListReport { table_config, runs } )
6364
}
6465

6566
}

module/move/assistant/src/bin/main.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ async fn main() -> Result< (), Box< dyn Error > >
2626

2727
let secret = Secret::load()?;
2828

29-
let client = client::client( &secret )?;
29+
let client = client( &secret )?;
3030

3131
let cli = Cli::parse();
3232

module/move/assistant/src/commands.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,21 @@ mod private
2929
OpenAi( openai::Command ),
3030
}
3131

32+
const DEFAULT_MAX_TABLE_WIDTH: usize = 130;
33+
34+
/// Common collection of arguments for formatting tabular data.
35+
#[ derive( Debug, Parser ) ]
36+
pub struct TableConfig
37+
{
38+
/// Limit table widht.
39+
#[ arg( long, default_value_t = DEFAULT_MAX_TABLE_WIDTH ) ]
40+
pub max_table_width : usize,
41+
42+
/// Show records as separate tables.
43+
#[ arg( long, default_value_t = false ) ]
44+
pub as_records : bool,
45+
}
46+
3247
}
3348

3449
crate::mod_interface!
@@ -45,5 +60,6 @@ crate::mod_interface!
4560
{
4661
Cli,
4762
CliCommand,
63+
TableConfig,
4864
};
4965
}

module/move/assistant/src/commands/openai_assistants.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ mod private
99

1010
use crate::*;
1111
use client::Client;
12-
use commands::openai_assistants_list;
12+
use commands::{ openai_assistants_list, TableConfig };
1313

1414
/// OpenAI assistants.
1515
#[ derive ( Debug, Subcommand ) ]
@@ -18,9 +18,9 @@ mod private
1818
/// List OpenAI assistants.
1919
List
2020
{
21-
/// Show records as separate tables.
22-
#[ arg( long, default_value_t = false ) ]
23-
show_records_as_tables : bool
21+
/// Configure table formatting.
22+
#[ clap( flatten ) ]
23+
table_config : TableConfig,
2424
},
2525
}
2626

@@ -33,9 +33,9 @@ mod private
3333
{
3434
match command
3535
{
36-
Command::List{ show_records_as_tables } =>
36+
Command::List{ table_config } =>
3737
{
38-
openai_assistants_list::command( client, show_records_as_tables ).await;
38+
openai_assistants_list::command( client, table_config ).await;
3939
}
4040
}
4141
}

module/move/assistant/src/commands/openai_assistants_list.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,16 @@ mod private
88
use crate::*;
99
use client::Client;
1010
use actions;
11+
use commands::TableConfig;
1112

1213
/// List OpenAI assistants command.
1314
pub async fn command
1415
(
1516
client : &Client,
16-
show_records_as_tables : bool,
17+
table_config : TableConfig,
1718
)
1819
{
19-
let result = actions::openai_assistants_list::action( client, show_records_as_tables ).await;
20+
let result = actions::openai_assistants_list::action( client, table_config ).await;
2021

2122
match result
2223
{

module/move/assistant/src/commands/openai_files.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ mod private
99

1010
use crate::*;
1111
use client::Client;
12-
use commands::openai_files_list;
12+
use commands::{ openai_files_list, TableConfig };
1313

1414
/// OpenAI files.
1515
#[ derive ( Debug, Subcommand ) ]
@@ -18,9 +18,9 @@ mod private
1818
/// List OpenAI files.
1919
List
2020
{
21-
/// Show records as separate tables.
22-
#[ arg( long, default_value_t = false ) ]
23-
show_records_as_tables : bool
21+
/// Configure table formatting.
22+
#[ clap( flatten ) ]
23+
table_config : TableConfig,
2424
},
2525
}
2626

@@ -33,9 +33,9 @@ mod private
3333
{
3434
match command
3535
{
36-
Command::List{ show_records_as_tables } =>
36+
Command::List{ table_config } =>
3737
{
38-
openai_files_list::command( client, show_records_as_tables ).await;
38+
openai_files_list::command( client, table_config ).await;
3939
}
4040
}
4141
}

module/move/assistant/src/commands/openai_files_list.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,16 @@ mod private
88
use crate::*;
99
use client::Client;
1010
use actions;
11+
use commands::TableConfig;
1112

1213
/// List files in your OpenAI API.
1314
pub async fn command
1415
(
1516
client : &Client,
16-
show_records_as_tables : bool,
17+
table_config : TableConfig,
1718
)
1819
{
19-
let result = actions::openai_files_list::action( client, show_records_as_tables ).await;
20+
let result = actions::openai_files_list::action( client, table_config ).await;
2021

2122
match result
2223
{

0 commit comments

Comments
 (0)