diff --git a/Cargo.lock b/Cargo.lock index 9b856ba..363fc0d 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -861,6 +861,15 @@ dependencies = [ "windows-sys 0.61.2", ] +[[package]] +name = "humansize" +version = "2.1.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6cb51c9a029ddc91b07a787f1d86b53ccfa49b0e86688c946ebe8d3555685dd7" +dependencies = [ + "libm", +] + [[package]] name = "icu_collections" version = "2.1.1" @@ -1205,6 +1214,7 @@ dependencies = [ "dialoguer", "futures-util", "git2", + "humansize", "indoc", "itertools", "landlock", diff --git a/Cargo.toml b/Cargo.toml index b84cddd..5e780b6 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -29,6 +29,7 @@ const_format = "0.2.35" derive_more = { version = "2.1.0", features = ["display", "error"] } dialoguer = "0.12.0" futures-util = "0.3.31" +humansize = "2.1.3" indoc = "2.0.7" itertools = "0.14.0" nix = { version = "0.30.1", features = ["fs", "process", "socket", "user"] } diff --git a/src/client/commands/show_db.rs b/src/client/commands/show_db.rs index e23e70b..4dcd601 100644 --- a/src/client/commands/show_db.rs +++ b/src/client/commands/show_db.rs @@ -26,6 +26,10 @@ pub struct ShowDbArgs { /// Print the information as JSON #[arg(short, long)] json: bool, + + /// Show sizes in bytes instead of human-readable format + #[arg(short, long)] + bytes: bool, } pub async fn show_databases( @@ -60,7 +64,7 @@ pub async fn show_databases( if args.json { print_list_databases_output_status_json(&databases); } else { - print_list_databases_output_status(&databases); + print_list_databases_output_status(&databases, args.bytes); if databases.iter().any(|(_, res)| { matches!( diff --git a/src/core/protocol/commands/list_databases.rs b/src/core/protocol/commands/list_databases.rs index fdecd1d..9162a2d 100644 --- a/src/core/protocol/commands/list_databases.rs +++ b/src/core/protocol/commands/list_databases.rs @@ -30,7 +30,10 @@ pub enum ListDatabasesError { MySqlError(String), } -pub fn print_list_databases_output_status(output: &ListDatabasesResponse) { +pub fn print_list_databases_output_status( + output: &ListDatabasesResponse, + display_size_as_bytes: bool, +) { let mut final_database_list: Vec<&DatabaseRow> = Vec::new(); for (db_name, db_result) in output { match db_result { @@ -52,7 +55,11 @@ pub fn print_list_databases_output_status(output: &ListDatabasesResponse) { "Users", "Collation", "Character Set", - "Size (Bytes)" + if display_size_as_bytes { + "Size (Bytes)" + } else { + "Size" + } ]); for db in final_database_list { table.add_row(row![ @@ -61,7 +68,11 @@ pub fn print_list_databases_output_status(output: &ListDatabasesResponse) { db.users.iter().map(|user| user.as_str()).join("\n"), db.collation.as_deref().unwrap_or("N/A"), db.character_set.as_deref().unwrap_or("N/A"), - db.size_bytes, + if display_size_as_bytes { + db.size_bytes.to_string() + } else { + humansize::format_size(db.size_bytes, humansize::DECIMAL) + } ]); }