From 053662ad98e4c9846c1db818d764c38eca3d9b4a Mon Sep 17 00:00:00 2001 From: Ethan-000 Date: Mon, 14 Aug 2023 13:19:14 +0100 Subject: [PATCH] chore: `nargo info` now prints information as a prettified table (#2282) --- Cargo.lock | 74 ++++++++++++++++++++++++++++ crates/nargo_cli/Cargo.toml | 3 +- crates/nargo_cli/build.rs | 6 ++- crates/nargo_cli/src/cli/info_cmd.rs | 62 ++++++++++++++++++----- cspell.json | 2 + 5 files changed, 132 insertions(+), 15 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index d9e30e08156..053ee03771b 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1061,6 +1061,27 @@ dependencies = [ "typenum", ] +[[package]] +name = "csv" +version = "1.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "626ae34994d3d8d668f4269922248239db4ae42d538b14c398b74a52208e8086" +dependencies = [ + "csv-core", + "itoa", + "ryu", + "serde", +] + +[[package]] +name = "csv-core" +version = "0.1.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2b2466559f260f48ad25fe6317b3c8dac77b5bdb5763ac7d9d6103530663bc90" +dependencies = [ + "memchr", +] + [[package]] name = "darling" version = "0.20.3" @@ -1151,6 +1172,16 @@ dependencies = [ "dirs-sys", ] +[[package]] +name = "dirs-next" +version = "2.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b98cf8ebf19c3d1b223e151f99a4f9f0690dca41414773390fc824184ac833e1" +dependencies = [ + "cfg-if", + "dirs-sys-next", +] + [[package]] name = "dirs-sys" version = "0.3.7" @@ -1162,6 +1193,17 @@ dependencies = [ "winapi", ] +[[package]] +name = "dirs-sys-next" +version = "0.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4ebda144c4fe02d1f7ea1a7d9641b6fc6b580adcfa024ae48797ecdeb6825b4d" +dependencies = [ + "libc", + "redox_users", + "winapi", +] + [[package]] name = "doc-comment" version = "0.3.3" @@ -1206,6 +1248,12 @@ dependencies = [ "zeroize", ] +[[package]] +name = "encode_unicode" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "34aa73646ffb006b8f5147f3dc182bd4bcb190227ce861fc4a4844bf8e3cb2c0" + [[package]] name = "encoding_rs" version = "0.8.32" @@ -2179,6 +2227,7 @@ dependencies = [ "paste", "pprof", "predicates 2.1.5", + "prettytable-rs", "rustc_version", "serde", "serde_json", @@ -2615,6 +2664,20 @@ dependencies = [ "termtree", ] +[[package]] +name = "prettytable-rs" +version = "0.10.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "eea25e07510aa6ab6547308ebe3c036016d162b8da920dbb079e3ba8acf3d95a" +dependencies = [ + "csv", + "encode_unicode", + "is-terminal", + "lazy_static", + "term", + "unicode-width", +] + [[package]] name = "proc-macro-error" version = "1.0.4" @@ -3584,6 +3647,17 @@ dependencies = [ "windows-sys 0.48.0", ] +[[package]] +name = "term" +version = "0.7.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c59df8ac95d96ff9bede18eb7300b0fda5e5d8d90960e76f8e14ae765eedbf1f" +dependencies = [ + "dirs-next", + "rustversion", + "winapi", +] + [[package]] name = "termcolor" version = "1.2.0" diff --git a/crates/nargo_cli/Cargo.toml b/crates/nargo_cli/Cargo.toml index 96d218d1593..13205c00022 100644 --- a/crates/nargo_cli/Cargo.toml +++ b/crates/nargo_cli/Cargo.toml @@ -32,13 +32,14 @@ acvm.workspace = true toml.workspace = true serde.workspace = true serde_json.workspace = true +prettytable-rs = "0.10" thiserror.workspace = true tower.workspace = true async-lsp = { version = "0.0.5", default-features = false, features = [ "client-monitor", "stdio", "tracing", - "tokio" + "tokio", ] } const_format = "0.2.30" hex = "0.4.2" diff --git a/crates/nargo_cli/build.rs b/crates/nargo_cli/build.rs index 6d8dae136e2..68ae2b31efd 100644 --- a/crates/nargo_cli/build.rs +++ b/crates/nargo_cli/build.rs @@ -110,7 +110,11 @@ fn compile_success_{test_name}() {{ cmd.arg("info"); // `compile_success` tests should be able to compile down to an empty circuit. - cmd.assert().stdout(predicate::str::contains("Total ACIR opcodes generated for language PLONKCSat {{ width: 3 }}: 0")); + cmd.assert().stdout(predicate::str::contains("| Package") + .and(predicate::str::contains("| Language")) + .and(predicate::str::contains("| ACIR Opcodes | Backend Circuit Size |")) + .and(predicate::str::contains("| PLONKCSat {{ width: 3 }} |")) + .and(predicate::str::contains("| 0 | 1 |"))); }} "#, test_dir = test_dir.display(), diff --git a/crates/nargo_cli/src/cli/info_cmd.rs b/crates/nargo_cli/src/cli/info_cmd.rs index d972aeb0511..a2900a6e249 100644 --- a/crates/nargo_cli/src/cli/info_cmd.rs +++ b/crates/nargo_cli/src/cli/info_cmd.rs @@ -5,6 +5,7 @@ use nargo::{package::Package, prepare_package}; use nargo_toml::{find_package_manifest, resolve_workspace_from_toml}; use noirc_driver::{compile_contracts, CompileOptions}; use noirc_frontend::graph::CrateName; +use prettytable::{row, Table}; use crate::{cli::compile_cmd::compile_package, errors::CliError}; @@ -36,14 +37,44 @@ pub(crate) fn run( let toml_path = find_package_manifest(&config.program_dir)?; let workspace = resolve_workspace_from_toml(&toml_path, args.package)?; + let mut package_table = Table::new(); + package_table.add_row( + row![Fm->"Package", Fm->"Language", Fm->"ACIR Opcodes", Fm->"Backend Circuit Size"], + ); + let mut contract_table = Table::new(); + contract_table.add_row(row![ + Fm->"Contract", + Fm->"Function", + Fm->"Language", + Fm->"ACIR Opcodes", + Fm->"Backend Circuit Size" + ]); + for package in &workspace { if package.is_contract() { - count_opcodes_and_gates_in_contracts(backend, package, &args.compile_options)?; + count_opcodes_and_gates_in_contracts( + backend, + package, + &args.compile_options, + &mut contract_table, + )?; } else { - count_opcodes_and_gates_in_package(backend, package, &args.compile_options)?; + count_opcodes_and_gates_in_package( + backend, + package, + &args.compile_options, + &mut package_table, + )?; } } + if package_table.len() > 1 { + package_table.printstd(); + } + if contract_table.len() > 1 { + contract_table.printstd(); + } + Ok(()) } @@ -51,22 +82,21 @@ fn count_opcodes_and_gates_in_package( backend: &B, package: &Package, compile_options: &CompileOptions, + table: &mut Table, ) -> Result<(), CliError> { let (_, compiled_program) = compile_package(backend, package, compile_options)?; let num_opcodes = compiled_program.circuit.opcodes.len(); - - println!( - "[{}] Total ACIR opcodes generated for language {:?}: {}", - package.name, - backend.np_language(), - num_opcodes - ); - let exact_circuit_size = backend .get_exact_circuit_size(&compiled_program.circuit) .map_err(CliError::ProofSystemCompilerError)?; - println!("[{}] Backend circuit size: {exact_circuit_size}", package.name); + + table.add_row(row![ + Fm->format!("{}", package.name), + format!("{:?}", backend.np_language()), + Fc->format!("{}", num_opcodes), + Fc->format!("{}", exact_circuit_size), + ]); Ok(()) } @@ -75,6 +105,7 @@ fn count_opcodes_and_gates_in_contracts( backend: &B, package: &Package, compile_options: &CompileOptions, + table: &mut Table, ) -> Result<(), CliError> { let (mut context, crate_id) = prepare_package(package); let result = compile_contracts(&mut context, crate_id, compile_options); @@ -92,8 +123,13 @@ fn count_opcodes_and_gates_in_contracts( .map_err(CliError::ProofSystemCompilerError)?; for info in function_info { - println!("[{}]({}) Total ACIR opcodes generated: {}", contract.name, info.0, info.1,); - println!("[{}]({}) Backend circuit size: {}", contract.name, info.0, info.2); + table.add_row(row![ + Fm->format!("{}", contract.name), + Fc->format!("{}", info.0), + format!("{:?}", backend.np_language()), + Fc->format!("{}", info.1), + Fc->format!("{}", info.2), + ]); } } diff --git a/cspell.json b/cspell.json index 5629742faee..9953b6f0cd0 100644 --- a/cspell.json +++ b/cspell.json @@ -53,6 +53,8 @@ "peekable", "pprof", "preprocess", + "prettytable", + "printstd", "pseudocode", "schnorr", "sdiv",