Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 20 additions & 3 deletions module/core/format_tools/src/format/output_format/table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,8 @@ pub struct Table
pub corner_lb : char,
/// Bottom-right corner character.
pub corner_rb : char,
/// Limit table size (0 - no limit).
pub max_width : usize,
}

impl Default for Table
Expand Down Expand Up @@ -103,6 +105,8 @@ impl Default for Table
let corner_lb = '└';
let corner_rb = '┘';

let max_width = 50;

Self
{
delimitting_header,
Expand All @@ -123,6 +127,7 @@ impl Default for Table
corner_rt,
corner_lb,
corner_rb,
max_width,
}
}
}
Expand Down Expand Up @@ -192,6 +197,17 @@ impl TableOutputFormat for Table

// dbg!( x.row_descriptors.len() );

let table_width: usize = x.col_descriptors.iter().map( |col| col.width ).sum();

let shrink_factor = if self.max_width == 0 || table_width <= self.max_width
{
1.0
}
else
{
( self.max_width as f32 ) / ( table_width as f32 )
};

for ( irow, row ) in x.row_descriptors.iter().enumerate()
{
let height = row.height;
Expand All @@ -203,7 +219,8 @@ impl TableOutputFormat for Table
if prev_typ == LineType::Header && row.typ == LineType::Regular
{
write!( c.buf, "{}", row_separator )?;
write!( c.buf, "{}", h.repeat( row_width ) )?;
let new_row_width = ( ( row_width as f32 ) * shrink_factor ).floor() as usize;
write!( c.buf, "{}", h.repeat( new_row_width ) )?;
delimitting_header = false
}
}
Expand Down Expand Up @@ -234,7 +251,7 @@ impl TableOutputFormat for Table
{
let col = &x.col_descriptors[ icol ];
let cell_width = x.data[ irow ][ icol ].1[0];
let width = col.width;
let width = ( ( col.width as f32 ) * shrink_factor ).floor() as usize;
let md_index = [ islice, icol, irow as usize ];
let slice = x.slices[ x.slices_dim.md_offset( md_index ) ];

Expand All @@ -247,7 +264,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,
/// Table config of the report.
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,
/// Table config of the report.
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
11 changes: 6 additions & 5 deletions module/move/assistant/src/actions/openai_runs_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::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,
/// Table config of the report.
pub table_config : TableConfig,

/// Current OpenAI runs.
pub runs : Vec< RunObjectWrap >
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
19 changes: 2 additions & 17 deletions module/move/assistant/src/bin/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,31 +12,16 @@ use std::
use dotenv::dotenv;
use clap::Parser;

use assistant::
{
client::client,
commands::{ Cli, CliCommand, self },
Secret
};
use assistant::commands::{ Cli, self };

#[ tokio::main ]
async fn main() -> Result< (), Box< dyn Error > >
{
dotenv().ok();

let secret = Secret::load()?;

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

let cli = Cli::parse();

match cli.command
{
CliCommand::OpenAi( openai_command ) =>
{
commands::openai::command( &client, openai_command ).await;
}
}
commands::execute(cli).await?;

Ok( () )
}
43 changes: 42 additions & 1 deletion module/move/assistant/src/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
mod private
{

use std::error::Error;

use clap::{ Parser, Subcommand };

use crate::*;
Expand All @@ -17,7 +19,26 @@ mod private
{
/// Root of the CLI commands.
#[ command ( subcommand ) ]
pub command : CliCommand,
pub subcommand : CliCommand,

/// Table view configuration.
#[ command ( flatten ) ]
pub table_config : TableConfig,
}

const DEFAULT_MAX_TABLE_WIDTH: usize = 130;

/// Table view configuration.
#[ derive( Debug, Parser ) ]
pub struct TableConfig
{
/// Show records as separate tables.
#[ arg( long, default_value_t = false ) ]
pub as_records : bool,

/// Limit table width.
#[ arg( long, default_value_t = DEFAULT_MAX_TABLE_WIDTH ) ]
pub max_table_width : usize,
}

/// Root of the CLI commands.
Expand All @@ -29,6 +50,24 @@ mod private
OpenAi( openai::Command ),
}

/// Execute CLI command.
pub async fn execute( command : Cli ) -> Result< (), Box< dyn Error > >
{
let secret = Secret::load()?;

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

match command.subcommand
{
CliCommand::OpenAi( openai_command ) =>
{
commands::openai::command( &client, openai_command, command.table_config ).await;
}
}

Ok( () )
}

}

crate::mod_interface!
Expand All @@ -45,5 +84,7 @@ crate::mod_interface!
{
Cli,
CliCommand,
TableConfig,
execute,
};
}
9 changes: 5 additions & 4 deletions module/move/assistant/src/commands/openai.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, openai_files, openai_runs };
use commands::{ openai_assistants, openai_files, openai_runs, TableConfig };

/// OpenAI API commands.
#[ derive ( Debug, Subcommand ) ]
Expand Down Expand Up @@ -42,23 +42,24 @@ mod private
(
client : &Client,
command : Command,
table_config : TableConfig,
)
{
match command
{
Command::Assistants( assistants_command ) =>
{
openai_assistants::command( client, assistants_command ).await;
openai_assistants::command( client, assistants_command, table_config ).await;
}

Command::Files( files_command ) =>
{
openai_files::command( client, files_command ).await;
openai_files::command( client, files_command, table_config ).await;
}

Command::Runs( runs_command ) =>
{
openai_runs::command( client, runs_command ).await;
openai_runs::command( client, runs_command, table_config ).await;
}
}
}
Expand Down
14 changes: 5 additions & 9 deletions module/move/assistant/src/commands/openai_assistants.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,33 +9,29 @@ mod private

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

/// OpenAI assistants.
#[ derive ( Debug, Subcommand ) ]
pub enum Command
{
/// List OpenAI assistants.
List
{
/// Show records as separate tables.
#[ arg( long, default_value_t = false ) ]
show_records_as_tables : bool
},
List,
}

/// Execute OpenAI command related to assistants.
pub async fn command
(
client : &Client,
command : Command,
table_config : TableConfig,
)
{
match command
{
Command::List{ show_records_as_tables } =>
Command::List =>
{
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
Loading
Loading