1515
1616//! Utilities for displaying the contents of a [`FileTree`].
1717
18+ use std:: borrow:: Cow ;
1819use std:: io;
1920
2021use nary_tree:: NodeId ;
@@ -23,15 +24,43 @@ use super::{FileTree, ModVec, TreeNodeKind};
2324use crate :: instance:: Instance ;
2425
2526/// Structure to display [`FileTree`]s using [`ptree`].
26- #[ derive( Clone ) ]
27+ #[ derive( Copy , Clone ) ]
2728pub struct FileTreeDisplay < ' a > {
29+ tree : & ' a FileTree ,
30+ current_node : NodeId ,
31+ }
32+
33+ impl ptree:: TreeItem for FileTreeDisplay < ' _ > {
34+ type Child = Self ;
35+
36+ fn write_self < W : io:: Write > ( & self , f : & mut W , style : & ptree:: Style ) -> io:: Result < ( ) > {
37+ let node = self . tree . get ( self . current_node ) . expect ( "node exists" ) ;
38+ match & node. data ( ) . kind {
39+ TreeNodeKind :: Dir => write ! ( f, "📁 {}" , style. paint( & node. data( ) . name) ) ,
40+ TreeNodeKind :: File ( ( ) ) => write ! ( f, "📄 {}" , style. paint( & node. data( ) . name) ) ,
41+ }
42+ }
43+
44+ fn children ( & self ) -> Cow < ' _ , [ Self :: Child ] > {
45+ let node = self . tree . get ( self . current_node ) . expect ( "node exists" ) ;
46+ let children: Vec < _ > = node
47+ . children ( )
48+ . map ( |node| Self { tree : self . tree , current_node : node. node_id ( ) } )
49+ . collect ( ) ;
50+ Cow :: Owned ( children)
51+ }
52+ }
53+
54+ /// Structure to display [`FileTree<ModVec>`]s using [`ptree`].
55+ #[ derive( Copy , Clone ) ]
56+ pub struct ModVecFileTreeDisplay < ' a > {
2857 tree : & ' a FileTree < ModVec > ,
2958 instance : & ' a dyn Instance ,
3059 current_node : NodeId ,
3160 kind : FileTreeDisplayKind ,
3261}
3362
34- /// Specifies what files are displayed by [`FileTreeDisplay `].
63+ /// Specifies what files are displayed by [`ModVecFileTreeDisplay `].
3564#[ derive( Copy , Clone , Debug , PartialEq , Eq ) ]
3665pub enum FileTreeDisplayKind {
3766 /// Show all files.
@@ -40,7 +69,7 @@ pub enum FileTreeDisplayKind {
4069 Conflicts ,
4170}
4271
43- impl < ' a > FileTreeDisplay < ' a > {
72+ impl < ' a > ModVecFileTreeDisplay < ' a > {
4473 #[ must_use]
4574 pub fn new ( tree : & ' a FileTree < ModVec > , instance : & ' a dyn Instance , kind : FileTreeDisplayKind ) -> Self {
4675 Self {
@@ -52,7 +81,17 @@ impl<'a> FileTreeDisplay<'a> {
5281 }
5382}
5483
55- impl ptree:: TreeItem for FileTreeDisplay < ' _ > {
84+ impl < ' a > FileTreeDisplay < ' a > {
85+ #[ must_use]
86+ pub fn new ( tree : & ' a FileTree ) -> Self {
87+ Self {
88+ tree,
89+ current_node : tree. root_id ( ) . expect ( "has root node" ) ,
90+ }
91+ }
92+ }
93+
94+ impl ptree:: TreeItem for ModVecFileTreeDisplay < ' _ > {
5695 type Child = Self ;
5796
5897 fn write_self < W : io:: Write > ( & self , f : & mut W , style : & ptree:: Style ) -> io:: Result < ( ) > {
@@ -73,7 +112,7 @@ impl ptree::TreeItem for FileTreeDisplay<'_> {
73112 }
74113 }
75114
76- fn children ( & self ) -> std :: borrow :: Cow < ' _ , [ Self :: Child ] > {
115+ fn children ( & self ) -> Cow < ' _ , [ Self :: Child ] > {
77116 let node = self . tree . get ( self . current_node ) . expect ( "node exists" ) ;
78117 let children: Vec < _ > = node
79118 . children ( )
@@ -89,13 +128,13 @@ impl ptree::TreeItem for FileTreeDisplay<'_> {
89128 TreeNodeKind :: File ( providing_mods) => providing_mods. len ( ) > 1 ,
90129 }
91130 } )
92- . map ( |node| FileTreeDisplay {
131+ . map ( |node| Self {
93132 tree : self . tree ,
94133 instance : self . instance ,
95134 current_node : node. node_id ( ) ,
96135 kind : self . kind ,
97136 } )
98137 . collect ( ) ;
99- std :: borrow :: Cow :: Owned ( children)
138+ Cow :: Owned ( children)
100139 }
101140}
0 commit comments