Skip to content

Commit 3b72728

Browse files
radik878onbjerg
andauthored
feat(forge): implement forge snapshot --format table stdout rendering (#11930)
* feat(forge): implement forge snapshot --format table stdout rendering * Update crates/forge/src/cmd/snapshot.rs Co-authored-by: onbjerg <[email protected]> --------- Co-authored-by: onbjerg <[email protected]>
1 parent 3fc0267 commit 3b72728

File tree

1 file changed

+34
-1
lines changed

1 file changed

+34
-1
lines changed

crates/forge/src/cmd/snapshot.rs

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,12 @@ use super::test;
22
use crate::result::{SuiteTestResult, TestKindReport, TestOutcome};
33
use alloy_primitives::{U256, map::HashMap};
44
use clap::{Parser, ValueHint, builder::RangedU64ValueParser};
5+
use comfy_table::{
6+
Cell, Color, Row, Table, modifiers::UTF8_ROUND_CORNERS, presets::ASCII_MARKDOWN,
7+
};
58
use eyre::{Context, Result};
69
use foundry_cli::utils::STATIC_FUZZ_SEED;
10+
use foundry_common::shell;
711
use regex::Regex;
812
use std::{
913
cmp::Ordering,
@@ -111,13 +115,17 @@ impl GasSnapshotArgs {
111115
std::process::exit(1)
112116
}
113117
} else {
118+
if matches!(self.format, Some(Format::Table)) {
119+
let table = build_gas_snapshot_table(&tests);
120+
sh_println!("\n{}", table)?;
121+
}
114122
write_to_gas_snapshot_file(&tests, self.snap, self.format)?;
115123
}
116124
Ok(())
117125
}
118126
}
119127

120-
// TODO implement pretty tables
128+
// Gas report format on stdout.
121129
#[derive(Clone, Debug)]
122130
pub enum Format {
123131
Table,
@@ -290,6 +298,31 @@ fn write_to_gas_snapshot_file(
290298
Ok(fs::write(path, content)?)
291299
}
292300

301+
fn build_gas_snapshot_table(tests: &[SuiteTestResult]) -> Table {
302+
let mut table = Table::new();
303+
if shell::is_markdown() {
304+
table.load_preset(ASCII_MARKDOWN);
305+
} else {
306+
table.apply_modifier(UTF8_ROUND_CORNERS);
307+
}
308+
309+
table.set_header(vec![
310+
Cell::new("Contract").fg(Color::Cyan),
311+
Cell::new("Signature").fg(Color::Cyan),
312+
Cell::new("Report").fg(Color::Cyan),
313+
]);
314+
315+
for test in tests {
316+
let mut row = Row::new();
317+
row.add_cell(Cell::new(test.contract_name()));
318+
row.add_cell(Cell::new(&test.signature));
319+
row.add_cell(Cell::new(test.result.kind.report().to_string()));
320+
table.add_row(row);
321+
}
322+
323+
table
324+
}
325+
293326
/// A Gas snapshot entry diff.
294327
#[derive(Clone, Debug, PartialEq, Eq)]
295328
pub struct GasSnapshotDiff {

0 commit comments

Comments
 (0)