From 5e97f452e6b90ed0bbd24eb9f474d14db171aeab Mon Sep 17 00:00:00 2001 From: InAnYan Date: Mon, 11 Nov 2024 17:11:08 +0200 Subject: [PATCH 1/4] Refactor table formatting --- .../src/format/output_format/table.rs | 2 +- .../src/actions/openai_assistants_list.rs | 11 ++++++----- .../assistant/src/actions/openai_files_list.rs | 11 ++++++----- .../assistant/src/actions/openai_runs_list.rs | 13 +++++++------ module/move/assistant/src/bin/main.rs | 2 +- module/move/assistant/src/commands.rs | 16 ++++++++++++++++ .../assistant/src/commands/openai_assistants.rs | 12 ++++++------ .../src/commands/openai_assistants_list.rs | 5 +++-- .../move/assistant/src/commands/openai_files.rs | 12 ++++++------ .../assistant/src/commands/openai_files_list.rs | 5 +++-- .../move/assistant/src/commands/openai_runs.rs | 14 +++++++------- .../assistant/src/commands/openai_runs_list.rs | 5 +++-- 12 files changed, 65 insertions(+), 43 deletions(-) diff --git a/module/core/format_tools/src/format/output_format/table.rs b/module/core/format_tools/src/format/output_format/table.rs index e96af36f20..73e737218d 100644 --- a/module/core/format_tools/src/format/output_format/table.rs +++ b/module/core/format_tools/src/format/output_format/table.rs @@ -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 diff --git a/module/move/assistant/src/actions/openai_assistants_list.rs b/module/move/assistant/src/actions/openai_assistants_list.rs index 9859761c3b..a6a689c038 100644 --- a/module/move/assistant/src/actions/openai_assistants_list.rs +++ b/module/move/assistant/src/actions/openai_assistants_list.rs @@ -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 > @@ -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() ) ) } @@ -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 } ) } } diff --git a/module/move/assistant/src/actions/openai_files_list.rs b/module/move/assistant/src/actions/openai_files_list.rs index 8a3f371a92..8c17567312 100644 --- a/module/move/assistant/src/actions/openai_files_list.rs +++ b/module/move/assistant/src/actions/openai_files_list.rs @@ -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 > @@ -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() ) ) } @@ -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 } ) } } diff --git a/module/move/assistant/src/actions/openai_runs_list.rs b/module/move/assistant/src/actions/openai_runs_list.rs index 631b6ed4cb..4229d3ff62 100644 --- a/module/move/assistant/src/actions/openai_runs_list.rs +++ b/module/move/assistant/src/actions/openai_runs_list.rs @@ -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 @@ -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() ) ) } @@ -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 } ) } } diff --git a/module/move/assistant/src/bin/main.rs b/module/move/assistant/src/bin/main.rs index adeeaf150f..419030d03b 100644 --- a/module/move/assistant/src/bin/main.rs +++ b/module/move/assistant/src/bin/main.rs @@ -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(); diff --git a/module/move/assistant/src/commands.rs b/module/move/assistant/src/commands.rs index 7a8f56c76c..bfd489cb47 100644 --- a/module/move/assistant/src/commands.rs +++ b/module/move/assistant/src/commands.rs @@ -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! @@ -45,5 +60,6 @@ crate::mod_interface! { Cli, CliCommand, + TableConfig, }; } diff --git a/module/move/assistant/src/commands/openai_assistants.rs b/module/move/assistant/src/commands/openai_assistants.rs index 66e2d057e8..0d941c94ba 100644 --- a/module/move/assistant/src/commands/openai_assistants.rs +++ b/module/move/assistant/src/commands/openai_assistants.rs @@ -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 ) ] @@ -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, }, } @@ -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; } } } diff --git a/module/move/assistant/src/commands/openai_assistants_list.rs b/module/move/assistant/src/commands/openai_assistants_list.rs index 95e1ffb62c..6ce7a80ac4 100644 --- a/module/move/assistant/src/commands/openai_assistants_list.rs +++ b/module/move/assistant/src/commands/openai_assistants_list.rs @@ -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 { diff --git a/module/move/assistant/src/commands/openai_files.rs b/module/move/assistant/src/commands/openai_files.rs index 33e18fea0a..ea72a42ad1 100644 --- a/module/move/assistant/src/commands/openai_files.rs +++ b/module/move/assistant/src/commands/openai_files.rs @@ -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 ) ] @@ -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, }, } @@ -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; } } } diff --git a/module/move/assistant/src/commands/openai_files_list.rs b/module/move/assistant/src/commands/openai_files_list.rs index 04bc83c0c4..6225b9faf2 100644 --- a/module/move/assistant/src/commands/openai_files_list.rs +++ b/module/move/assistant/src/commands/openai_files_list.rs @@ -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 { diff --git a/module/move/assistant/src/commands/openai_runs.rs b/module/move/assistant/src/commands/openai_runs.rs index e5e183b33e..2cf7812000 100644 --- a/module/move/assistant/src/commands/openai_runs.rs +++ b/module/move/assistant/src/commands/openai_runs.rs @@ -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 ) ] @@ -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. @@ -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; } } } diff --git a/module/move/assistant/src/commands/openai_runs_list.rs b/module/move/assistant/src/commands/openai_runs_list.rs index 928e0b473a..6d08d64ed3 100644 --- a/module/move/assistant/src/commands/openai_runs_list.rs +++ b/module/move/assistant/src/commands/openai_runs_list.rs @@ -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 { From 65fc751b780b2950d653d9ca114b77a9a9b440ef Mon Sep 17 00:00:00 2001 From: InAnYan Date: Tue, 12 Nov 2024 15:26:38 +0200 Subject: [PATCH 2/4] Partial refactoring --- .../src/actions/openai_assistants_list.rs | 12 +- .../src/actions/openai_files_list.rs | 12 +- .../assistant/src/actions/openai_runs_list.rs | 12 +- module/move/assistant/src/commands.rs | 29 ++++- module/move/assistant/src/lib.rs | 1 + module/move/assistant/src/util.rs | 10 ++ .../move/assistant/src/util/display_table.rs | 109 ++++++++++++++++++ 7 files changed, 154 insertions(+), 31 deletions(-) create mode 100644 module/move/assistant/src/util.rs create mode 100644 module/move/assistant/src/util/display_table.rs diff --git a/module/move/assistant/src/actions/openai_assistants_list.rs b/module/move/assistant/src/actions/openai_assistants_list.rs index a6a689c038..a980bc0a94 100644 --- a/module/move/assistant/src/actions/openai_assistants_list.rs +++ b/module/move/assistant/src/actions/openai_assistants_list.rs @@ -18,7 +18,8 @@ mod private use client::Client; use debug::AssistantObjectWrap; use actions::openai::Result; - use commands::TableConfig; + use commands::{ TableConfig, TableStyle }; + use util::display_table::display_tabular_data; /// Report for `openai assistants list`. #[ derive( Debug ) ] @@ -39,14 +40,7 @@ mod private f : &mut fmt::Formatter< '_ > ) -> fmt::Result { - if self.table_config.as_records - { - writeln!(f, "{}", AsTable::new( &self.assistants ).table_to_string_with_format( &output_format::Records::default() ) ) - } - else - { - writeln!(f, "{}", AsTable::new( &self.assistants ).table_to_string_with_format( &output_format::Table::default() ) ) - } + display_tabular_data( &self.assistants, f, &self.table_config ) } } diff --git a/module/move/assistant/src/actions/openai_files_list.rs b/module/move/assistant/src/actions/openai_files_list.rs index 8c17567312..2ad4823f37 100644 --- a/module/move/assistant/src/actions/openai_files_list.rs +++ b/module/move/assistant/src/actions/openai_files_list.rs @@ -18,7 +18,8 @@ mod private use client::Client; use debug::FileDataWrap; use actions::openai::Result; - use commands::TableConfig; + use commands::{ TableConfig, TableStyle }; + use util::display_table::display_tabular_data; /// Report for `openai files list`. #[ derive( Debug ) ] @@ -39,14 +40,7 @@ mod private f : &mut fmt::Formatter< '_ > ) -> fmt::Result { - if self.table_config.as_records - { - writeln!(f, "{}", AsTable::new( &self.files ).table_to_string_with_format( &output_format::Records::default() ) ) - } - else - { - writeln!(f, "{}", AsTable::new( &self.files ).table_to_string_with_format( &output_format::Table::default() ) ) - } + display_tabular_data( &self.files, f, &self.table_config ) } } diff --git a/module/move/assistant/src/actions/openai_runs_list.rs b/module/move/assistant/src/actions/openai_runs_list.rs index 4229d3ff62..e317f2da67 100644 --- a/module/move/assistant/src/actions/openai_runs_list.rs +++ b/module/move/assistant/src/actions/openai_runs_list.rs @@ -18,7 +18,8 @@ mod private use client::Client; use debug::RunObjectWrap; use actions::openai::Result; - use commands::TableConfig; + use commands::{ TableConfig, TableStyle }; + use util::display_table::display_tabular_data; /// Report for `openai runs list`. #[ derive( Debug ) ] @@ -39,14 +40,7 @@ mod private f : &mut fmt::Formatter< '_ > ) -> fmt::Result { - if self.table_config.as_records - { - writeln!(f, "{}", AsTable::new( &self.runs ).table_to_string_with_format( &output_format::Records::default() ) ) - } - else - { - writeln!(f, "{}", AsTable::new( &self.runs ).table_to_string_with_format( &output_format::Table::default() ) ) - } + display_tabular_data( &self.runs, f, &self.table_config ) } } diff --git a/module/move/assistant/src/commands.rs b/module/move/assistant/src/commands.rs index bfd489cb47..ae2615ad9a 100644 --- a/module/move/assistant/src/commands.rs +++ b/module/move/assistant/src/commands.rs @@ -6,7 +6,9 @@ mod private { - use clap::{ Parser, Subcommand }; + use clap::{ Parser, Subcommand, ValueEnum }; + + use derive_tools::Display; use crate::*; use commands::openai; @@ -39,9 +41,27 @@ mod private #[ 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, + /// Table style option. + #[ arg( long, default_value = "table" ) ] + pub style : TableStyle, + + /// Filter columns of tabular data. + #[ arg( long, value_delimiter( ',' ) ) ] + pub filter_columns : Vec< String >, + } + + /// Table style. + #[ derive( Debug, Clone, Display, ValueEnum ) ] + pub enum TableStyle + { + /// Show data as an ordinary table. + Table, + + /// Show entities as separate tables. + AsRecords, + + /// Show only columns of the tables, no records. + Columns } } @@ -61,5 +81,6 @@ crate::mod_interface! Cli, CliCommand, TableConfig, + TableStyle, }; } diff --git a/module/move/assistant/src/lib.rs b/module/move/assistant/src/lib.rs index c064213715..5a33e41692 100644 --- a/module/move/assistant/src/lib.rs +++ b/module/move/assistant/src/lib.rs @@ -32,6 +32,7 @@ crate::mod_interface! layer commands; layer actions; layer secret; + layer util; exposed use ::reflect_tools:: { diff --git a/module/move/assistant/src/util.rs b/module/move/assistant/src/util.rs new file mode 100644 index 0000000000..7e34c0fd16 --- /dev/null +++ b/module/move/assistant/src/util.rs @@ -0,0 +1,10 @@ +//! +//! Collection of utility functions for this crate. +//! + +mod private {} + +crate::mod_interface! +{ + layer display_table; +} \ No newline at end of file diff --git a/module/move/assistant/src/util/display_table.rs b/module/move/assistant/src/util/display_table.rs new file mode 100644 index 0000000000..efa6c078f7 --- /dev/null +++ b/module/move/assistant/src/util/display_table.rs @@ -0,0 +1,109 @@ +//! +//! Function for displaying tabular data according to `TableConfig`. +//! + +use std::fmt; + +mod private +{ + + use std:: + { + fmt, + borrow::Cow + }; + + use format_tools:: + { + AsTable, + TableFormatter, + output_format, + print, + TableOutputFormat, + }; + + use crate::*; + use commands::{ TableConfig, TableStyle }; + + /// Function for displaying tabular data according to `TableConfig`. + pub fn display_tabular_data<'a> + ( + data: &impl Fields< &'a str, Option< Cow< 'a, str > > >, + f : &mut fmt::Formatter< '_ >, + table_config : &TableConfig, + ) -> fmt::Result + { + match table_config.style + { + TableStyle::Table => + { + display_table( data, f, &table_config.filter_columns ) + } + + TableStyle::AsRecords => + { + display_records( data, f, &table_config.filter_columns ) + } + + TableStyle::Columns => + { + display_columns( data, f, &table_config.filter_columns ) + } + } + } + + fn display_table<'a> + ( + data : &impl Fields< &'a str, Option< Cow< 'a, str > > >, + f : &mut fmt::Formatter< '_ >, + filter_columns : &Vec< String >, + ) -> fmt::Result + { + display_data( data, f, filter_columns, output_format::Table::default() ) + } + + fn display_records<'a> + ( + data : &impl Fields< &'a str, Option< Cow< 'a, str > > >, + f : &mut fmt::Formatter< '_ >, + filter_columns : &Vec< String >, + ) -> fmt::Result + { + display_data( data, f, filter_columns, output_format::Records::default() ) + } + + fn display_columns<'a> + ( + data : &impl Fields< &'a str, Option< Cow< 'a, str > > >, + f : &mut fmt::Formatter< '_ >, + filter_columns : &Vec< String >, + ) -> fmt::Result + { + display_data( data, f, filter_columns, output_format::Keys::default() ) + } + + fn display_data<'a> + ( + data : &impl Fields< &'a str, Option< Cow< 'a, str > > >, + f : &mut fmt::Formatter< '_ >, + filter_columns : &Vec< String >, + format : impl TableOutputFormat, + ) -> fmt::Result + { + let mut printer = print::Printer::with_format( &format ); + printer.filter_col = &| title : &str | + { + filter_columns.iter().any( |c| c.as_str() == title ) + }; + + let as_table = AsTable::new( &data ); + let mut context = print::Context::new( f, printer ); + TableFormatter::fmt( &as_table, &mut context ) + } + +} + +crate::mod_interface! +{ + own use display_tabular_data; +} \ No newline at end of file From aaa50ff65a372e974c14a061616164be64d49d2a Mon Sep 17 00:00:00 2001 From: InAnYan Date: Tue, 12 Nov 2024 16:49:10 +0200 Subject: [PATCH 3/4] Filter columns --- .../src/actions/openai_assistants_list.rs | 14 +++---- .../src/actions/openai_files_list.rs | 14 +++---- .../assistant/src/actions/openai_runs_list.rs | 14 +++---- .../move/assistant/src/util/display_table.rs | 37 ++++++++----------- 4 files changed, 33 insertions(+), 46 deletions(-) diff --git a/module/move/assistant/src/actions/openai_assistants_list.rs b/module/move/assistant/src/actions/openai_assistants_list.rs index a980bc0a94..3eabffec6b 100644 --- a/module/move/assistant/src/actions/openai_assistants_list.rs +++ b/module/move/assistant/src/actions/openai_assistants_list.rs @@ -7,18 +7,16 @@ mod private use std::fmt; - use format_tools:: - { - AsTable, - TableFormatter, - output_format, - }; + use format_tools::AsTable; use crate::*; use client::Client; + use debug::AssistantObjectWrap; + use actions::openai::Result; - use commands::{ TableConfig, TableStyle }; + + use commands::TableConfig; use util::display_table::display_tabular_data; /// Report for `openai assistants list`. @@ -40,7 +38,7 @@ mod private f : &mut fmt::Formatter< '_ > ) -> fmt::Result { - display_tabular_data( &self.assistants, f, &self.table_config ) + display_tabular_data( &AsTable::new( &self.assistants ), f, &self.table_config ) } } diff --git a/module/move/assistant/src/actions/openai_files_list.rs b/module/move/assistant/src/actions/openai_files_list.rs index 2ad4823f37..ec2c713b92 100644 --- a/module/move/assistant/src/actions/openai_files_list.rs +++ b/module/move/assistant/src/actions/openai_files_list.rs @@ -7,18 +7,16 @@ mod private use std::fmt; - use format_tools:: - { - AsTable, - TableFormatter, - output_format, - }; + use format_tools::AsTable; use crate::*; use client::Client; + use debug::FileDataWrap; + use actions::openai::Result; - use commands::{ TableConfig, TableStyle }; + + use commands::TableConfig; use util::display_table::display_tabular_data; /// Report for `openai files list`. @@ -40,7 +38,7 @@ mod private f : &mut fmt::Formatter< '_ > ) -> fmt::Result { - display_tabular_data( &self.files, f, &self.table_config ) + display_tabular_data( &AsTable::new( &self.files ), f, &self.table_config ) } } diff --git a/module/move/assistant/src/actions/openai_runs_list.rs b/module/move/assistant/src/actions/openai_runs_list.rs index e317f2da67..ee4c0ffc3e 100644 --- a/module/move/assistant/src/actions/openai_runs_list.rs +++ b/module/move/assistant/src/actions/openai_runs_list.rs @@ -7,18 +7,16 @@ mod private use std::fmt; - use format_tools:: - { - AsTable, - TableFormatter, - output_format, - }; + use format_tools::AsTable; use crate::*; use client::Client; + use debug::RunObjectWrap; + use actions::openai::Result; - use commands::{ TableConfig, TableStyle }; + + use commands::TableConfig; use util::display_table::display_tabular_data; /// Report for `openai runs list`. @@ -40,7 +38,7 @@ mod private f : &mut fmt::Formatter< '_ > ) -> fmt::Result { - display_tabular_data( &self.runs, f, &self.table_config ) + display_tabular_data( &AsTable::new( &self.runs ), f, &self.table_config ) } } diff --git a/module/move/assistant/src/util/display_table.rs b/module/move/assistant/src/util/display_table.rs index efa6c078f7..fb73835a3a 100644 --- a/module/move/assistant/src/util/display_table.rs +++ b/module/move/assistant/src/util/display_table.rs @@ -2,20 +2,13 @@ //! Function for displaying tabular data according to `TableConfig`. //! -use std::fmt; - mod private { - use std:: - { - fmt, - borrow::Cow - }; + use std::fmt; use format_tools:: { - AsTable, TableFormatter, output_format, print, @@ -28,9 +21,9 @@ mod private /// Function for displaying tabular data according to `TableConfig`. pub fn display_tabular_data<'a> ( - data: &impl Fields< &'a str, Option< Cow< 'a, str > > >, + data : &'a impl TableFormatter< 'a >, f : &mut fmt::Formatter< '_ >, - table_config : &TableConfig, + table_config : &'a TableConfig, ) -> fmt::Result { match table_config.style @@ -54,9 +47,9 @@ mod private fn display_table<'a> ( - data : &impl Fields< &'a str, Option< Cow< 'a, str > > >, + data : &'a impl TableFormatter< 'a >, f : &mut fmt::Formatter< '_ >, - filter_columns : &Vec< String >, + filter_columns : &'a Vec< String >, ) -> fmt::Result { display_data( data, f, filter_columns, output_format::Table::default() ) @@ -64,9 +57,9 @@ mod private fn display_records<'a> ( - data : &impl Fields< &'a str, Option< Cow< 'a, str > > >, + data : &'a impl TableFormatter< 'a >, f : &mut fmt::Formatter< '_ >, - filter_columns : &Vec< String >, + filter_columns : &'a Vec< String >, ) -> fmt::Result { display_data( data, f, filter_columns, output_format::Records::default() ) @@ -74,9 +67,9 @@ mod private fn display_columns<'a> ( - data : &impl Fields< &'a str, Option< Cow< 'a, str > > >, + data : &'a impl TableFormatter< 'a >, f : &mut fmt::Formatter< '_ >, - filter_columns : &Vec< String >, + filter_columns : &'a Vec< String >, ) -> fmt::Result { display_data( data, f, filter_columns, output_format::Keys::default() ) @@ -84,21 +77,21 @@ mod private fn display_data<'a> ( - data : &impl Fields< &'a str, Option< Cow< 'a, str > > >, + data : &'a impl TableFormatter< 'a >, f : &mut fmt::Formatter< '_ >, - filter_columns : &Vec< String >, + filter_columns : &'a Vec< String >, format : impl TableOutputFormat, ) -> fmt::Result { let mut printer = print::Printer::with_format( &format ); - printer.filter_col = &| title : &str | + let binding = | title : &str | { - filter_columns.iter().any( |c| c.as_str() == title ) + filter_columns.is_empty() || filter_columns.iter().any( |c| c.as_str() == title ) }; + printer.filter_col = &binding; - let as_table = AsTable::new( &data ); let mut context = print::Context::new( f, printer ); - TableFormatter::fmt( &as_table, &mut context ) + TableFormatter::fmt( data, &mut context ) } } From 970a165d680cad70cd9b5bcb2d46b6325d0a1a88 Mon Sep 17 00:00:00 2001 From: InAnYan Date: Mon, 18 Nov 2024 10:33:19 +0200 Subject: [PATCH 4/4] Improve commands --- module/move/assistant/src/actions/openai.rs | 26 +++++++++++- .../src/actions/openai_assistants_list.rs | 4 +- .../src/actions/openai_files_list.rs | 4 +- .../assistant/src/actions/openai_runs_list.rs | 4 +- module/move/assistant/src/commands.rs | 41 ++++++++----------- .../move/assistant/src/util/display_table.rs | 35 ++++++++-------- 6 files changed, 67 insertions(+), 47 deletions(-) diff --git a/module/move/assistant/src/actions/openai.rs b/module/move/assistant/src/actions/openai.rs index 82053c4d47..da8be32030 100644 --- a/module/move/assistant/src/actions/openai.rs +++ b/module/move/assistant/src/actions/openai.rs @@ -13,6 +13,8 @@ mod private use crate::*; use ser::DisplayFromStr; + use commands::TableConfig; + /// Collective enum for errors in OpenAI actions. #[ ser::serde_as ] #[ derive( Debug, Error, AsRefStr, ser::Serialize ) ] @@ -26,12 +28,31 @@ mod private #[ from ] #[ serde_as( as = "DisplayFromStr" ) ] openai_api_rs::v1::error::APIError - ) + ), + + /// User chosen a mix of table styles instead of a single one. + /// E.g.: both `--as-table` and `--as-records` were set, however only one style must be chosen + #[ error( "Select only one table style: either `--as-table`, `--as-records`, or `--columns`" ) ] + WrongTableStyle, } /// Shorthand for `Result` in OpenAI actions. pub type Result< T > = core::result::Result< T, Error >; + /// Check the CLI arguments for table style. + /// There are 3 arguments: `--as-table`, `--as-records`, `--columns`. Only one argument + /// should be active a time. + pub fn check_table_style( table_config: &TableConfig ) -> Result< () > + { + if table_config.as_table && ( table_config.as_records || table_config.columns ) + || table_config.as_records && ( table_config.as_table || table_config.columns ) + || table_config.columns && ( table_config.as_records || table_config.as_table ) + { + return Err( Error::WrongTableStyle ) + } + + Ok( () ) + } } crate::mod_interface! @@ -39,6 +60,7 @@ crate::mod_interface! own use { Error, - Result + Result, + check_table_style, }; } \ No newline at end of file diff --git a/module/move/assistant/src/actions/openai_assistants_list.rs b/module/move/assistant/src/actions/openai_assistants_list.rs index 3eabffec6b..2326ac2a6a 100644 --- a/module/move/assistant/src/actions/openai_assistants_list.rs +++ b/module/move/assistant/src/actions/openai_assistants_list.rs @@ -14,7 +14,7 @@ mod private use debug::AssistantObjectWrap; - use actions::openai::Result; + use actions::openai::{ Result, check_table_style }; use commands::TableConfig; use util::display_table::display_tabular_data; @@ -49,6 +49,8 @@ mod private table_config : TableConfig, ) -> Result < ListReport > { + check_table_style( &table_config )?; + let response = client.list_assistant( None, None, None, None ).await?; let assistants = response.data.into_iter().map( AssistantObjectWrap ).collect(); Ok( ListReport { table_config, assistants } ) diff --git a/module/move/assistant/src/actions/openai_files_list.rs b/module/move/assistant/src/actions/openai_files_list.rs index ec2c713b92..12f6ab7bd0 100644 --- a/module/move/assistant/src/actions/openai_files_list.rs +++ b/module/move/assistant/src/actions/openai_files_list.rs @@ -14,7 +14,7 @@ mod private use debug::FileDataWrap; - use actions::openai::Result; + use actions::openai::{ Result, check_table_style }; use commands::TableConfig; use util::display_table::display_tabular_data; @@ -49,6 +49,8 @@ mod private table_config : TableConfig, ) -> Result < ListReport > { + check_table_style( &table_config )?; + let response = client.file_list().await?; let files = response.data.into_iter().map( FileDataWrap ).collect(); Ok( ListReport { table_config, files } ) diff --git a/module/move/assistant/src/actions/openai_runs_list.rs b/module/move/assistant/src/actions/openai_runs_list.rs index ee4c0ffc3e..d5bf8098c6 100644 --- a/module/move/assistant/src/actions/openai_runs_list.rs +++ b/module/move/assistant/src/actions/openai_runs_list.rs @@ -14,7 +14,7 @@ mod private use debug::RunObjectWrap; - use actions::openai::Result; + use actions::openai::{ Result, check_table_style }; use commands::TableConfig; use util::display_table::display_tabular_data; @@ -50,6 +50,8 @@ mod private table_config : TableConfig, ) -> Result < ListReport > { + check_table_style( &table_config )?; + let response = client.list_run( thread_id, None, None, None, None ).await?; let runs = response.data.into_iter().map( RunObjectWrap ).collect(); Ok( ListReport { table_config, runs } ) diff --git a/module/move/assistant/src/commands.rs b/module/move/assistant/src/commands.rs index ae2615ad9a..7f54ec320b 100644 --- a/module/move/assistant/src/commands.rs +++ b/module/move/assistant/src/commands.rs @@ -6,9 +6,7 @@ mod private { - use clap::{ Parser, Subcommand, ValueEnum }; - - use derive_tools::Display; + use clap::{ Parser, Subcommand }; use crate::*; use commands::openai; @@ -31,39 +29,35 @@ mod private OpenAi( openai::Command ), } - const DEFAULT_MAX_TABLE_WIDTH: usize = 130; + // const DEFAULT_MAX_TABLE_WIDTH: usize = 130; + // Commented out as not yet implemented in `format_tools`. /// 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, + // #[ arg( long, default_value_t = DEFAULT_MAX_TABLE_WIDTH ) ] + // pub max_table_width : usize, + // Commented out as not yet implemented in `format_tools`. + + /// Show tabular data as an ordinary table. + #[ arg( long ) ] + pub as_table : bool, - /// Table style option. - #[ arg( long, default_value = "table" ) ] - pub style : TableStyle, + /// Show each record of a tabular data as a separate table. + #[ arg( long ) ] + pub as_records : bool, + + /// Show only keys (columns) of tabular data. + #[ arg( long ) ] + pub columns : bool, /// Filter columns of tabular data. #[ arg( long, value_delimiter( ',' ) ) ] pub filter_columns : Vec< String >, } - /// Table style. - #[ derive( Debug, Clone, Display, ValueEnum ) ] - pub enum TableStyle - { - /// Show data as an ordinary table. - Table, - - /// Show entities as separate tables. - AsRecords, - - /// Show only columns of the tables, no records. - Columns - } - } crate::mod_interface! @@ -81,6 +75,5 @@ crate::mod_interface! Cli, CliCommand, TableConfig, - TableStyle, }; } diff --git a/module/move/assistant/src/util/display_table.rs b/module/move/assistant/src/util/display_table.rs index fb73835a3a..edced5fbdd 100644 --- a/module/move/assistant/src/util/display_table.rs +++ b/module/move/assistant/src/util/display_table.rs @@ -16,7 +16,7 @@ mod private }; use crate::*; - use commands::{ TableConfig, TableStyle }; + use commands::{ TableConfig }; /// Function for displaying tabular data according to `TableConfig`. pub fn display_tabular_data<'a> @@ -26,23 +26,22 @@ mod private table_config : &'a TableConfig, ) -> fmt::Result { - match table_config.style - { - TableStyle::Table => - { - display_table( data, f, &table_config.filter_columns ) - } - - TableStyle::AsRecords => - { - display_records( data, f, &table_config.filter_columns ) - } - - TableStyle::Columns => - { - display_columns( data, f, &table_config.filter_columns ) - } - } + if table_config.as_table + { + display_table( data, f, &table_config.filter_columns ) + } + else if table_config.as_records + { + display_records( data, f, &table_config.filter_columns ) + } + else if table_config.columns + { + display_columns( data, f, &table_config.filter_columns ) + } + else + { + display_table( data, f, &table_config.filter_columns ) + } } fn display_table<'a>