~taiite/protodump

b942e4801ee04d6afb7a2959161f009aeea727a3 — Hubert Hirtz 9 days ago 5293bee
Change from unmaintained ansi_term to owo-colors
3 files changed, 77 insertions(+), 53 deletions(-)

M Cargo.lock
M Cargo.toml
M src/main.rs
M Cargo.lock => Cargo.lock +26 -11
@@ 9,15 9,6 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f26201604c87b1e01bd3d98f8d5d9a8fcbb815e8cedb41ffccbeb4bf593a35fe"

[[package]]
name = "ansi_term"
version = "0.12.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d52a9bb7ec0cf484c551830a7ce27bd20d67eac647e1befb56b0be4ee39a55d2"
dependencies = [
 "winapi",
]

[[package]]
name = "anyhow"
version = "1.0.42"
source = "registry+https://github.com/rust-lang/crates.io-index"


@@ 103,6 94,12 @@ dependencies = [
]

[[package]]
name = "is_ci"
version = "1.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "616cde7c720bb2bb5824a224687d8f77bfd38922027f01d825cd7453be5099fb"

[[package]]
name = "libc"
version = "0.2.99"
source = "registry+https://github.com/rust-lang/crates.io-index"


@@ 145,17 142,25 @@ dependencies = [
]

[[package]]
name = "owo-colors"
version = "3.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c1b04fb49957986fdce4d6ee7a65027d55d4b6d2265e5848bbb507b58ccfdb6f"
dependencies = [
 "supports-color",
]

[[package]]
name = "protodump"
version = "0.1.0"
dependencies = [
 "ansi_term",
 "anyhow",
 "atty",
 "fallible-iterator",
 "getopts",
 "gimli",
 "memmap",
 "object",
 "owo-colors",
]

[[package]]


@@ 171,6 176,16 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a8f112729512f8e442d81f95a8a7ddf2b7c6b8a1a6f509a95864142b30cab2d3"

[[package]]
name = "supports-color"
version = "1.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4872ced36b91d47bae8a214a683fe54e7078875b399dfa251df346c9b547d1f9"
dependencies = [
 "atty",
 "is_ci",
]

[[package]]
name = "unicode-width"
version = "0.1.8"
source = "registry+https://github.com/rust-lang/crates.io-index"

M Cargo.toml => Cargo.toml +1 -2
@@ 14,8 14,7 @@ getopts = { version = "0.2", default-features = false, features = ["std"] }
anyhow = { version = "1", default-features = false, features = ["std"] }

# Terminal colors
ansi_term = { version = "0.12", default-features = false }
atty = { version = "0.2", default-features = false }
owo-colors = { version = "3.5", default-features = false, features = ["supports-colors"] }

# Read DWARF and object files
fallible-iterator = { version = "0.2", default-features = false, features = [] }

M src/main.rs => src/main.rs +50 -40
@@ 3,9 3,12 @@ use anyhow::Result;
use object::read::Object as _;
use object::read::ObjectSection as _;
use object::read::ObjectSymbol as _;
use owo_colors::OwoColorize;
use owo_colors::Stream;
use std::borrow::Cow;
use std::collections::HashSet;
use std::env;
use std::fmt;
use std::fs;
use std::process;



@@ 127,23 130,18 @@ fn diff_sources<'a>(
    res
}

fn show_source_diff(d: &[(Reader<'_>, dwarf::Subprogram<Reader<'_>>, Diff<'_>)]) {
    let color = atty::is(atty::Stream::Stdout);
    for (func, prototype, diff) in d {
fn show_source_diff<'a>(d: &'a [(Reader<'a>, dwarf::Subprogram<Reader<'a>>, Diff<'a>)]) {
    for (func, p, diff) in d {
        match diff {
            Diff::Added => {
                print!("add ");
                show_prototype(*func, prototype, color);
                println!("add {}", prototype(*func, p));
            }
            Diff::Removed => {
                print!("del ");
                show_prototype(*func, prototype, color);
                println!("del {}", prototype(*func, p));
            }
            Diff::Changed(new_prototype) => {
                print!("prv ");
                show_prototype(*func, prototype, color);
                print!("nxt ");
                show_prototype(*func, new_prototype, color);
            Diff::Changed(new_p) => {
                println!("prv {}", prototype(*func, p));
                println!("nxt {}", prototype(*func, new_p));
            }
        }
    }


@@ 209,46 207,58 @@ fn read_object_info<'a>(obj: &object::File<'a>, filter: SymbolFilter) -> Result<
    })
}

fn show_prototype(name: Reader<'_>, prototype: &dwarf::Subprogram<Reader<'_>>, color: bool) {
    let return_type = prototype.return_type.borrow();
    print!("{}{}", return_type, return_type.display_padding());
    if color {
        print!(
struct Prototype<'a, 'b> {
    name: Reader<'a>,
    subprogram: &'b dwarf::Subprogram<Reader<'a>>,
}

fn prototype<'a, 'b>(
    name: Reader<'a>,
    subprogram: &'b dwarf::Subprogram<Reader<'a>>,
) -> Prototype<'a, 'b> {
    Prototype { name, subprogram }
}

impl fmt::Display for Prototype<'_, '_> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        let return_type = self.subprogram.return_type.borrow();
        write!(f, "{}{}", return_type, return_type.display_padding())?;
        write!(
            f,
            "{}(",
            ansi_term::Style::new().bold().paint(name.to_string_lossy())
        );
    } else {
        print!("{}(", name.to_string_lossy());
    }
    match &prototype.parameters {
        None => println!(");"),
        Some(params) if params.is_empty() => println!("void);"),
        Some(params) => {
            let mut first = true;
            for param in params {
                if first {
                    first = false;
                } else {
                    print!(", ");
                }
                let type_ = param.type_.borrow();
                print!("{}", type_);
                if let Some(name) = param.name {
                    print!("{}{}", type_.display_padding(), name.to_string_lossy());
            self.name
                .to_string_lossy()
                .if_supports_color(Stream::Stdout, |v| v.bold()),
        )?;
        match &self.subprogram.parameters {
            None => write!(f, ");"),
            Some(params) if params.is_empty() => write!(f, "void);"),
            Some(params) => {
                let mut first = true;
                for param in params {
                    if first {
                        first = false;
                    } else {
                        write!(f, ", ")?;
                    }
                    let type_ = param.type_.borrow();
                    write!(f, "{}", type_)?;
                    if let Some(name) = param.name {
                        write!(f, "{}{}", type_.display_padding(), name.to_string_lossy())?;
                    }
                }
                write!(f, ");")
            }
            println!(");");
        }
    }
}

fn show_prototypes(sources: Sources<'_>) {
    let color = atty::is(atty::Stream::Stdout);
    // TODO show types
    let mut prototypes: Vec<_> = sources.subprograms.into_iter().collect();
    prototypes.sort_unstable_by(|(func1, _), (func2, _)| func1.slice().cmp(func2.slice()));
    for (name, p) in prototypes {
        show_prototype(name, &p, color);
        println!("{}", prototype(name, &p));
    }
}