Skip to content

Commit

Permalink
feat: add dependency insights field
Browse files Browse the repository at this point in the history
Signed-off-by: Luke-zhang-04 <luke.zhang2004dev@gmail.com>
  • Loading branch information
Luke-zhang-04 committed Nov 4, 2020
1 parent 230088d commit 1c62703
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 7 deletions.
7 changes: 7 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ priority = "optional"
color_quant = "1.1" # Use version required by image
colored= "2.0.0"
git2 = { version = "0.13.12", default-features = false }
json = "0.12.4"
tokei = "12.0.0"
askalono = "0.4.3"
bytecount = "0.6.0"
Expand All @@ -46,4 +47,4 @@ more-asserts = "0.2"
paste = "1"

[features]
fail-on-deprecated = []
fail-on-deprecated = []
12 changes: 7 additions & 5 deletions src/onefetch/deps.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,24 +18,26 @@ impl std::fmt::Display for PackageManager {

type DependencyParser = fn(&str) -> Option<i32>;

struct Detector {
pub struct Detector {
package_managers: HashMap<String, (DependencyParser, PackageManager)>,
}

fn npm(contents: &str) -> Option<i32> {
Some(0)
let parsed = json::parse(contents).unwrap();

Some(parsed["dependencies"].len() as i32)
}

impl Detector {
pub fn new(&self) -> Detector {
pub fn new() -> Self {
let mut package_managers: HashMap<String, (DependencyParser, PackageManager)> =
HashMap::new();
package_managers.insert(String::from("package.json"), (npm, PackageManager::Npm));

Detector { package_managers }
Self { package_managers }
}

pub fn get_dep_count(&self, dir: &str) -> Result<String> {
pub fn get_deps_info(&self, dir: &str) -> Result<String> {
fn is_package_file(detector: &Detector, file_name: &str) -> bool {
detector
.package_managers
Expand Down
14 changes: 13 additions & 1 deletion src/onefetch/info.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use {
crate::onefetch::{
cli::Cli, commit_info::CommitInfo, error::*, language::Language, license::Detector,
cli::Cli, commit_info::CommitInfo, deps, error::*, language::Language, license::Detector,
text_color::TextColor,
},
colored::{Color, ColoredString, Colorize},
Expand All @@ -18,6 +18,7 @@ pub struct Info {
creation_date: String,
pub dominant_language: Language,
languages: Vec<(Language, f64)>,
dependencies: String,
authors: Vec<(String, usize, usize)>,
last_change: String,
repo_url: String,
Expand Down Expand Up @@ -118,6 +119,15 @@ impl std::fmt::Display for Info {
)?;
}

if !self.config.disabled_fields.dependencies && !self.dependencies.is_empty() {
writeln!(
f,
"{}{}",
&self.get_formatted_subtitle_label("Dependencies"),
&self.dependencies.color(self.color_set.info),
)?;
}

if !self.config.disabled_fields.authors && !self.authors.is_empty() {
let title = if self.authors.len() > 1 {
"Authors"
Expand Down Expand Up @@ -244,6 +254,7 @@ impl Info {
let last_change = Info::get_date_of_last_commit(&git_history);
let project_license = Detector::new()?.get_project_license(workdir_str);
let dominant_language = Language::get_dominant_language(&languages_stats);
let dependencies = deps::Detector::new().get_deps_info(workdir_str).unwrap();
let colors = Info::get_colors(
&config.ascii_language,
&dominant_language,
Expand All @@ -261,6 +272,7 @@ impl Info {
creation_date: creation_date?,
dominant_language,
languages: languages_stats,
dependencies,
authors,
last_change: last_change?,
repo_url: repository_url,
Expand Down
3 changes: 3 additions & 0 deletions src/onefetch/info_fields.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ pub struct InfoFieldOn {
pub head: bool,
pub version: bool,
pub created: bool,
pub dependencies: bool,
pub languages: bool,
pub authors: bool,
pub last_change: bool,
Expand All @@ -30,6 +31,7 @@ pub enum InfoFields {
HEAD,
Version,
Created,
Dependencies,
Languages,
Authors,
LastChange,
Expand Down Expand Up @@ -57,6 +59,7 @@ pub fn get_disabled_fields(fields_to_hide: Vec<String>) -> Result<InfoFieldOn> {
InfoFields::HEAD => disabled_fields.head = true,
InfoFields::Version => disabled_fields.version = true,
InfoFields::Created => disabled_fields.created = true,
InfoFields::Dependencies => disabled_fields.dependencies = true,
InfoFields::Languages => disabled_fields.languages = true,
InfoFields::Authors => disabled_fields.authors = true,
InfoFields::LastChange => disabled_fields.last_change = true,
Expand Down

0 comments on commit 1c62703

Please sign in to comment.