Skip to content

Commit

Permalink
Refactor table formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
InAnYan committed Nov 11, 2024
1 parent c055473 commit 5e97f45
Show file tree
Hide file tree
Showing 12 changed files with 65 additions and 43 deletions.
2 changes: 1 addition & 1 deletion module/core/format_tools/src/format/output_format/table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,7 @@ impl TableOutputFormat for Table

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

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

let lspaces = if cell_width > width {
0
Expand Down
11 changes: 6 additions & 5 deletions module/move/assistant/src/actions/openai_assistants_list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,14 @@ mod private
use client::Client;
use debug::AssistantObjectWrap;
use actions::openai::Result;
use commands::TableConfig;

/// Report for `openai assistants list`.
#[ derive( Debug ) ]
pub struct ListReport
{
/// Show records as separate tables.
pub show_records_as_tables : bool,
/// Configure table formatting.
pub table_config : TableConfig,

/// OpenAI assistants.
pub assistants: Vec< AssistantObjectWrap >
Expand All @@ -38,7 +39,7 @@ mod private
f : &mut fmt::Formatter< '_ >
) -> fmt::Result
{
if self.show_records_as_tables
if self.table_config.as_records
{
writeln!(f, "{}", AsTable::new( &self.assistants ).table_to_string_with_format( &output_format::Records::default() ) )
}
Expand All @@ -53,12 +54,12 @@ mod private
pub async fn action
(
client : &Client,
show_records_as_tables : bool,
table_config : TableConfig,
) -> Result < ListReport >
{
let response = client.list_assistant( None, None, None, None ).await?;
let assistants = response.data.into_iter().map( AssistantObjectWrap ).collect();
Ok( ListReport { show_records_as_tables, assistants } )
Ok( ListReport { table_config, assistants } )
}
}

Expand Down
11 changes: 6 additions & 5 deletions module/move/assistant/src/actions/openai_files_list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,14 @@ mod private
use client::Client;
use debug::FileDataWrap;
use actions::openai::Result;
use commands::TableConfig;

/// Report for `openai files list`.
#[ derive( Debug ) ]
pub struct ListReport
{
/// Show records as separate tables.
pub show_records_as_tables : bool,
/// Configure table formatting.
pub table_config : TableConfig,

/// Files in OpenAI.
pub files : Vec< FileDataWrap >
Expand All @@ -38,7 +39,7 @@ mod private
f : &mut fmt::Formatter< '_ >
) -> fmt::Result
{
if self.show_records_as_tables
if self.table_config.as_records
{
writeln!(f, "{}", AsTable::new( &self.files ).table_to_string_with_format( &output_format::Records::default() ) )
}
Expand All @@ -53,12 +54,12 @@ mod private
pub async fn action
(
client : &Client,
show_records_as_tables : bool,
table_config : TableConfig,
) -> Result < ListReport >
{
let response = client.file_list().await?;
let files = response.data.into_iter().map( FileDataWrap ).collect();
Ok( ListReport { show_records_as_tables, files } )
Ok( ListReport { table_config, files } )
}

}
Expand Down
13 changes: 7 additions & 6 deletions module/move/assistant/src/actions/openai_runs_list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,17 @@ mod private
use client::Client;
use debug::RunObjectWrap;
use actions::openai::Result;
use commands::TableConfig;

/// Report for `openai runs list`.
#[ derive( Debug ) ]
pub struct ListReport
{
/// Show records as separate tables.
pub show_records_as_tables : bool,
/// Configure table formatting.
pub table_config : TableConfig,

/// Current OpenAI runs.
pub runs : Vec< RunObjectWrap >
pub runs : Vec< RunObjectWrap >,
}

impl fmt::Display for ListReport
Expand All @@ -38,7 +39,7 @@ mod private
f : &mut fmt::Formatter< '_ >
) -> fmt::Result
{
if self.show_records_as_tables
if self.table_config.as_records
{
writeln!(f, "{}", AsTable::new( &self.runs ).table_to_string_with_format( &output_format::Records::default() ) )
}
Expand All @@ -54,12 +55,12 @@ mod private
(
client : &Client,
thread_id : String,
show_records_as_tables : bool,
table_config : TableConfig,
) -> Result < ListReport >
{
let response = client.list_run( thread_id, None, None, None, None ).await?;
let runs = response.data.into_iter().map( RunObjectWrap ).collect();
Ok( ListReport { show_records_as_tables, runs } )
Ok( ListReport { table_config, runs } )
}

}
Expand Down
2 changes: 1 addition & 1 deletion module/move/assistant/src/bin/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ async fn main() -> Result< (), Box< dyn Error > >

let secret = Secret::load()?;

let client = client::client( &secret )?;
let client = client( &secret )?;

let cli = Cli::parse();

Expand Down
16 changes: 16 additions & 0 deletions module/move/assistant/src/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,21 @@ mod private
OpenAi( openai::Command ),
}

const DEFAULT_MAX_TABLE_WIDTH: usize = 130;

/// Common collection of arguments for formatting tabular data.
#[ derive( Debug, Parser ) ]
pub struct TableConfig
{
/// Limit table widht.
#[ arg( long, default_value_t = DEFAULT_MAX_TABLE_WIDTH ) ]
pub max_table_width : usize,

/// Show records as separate tables.
#[ arg( long, default_value_t = false ) ]
pub as_records : bool,
}

}

crate::mod_interface!
Expand All @@ -45,5 +60,6 @@ crate::mod_interface!
{
Cli,
CliCommand,
TableConfig,
};
}
12 changes: 6 additions & 6 deletions module/move/assistant/src/commands/openai_assistants.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ mod private

use crate::*;
use client::Client;
use commands::openai_assistants_list;
use commands::{ openai_assistants_list, TableConfig };

/// OpenAI assistants.
#[ derive ( Debug, Subcommand ) ]
Expand All @@ -18,9 +18,9 @@ mod private
/// List OpenAI assistants.
List
{
/// Show records as separate tables.
#[ arg( long, default_value_t = false ) ]
show_records_as_tables : bool
/// Configure table formatting.
#[ clap( flatten ) ]
table_config : TableConfig,
},
}

Expand All @@ -33,9 +33,9 @@ mod private
{
match command
{
Command::List{ show_records_as_tables } =>
Command::List{ table_config } =>
{
openai_assistants_list::command( client, show_records_as_tables ).await;
openai_assistants_list::command( client, table_config ).await;
}
}
}
Expand Down
5 changes: 3 additions & 2 deletions module/move/assistant/src/commands/openai_assistants_list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,16 @@ mod private
use crate::*;
use client::Client;
use actions;
use commands::TableConfig;

/// List OpenAI assistants command.
pub async fn command
(
client : &Client,
show_records_as_tables : bool,
table_config : TableConfig,
)
{
let result = actions::openai_assistants_list::action( client, show_records_as_tables ).await;
let result = actions::openai_assistants_list::action( client, table_config ).await;

match result
{
Expand Down
12 changes: 6 additions & 6 deletions module/move/assistant/src/commands/openai_files.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ mod private

use crate::*;
use client::Client;
use commands::openai_files_list;
use commands::{ openai_files_list, TableConfig };

/// OpenAI files.
#[ derive ( Debug, Subcommand ) ]
Expand All @@ -18,9 +18,9 @@ mod private
/// List OpenAI files.
List
{
/// Show records as separate tables.
#[ arg( long, default_value_t = false ) ]
show_records_as_tables : bool
/// Configure table formatting.
#[ clap( flatten ) ]
table_config : TableConfig,
},
}

Expand All @@ -33,9 +33,9 @@ mod private
{
match command
{
Command::List{ show_records_as_tables } =>
Command::List{ table_config } =>
{
openai_files_list::command( client, show_records_as_tables ).await;
openai_files_list::command( client, table_config ).await;
}
}
}
Expand Down
5 changes: 3 additions & 2 deletions module/move/assistant/src/commands/openai_files_list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,16 @@ mod private
use crate::*;
use client::Client;
use actions;
use commands::TableConfig;

/// List files in your OpenAI API.
pub async fn command
(
client : &Client,
show_records_as_tables : bool,
table_config : TableConfig,
)
{
let result = actions::openai_files_list::action( client, show_records_as_tables ).await;
let result = actions::openai_files_list::action( client, table_config ).await;

match result
{
Expand Down
14 changes: 7 additions & 7 deletions module/move/assistant/src/commands/openai_runs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ mod private

use crate::*;
use client::Client;
use commands::openai_runs_list;
use commands::{ openai_runs_list, TableConfig };

/// OpenAI runs.
#[ derive ( Debug, Subcommand ) ]
Expand All @@ -21,10 +21,10 @@ mod private
/// Thread ID.
thread_id : String,

/// Show records as separate tables.
#[ arg( long, default_value_t = false ) ]
show_records_as_tables : bool
},
/// Configure table formatting.
#[ clap( flatten ) ]
table_config : TableConfig,
}
}

/// Execute OpenAI commands related to runs.
Expand All @@ -36,9 +36,9 @@ mod private
{
match command
{
Command::List { thread_id, show_records_as_tables } =>
Command::List { thread_id, table_config } =>
{
openai_runs_list::command( client, thread_id, show_records_as_tables ).await;
openai_runs_list::command( client, thread_id, table_config ).await;
}
}
}
Expand Down
5 changes: 3 additions & 2 deletions module/move/assistant/src/commands/openai_runs_list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,17 @@ mod private
use crate::*;
use client::Client;
use actions;
use commands::TableConfig;

/// List runs in the thread in OpenAI API.
pub async fn command
(
client : &Client,
thread_id : String,
show_records_as_tables : bool,
table_config : TableConfig,
)
{
let result = actions::openai_runs_list::action( client, thread_id, show_records_as_tables ).await;
let result = actions::openai_runs_list::action( client, thread_id, table_config ).await;

match result
{
Expand Down

0 comments on commit 5e97f45

Please sign in to comment.