11use std:: path:: PathBuf ;
22
33use ratatui:: {
4- layout:: { Constraint , Direction , Layout , Rect } ,
4+ layout:: { Alignment , Constraint , Direction , Layout , Rect } ,
55 style:: { Color , Modifier , Style } ,
66 text:: { Line , Span } ,
77 widgets:: { Block , Borders , List , ListItem , Paragraph } ,
@@ -22,7 +22,7 @@ pub fn render(frame: &mut Frame, app: &App) {
2222 . title ( format ! (
2323 "{} File Browser - PeerID: {}" ,
2424 if app. is_host { "Host" } else { "Remote" } ,
25- app. peer_id. to_string ( )
25+ app. peer_id
2626 ) )
2727 . borders ( Borders :: ALL ) ;
2828 frame. render_widget ( main_block, frame. area ( ) ) ;
@@ -53,11 +53,11 @@ pub fn render(frame: &mut Frame, app: &App) {
5353 . block ( Block :: default ( ) . title ( "Loading..." ) . borders ( Borders :: ALL ) )
5454 . style ( Style :: default ( ) . fg ( Color :: Yellow ) ) ;
5555 frame. render_widget ( loading, left_chunks[ 1 ] ) ;
56- } else if app. is_warning {
57- tracing:: warn!( "Warning: {}" , app. warning_message) ;
58- let warning = Paragraph :: new ( app. warning_message . clone ( ) )
59- . block ( Block :: default ( ) . title ( "⚠️ Warning" ) . borders ( Borders :: ALL ) )
60- . style ( Style :: default ( ) . fg ( Color :: Red ) . add_modifier ( Modifier :: BOLD ) ) ;
56+ } else if app. is_warning ( ) {
57+ tracing:: warn!( "Warning: {}" , app. warning_message( ) ) ;
58+ let warning = Paragraph :: new ( app. warning_message ( ) . to_string ( ) )
59+ . style ( Style :: default ( ) . fg ( Color :: Yellow ) )
60+ . alignment ( Alignment :: Center ) ;
6161 frame. render_widget ( warning, left_chunks[ 1 ] ) ;
6262 } else {
6363 render_file_tree ( frame, app, left_chunks[ 1 ] ) ;
@@ -69,43 +69,41 @@ pub fn render(frame: &mut Frame, app: &App) {
6969 // Right panel with preview
7070 let preview_block = Block :: default ( ) . title ( " Preview " ) . borders ( Borders :: ALL ) ;
7171
72- let preview_content = if let Some ( index) = app. selected_index {
73- if let Some ( item) = app. directory_items . get ( index) {
74- if !item. is_dir {
75- match std:: fs:: read_to_string ( & item. path ) {
76- Ok ( contents) => contents,
77- Err ( _) => "Unable to read file contents" . to_string ( ) ,
78- }
79- } else {
80- // For directories, show a friendly message or list children
81- let children: Vec < _ > = app
82- . directory_items
83- . iter ( )
84- . filter ( |child| child. path . parent ( ) . unwrap_or ( & PathBuf :: new ( ) ) == item. path )
85- . map ( |child| {
72+ let preview_content = app
73+ . selected_index
74+ . and_then ( |index| app. directory_items . get ( index) )
75+ . map_or_else (
76+ || "No file selected" . to_string ( ) ,
77+ |item| {
78+ if item. is_dir {
79+ // For directories, show a friendly message or list children
80+ let children: Vec < _ > = app
81+ . directory_items
82+ . iter ( )
83+ . filter ( |child| child. path . parent ( ) . unwrap_or ( & PathBuf :: new ( ) ) == item. path )
84+ . map ( |child| {
85+ format ! (
86+ "{}{}" ,
87+ if child. is_dir { "/" } else { "" } ,
88+ child. name. clone( )
89+ )
90+ } )
91+ . collect ( ) ;
92+ if children. is_empty ( ) {
93+ format ! ( "Directory: {} (empty)" , item. name)
94+ } else {
8695 format ! (
87- "{} {}" ,
88- if child . is_dir { "/" } else { "" } ,
89- child . name . clone ( )
96+ "Directory: {} \n Children: \n {}" ,
97+ item . name ,
98+ children . join ( " \n " )
9099 )
91- } )
92- . collect ( ) ;
93- if children. is_empty ( ) {
94- format ! ( "Directory: {} (empty)" , item. name)
100+ }
95101 } else {
96- format ! (
97- "Directory: {}\n Children:\n {}" ,
98- item. name,
99- children. join( "\n " )
100- )
102+ std:: fs:: read_to_string ( & item. path )
103+ . unwrap_or_else ( |_| "Unable to read file contents" . to_string ( ) )
101104 }
102- }
103- } else {
104- "No file selected" . to_string ( )
105- }
106- } else {
107- "No file selected" . to_string ( )
108- } ;
105+ } ,
106+ ) ;
109107
110108 let preview = Paragraph :: new ( preview_content)
111109 . block ( preview_block)
@@ -148,37 +146,37 @@ fn render_file_tree(frame: &mut Frame, app: &App, area: Rect) {
148146 . block ( Block :: default ( ) . title ( "Status" ) . borders ( Borders :: ALL ) )
149147 . style ( Style :: default ( ) . fg ( Color :: Yellow ) ) ;
150148 frame. render_widget ( loading, area) ;
151- } else if app. is_warning {
152- let warning = Paragraph :: new ( app. warning_message . clone ( ) )
153- . block ( Block :: default ( ) . title ( "Warning" ) . borders ( Borders :: ALL ) )
154- . style ( Style :: default ( ) . fg ( Color :: Red ) ) ;
149+ } else if app. is_warning ( ) {
150+ let warning = Paragraph :: new ( app. warning_message ( ) . to_string ( ) )
151+ . style ( Style :: default ( ) . fg ( Color :: Yellow ) )
152+ . alignment ( Alignment :: Center ) ;
155153 frame. render_widget ( warning, area) ;
156154 } else {
157155 let items: Vec < ListItem > = app
158156 . directory_items
159157 . iter ( )
160158 . map ( |item| {
161159 let indent = " " . repeat ( item. depth ) ;
162- let selected = if let Ok ( rel_path) = item. path . strip_prefix ( & app. current_path ) {
163- match app. state {
164- AppState :: Share => {
165- if app. items_to_share . contains ( & rel_path. to_path_buf ( ) ) {
160+ let selected = item. path . strip_prefix ( & app. current_path ) . map_or_else (
161+ |_| match app. state {
162+ AppState :: Download => {
163+ let path_buf = PathBuf :: from ( & item. name ) ;
164+ if app. items_to_download . contains ( & path_buf) {
166165 "🔵 "
167166 } else {
168167 " "
169168 }
170169 }
171- AppState :: Download => {
172- let path_buf = PathBuf :: from ( & item. name ) ;
173- if app. items_to_download . contains ( & path_buf) {
170+ AppState :: Share => " " ,
171+ } ,
172+ |rel_path| match app. state {
173+ AppState :: Share => {
174+ if app. items_to_share . contains ( & rel_path. to_path_buf ( ) ) {
174175 "🔵 "
175176 } else {
176177 " "
177178 }
178179 }
179- }
180- } else {
181- match app. state {
182180 AppState :: Download => {
183181 let path_buf = PathBuf :: from ( & item. name ) ;
184182 if app. items_to_download . contains ( & path_buf) {
@@ -187,9 +185,8 @@ fn render_file_tree(frame: &mut Frame, app: &App, area: Rect) {
187185 " "
188186 }
189187 }
190- _ => " " ,
191- }
192- } ;
188+ } ,
189+ ) ;
193190 let prefix = if item. is_dir { "📁 " } else { "📄 " } ;
194191
195192 let style = if Some ( item. index ) == app. selected_index . or ( Some ( 0 ) ) {
@@ -206,8 +203,7 @@ fn render_file_tree(frame: &mut Frame, app: &App, area: Rect) {
206203 ) ,
207204 AppState :: Download => {
208205 let path_buf = PathBuf :: from ( & item. name ) ;
209- let is_selected = app. items_to_download . contains ( & path_buf) ;
210- is_selected
206+ app. items_to_download . contains ( & path_buf)
211207 }
212208 } {
213209 Style :: default ( ) . fg ( Color :: Green )
@@ -237,22 +233,22 @@ fn render_file_tree(frame: &mut Frame, app: &App, area: Rect) {
237233}
238234
239235fn render_status ( frame : & mut Frame , app : & App , area : Rect ) {
240- let total_selected = match app. state {
241- AppState :: Share => app. items_to_share . len ( ) ,
242- AppState :: Download => app. items_to_download . len ( ) ,
243- } ;
236+ // Calculate total selected items
237+ let total_selected = app. items_to_share . len ( ) + app. items_to_download . len ( ) ;
244238
245- let status = if app. connected {
239+ // Create status bar
240+ let status = if app. is_connected ( ) {
246241 format ! (
247- "Connected to: {} | Selected items: {}" ,
248- app. connected_peer_id. unwrap( ) . to_string( ) ,
242+ "Connected to peer: {} | Selected items: {}" ,
243+ app. connected_peer_id
244+ . map_or( "Unknown" . to_string( ) , |id| id. to_string( ) ) ,
249245 total_selected
250246 )
251247 } else {
252- format ! ( "Disconnected | Selected items: {}" , total_selected )
248+ format ! ( "Disconnected | Selected items: {total_selected}" )
253249 } ;
254250
255- let status_style = if app. connected {
251+ let status_style = if app. is_connected ( ) {
256252 Style :: default ( ) . fg ( Color :: Green )
257253 } else {
258254 Style :: default ( ) . fg ( Color :: Red )
0 commit comments