11use super :: { DisplayOptions , Traversal , Tree , TreeIndex } ;
2+ use crate :: ByteFormat ;
23use tui:: layout:: { Constraint , Direction , Layout } ;
34use tui:: style:: { Color , Style } ;
45use tui:: {
@@ -13,56 +14,78 @@ pub struct Entries<'a> {
1314 pub display : DisplayOptions ,
1415}
1516
16- pub struct InitWindow < ' a > {
17+ pub struct MainWindow < ' a > {
1718 pub traversal : & ' a Traversal ,
1819 pub display : DisplayOptions ,
1920}
2021
22+ pub struct Footer {
23+ pub total_bytes : u64 ,
24+ pub entries_traversed : u64 ,
25+ pub format : ByteFormat ,
26+ }
27+
28+ impl Widget for Footer {
29+ fn draw ( & mut self , area : Rect , buf : & mut Buffer ) {
30+ assert ! ( area. height == 1 , "The footer must be a line" ) ;
31+ let bg_color = Color :: White ;
32+ let text_color = Color :: Black ;
33+ let margin = 1 ;
34+ self . background ( area, buf, bg_color) ;
35+ buf. set_stringn (
36+ area. x + margin,
37+ area. y ,
38+ format ! (
39+ "Total disk usage: {} Entries: {}" ,
40+ format!( "{}" , self . format. display( self . total_bytes) ) . trim( ) ,
41+ self . entries_traversed
42+ ) ,
43+ ( area. width - margin) as usize ,
44+ Style {
45+ fg : text_color,
46+ bg : bg_color,
47+ ..Default :: default ( )
48+ } ,
49+ )
50+ }
51+ }
52+
2153fn get_size_or_panic ( tree : & Tree , node_idx : TreeIndex ) -> u64 {
2254 tree. node_weight ( node_idx)
2355 . expect ( "node should always be retrievable with valid index" )
2456 . size
2557}
2658
27- impl < ' a > Widget for InitWindow < ' a > {
59+ impl < ' a > Widget for MainWindow < ' a > {
2860 fn draw ( & mut self , area : Rect , buf : & mut Buffer ) {
61+ let Self {
62+ traversal :
63+ Traversal {
64+ tree,
65+ root_index,
66+ entries_traversed,
67+ ..
68+ } ,
69+ display,
70+ } = * self ;
2971 let regions = Layout :: default ( )
3072 . direction ( Direction :: Vertical )
3173 . constraints ( [ Constraint :: Max ( 256 ) , Constraint :: Length ( 1 ) ] . as_ref ( ) )
3274 . split ( area) ;
3375 let ( entries, footer) = ( regions[ 0 ] , regions[ 1 ] ) ;
3476 Entries {
35- tree : & self . traversal . tree ,
36- root : self . traversal . root_index ,
37- display : self . display ,
77+ tree : & tree,
78+ root : * root_index,
79+ display : display,
3880 }
3981 . draw ( entries, buf) ;
4082
41- let bg_color = Color :: White ;
42- let text_color = Color :: Black ;
43- let margin = 1 ;
44- self . background ( footer, buf, bg_color) ;
45- buf. set_stringn (
46- footer. x + margin,
47- footer. y ,
48- format ! (
49- "Total disk usage: {}" ,
50- format!(
51- "{}" ,
52- self . display. byte_format. display( get_size_or_panic(
53- & self . traversal. tree,
54- self . traversal. root_index
55- ) )
56- )
57- . trim( )
58- ) ,
59- ( footer. width - margin) as usize ,
60- Style {
61- fg : text_color,
62- bg : bg_color,
63- ..Default :: default ( )
64- } ,
65- )
83+ Footer {
84+ total_bytes : get_size_or_panic ( & tree, * root_index) ,
85+ entries_traversed : * entries_traversed,
86+ format : display. byte_format ,
87+ }
88+ . draw ( footer, buf) ;
6689 }
6790}
6891
0 commit comments