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 | |
parent | 0412ab2308a220cf432b5debb7f4f706dfbc190d (diff) |
Module dep pretty printing in --info
-rw-r--r-- | src/modules.rs | 47 | ||||
-rw-r--r-- | tests/022_info_flag.out | 14 | ||||
-rw-r--r-- | tests/022_info_flag.test | 2 |
3 files changed, 48 insertions, 15 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); + } + } } diff --git a/tests/022_info_flag.out b/tests/022_info_flag.out index 163746f11..329e07b6d 100644 --- a/tests/022_info_flag.out +++ b/tests/022_info_flag.out @@ -1,6 +1,14 @@ -local: [WILDCARD]deps/http/127.0.0.1_PORT4545/tests/003_relative_import.ts +local: [WILDCARD]deps/http/127.0.0.1_PORT4545/tests/019_media_types.ts type: TypeScript compiled: [WILDCARD].js map: [WILDCARD].js.map -deps: http://127.0.0.1:4545/tests/003_relative_import.ts -| http://127.0.0.1:4545/tests/subdir/print_hello.ts +deps: +http://127.0.0.1:4545/tests/019_media_types.ts + ├── http://localhost:4545/tests/subdir/mt_text_typescript.t1.ts + ├── http://localhost:4545/tests/subdir/mt_video_vdn.t2.ts + ├── http://localhost:4545/tests/subdir/mt_video_mp2t.t3.ts + ├── http://localhost:4545/tests/subdir/mt_application_x_typescript.t4.ts + ├── http://localhost:4545/tests/subdir/mt_text_javascript.j1.js + ├── http://localhost:4545/tests/subdir/mt_application_ecmascript.j2.js + ├── http://localhost:4545/tests/subdir/mt_text_ecmascript.j3.js + └── http://localhost:4545/tests/subdir/mt_application_x_javascript.j4.js diff --git a/tests/022_info_flag.test b/tests/022_info_flag.test index e2132a85f..2de27bdfa 100644 --- a/tests/022_info_flag.test +++ b/tests/022_info_flag.test @@ -1,4 +1,4 @@ # The output assumes 003_relative_import.ts has already been run earlier # and its output is cached to $DENO_DIR. -args: --info http://127.0.0.1:4545/tests/003_relative_import.ts +args: --info http://127.0.0.1:4545/tests/019_media_types.ts output: tests/022_info_flag.out |