diff options
author | Greg Altman <g.s.altman@gmail.com> | 2019-02-14 14:11:51 -0500 |
---|---|---|
committer | Ryan Dahl <ry@tinyclouds.org> | 2019-02-15 11:06:49 -0500 |
commit | 061a9353ba3d359ed62b494f9f63af78aed4498b (patch) | |
tree | 96a52d771406a93dc0d9c74b73142fd5c2b7b4e5 /src | |
parent | 0412ab2308a220cf432b5debb7f4f706dfbc190d (diff) |
Module dep pretty printing in --info
Diffstat (limited to 'src')
-rw-r--r-- | src/modules.rs | 47 |
1 files changed, 36 insertions, 11 deletions
diff --git a/src/modules.rs b/src/modules.rs index 7711dec95..315a6f8f1 100644 --- a/src/modules.rs +++ b/src/modules.rs @@ -91,19 +91,21 @@ impl Modules { pub struct Deps { pub name: String, pub deps: Option<Vec<Deps>>, - depth: usize, + prefix: String, + is_last: bool, } impl Deps { pub fn new(modules: &Modules, module_name: &str) -> Deps { let mut seen = HashSet::new(); let id = modules.get_id(module_name).unwrap(); - Self::helper(&mut seen, 0, modules, id) + Self::helper(&mut seen, "".to_string(), true, modules, id) } fn helper( seen: &mut HashSet<deno_mod>, - depth: usize, + prefix: String, + is_last: bool, modules: &Modules, id: deno_mod, ) -> Deps { @@ -111,20 +113,29 @@ impl Deps { if seen.contains(&id) { Deps { name, - depth, + prefix, deps: None, + is_last, } } else { seen.insert(id); let child_ids = modules.get_children(id).unwrap(); + let child_count = child_ids.iter().count(); let deps = child_ids .iter() - .map(|dep_id| Self::helper(seen, depth + 1, modules, *dep_id)) - .collect(); + .enumerate() + .map(|(index, dep_id)| { + let new_is_last = index == child_count - 1; + let mut new_prefix = prefix.clone(); + new_prefix.push(if is_last { ' ' } else { '│' }); + new_prefix.push(' '); + Self::helper(seen, new_prefix, new_is_last, modules, *dep_id) + }).collect(); Deps { name, - depth, + prefix, deps: Some(deps), + is_last, } } } @@ -132,10 +143,19 @@ impl Deps { impl fmt::Display for Deps { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - for _i in 0..self.depth { - write!(f, "| ")?; + let mut has_children = false; + if let Some(ref deps) = self.deps { + has_children = !deps.is_empty(); } - write!(f, "{}", self.name)?; + write!( + f, + "{}{}─{} {}", + self.prefix, + if self.is_last { "└" } else { "├" }, + if has_children { "┬" } else { "─" }, + self.name + )?; + if let Some(ref deps) = self.deps { for d in deps { write!(f, "\n{}", d)?; @@ -179,5 +199,10 @@ pub fn print_file_info( } let deps = Deps::new(modules, &out.module_name); - println!("{} {}", ansi::bold("deps:".to_string()), deps); + println!("{}{}", ansi::bold("deps:\n".to_string()), deps.name); + if let Some(ref depsdeps) = deps.deps { + for d in depsdeps { + println!("{}", d); + } + } } |