irevenko / ferris-fetch

🎨🦀 A system information tool for Rustaceans
MIT License
123 stars 6 forks source link

`cargo install --list` lines / 2 is not how to count the number of installed crates #2

Closed jplatte closed 3 years ago

jplatte commented 3 years ago

Crates may install more than one binary, for example:

cargo-binutils v0.3.2:
    cargo-nm
    cargo-objcopy
    cargo-objdump
    cargo-profdata
    cargo-readobj
    cargo-size
    cargo-strip
    rust-ar
    rust-ld
    rust-lld
    rust-nm
    rust-objcopy
    rust-objdump
    rust-profdata
    rust-readobj
    rust-size
    rust-strip
cargo-edit v0.7.0:
    cargo-add
    cargo-rm
    cargo-upgrade
sqlx-cli v0.2.0:
    cargo-sqlx
    sqlx

Counting the un-indented lines should work though.

irevenko commented 3 years ago

Interesting, i'll dig into this

irevenko commented 3 years ago

@jplatte
Republished the crate. Now function looks like this Can you check this out again?

fn get_cargo_crates() -> usize {
    let cargo_installs = match exc("cargo install --list") {
        Ok(installs) => installs.stdout,
        Err(_) => "not present".as_bytes().to_vec(),
    };
    let cargo_installs = std::str::from_utf8(&cargo_installs).unwrap().lines();

    let mut cargo_vec: Vec<String> = Vec::new();

    for line in cargo_installs {
        if !line.starts_with("    ") { 
            cargo_vec.push(line.to_string());
        }
    }
    return cargo_vec.len();
}
jplatte commented 3 years ago

Instead of collecting all of the lines into another Vec and counting the elements, you can just filter the iterator and count how many elements it contains:

cargo_installs.filter(|line| !line.starts_with("    ")).count()

(plus you don't need the explicit return)

irevenko commented 3 years ago

Thanks i did ! (need to read Rust Book and Rust idiomatic ways like FP) That is because i've been doing lots of GoLang.

irevenko commented 3 years ago

@jplatte can we close this issue and you can leave the star?

jplatte commented 3 years ago

Yes, I don't see why this would need to be kept open.