Skip to content

Commit

Permalink
perf(cli): use less for --output print handling (#196)
Browse files Browse the repository at this point in the history
* perf(cli): use `less` for `--output print` handling

* chore(cli): cleanup
  • Loading branch information
Jon-Becker authored Nov 23, 2023
1 parent 0b330d3 commit c59a84a
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 13 deletions.
26 changes: 14 additions & 12 deletions cli/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
pub(crate) mod output;

use backtrace::Backtrace;
use output::build_output_path;
use output::{build_output_path, print_with_less};
use std::{io, panic};

use clap::{Parser, Subcommand};
Expand Down Expand Up @@ -108,8 +108,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
let assembly = disassemble(cmd.clone()).await?;

if cmd.output == "print" {
// TODO: use `less`
println!("{}", assembly);
print_with_less(&assembly).await?;
} else {
let output_path =
build_output_path(&cmd.output, &cmd.target, &cmd.rpc_url, "disassembled.asm")
Expand All @@ -128,12 +127,19 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
let result = decompile(cmd.clone()).await?;

if cmd.output == "print" {
let mut output_str = String::new();

if let Some(abi) = &result.abi {
println!("ABI:\n\n{}\n", serde_json::to_string_pretty(abi).unwrap());
output_str.push_str(&format!(
"ABI:\n\n{}\n",
serde_json::to_string_pretty(abi).unwrap()
));
}
if let Some(source) = &result.source {
println!("Source:\n\n{}\n", source);
output_str.push_str(&format!("Source:\n\n{}\n", source));
}

print_with_less(&output_str).await?;
} else {
// write the contract ABI
if let Some(abi) = result.abi {
Expand Down Expand Up @@ -206,7 +212,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
let stringified_dot = build_cfg(&cfg, &cmd);

if cmd.output == "print" {
println!("{}", stringified_dot);
print_with_less(&stringified_dot).await?;
} else {
let output_path =
build_output_path(&cmd.output, &cmd.target, &cmd.rpc_url, "cfg.dot").await?;
Expand Down Expand Up @@ -240,9 +246,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
}

if cmd.output == "print" {
for line in &lines {
println!("{}", line);
}
print_with_less(&lines.join("\n")).await?;
} else {
let output_path =
build_output_path(&cmd.output, &cmd.target, &cmd.rpc_url, "dump.csv").await?;
Expand All @@ -265,9 +269,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
);

if cmd.output == "print" {
for line in &csv_lines {
println!("{}", line);
}
print_with_less(&csv_lines.join("\n")).await?;
} else {
let output_path =
build_output_path(&cmd.output, &cmd.target, &cmd.rpc_url, "snapshot.csv")
Expand Down
14 changes: 13 additions & 1 deletion cli/src/output.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::env;
use std::{env, io::Write};

use heimdall_common::{constants::ADDRESS_REGEX, ether::rpc};

Expand Down Expand Up @@ -31,6 +31,18 @@ pub async fn build_output_path(
Ok(format!("{}/{}", output, filename))
}

/// pass the input to the `less` command
pub async fn print_with_less(input: &str) -> Result<(), Box<dyn std::error::Error>> {
let mut child =
std::process::Command::new("less").stdin(std::process::Stdio::piped()).spawn()?;

let stdin = child.stdin.as_mut().unwrap();
stdin.write_all(input.as_bytes())?;

child.wait()?;
Ok(())
}

#[cfg(test)]
mod tests {
use super::*;
Expand Down

0 comments on commit c59a84a

Please sign in to comment.