From 6b409cdcae6c8a0e0d4dd4c6830542525a05b085 Mon Sep 17 00:00:00 2001 From: Ian Chamberlain Date: Sat, 12 Feb 2022 14:41:36 -0500 Subject: [PATCH] Add support for reading files to file-explorer --- ctru-rs/examples/file-explorer.rs | 105 ++++++++++++++++++++------- ctru-rs/examples/romfs/garbage-data | 1 + ctru-rs/examples/romfs/test-file.txt | 4 +- 3 files changed, 83 insertions(+), 27 deletions(-) create mode 100644 ctru-rs/examples/romfs/garbage-data diff --git a/ctru-rs/examples/file-explorer.rs b/ctru-rs/examples/file-explorer.rs index a334301..4389c31 100644 --- a/ctru-rs/examples/file-explorer.rs +++ b/ctru-rs/examples/file-explorer.rs @@ -6,7 +6,8 @@ use ctru::console::Console; use ctru::services::hid::KeyPad; use ctru::services::{Apt, Hid}; use ctru::Gfx; -use std::fs::DirEntry; +use std::fs::{DirEntry, File}; +use std::io::{BufRead, BufReader}; use std::path::{Path, PathBuf}; fn main() { @@ -74,8 +75,36 @@ impl<'a> FileExplorer<'a> { } fn print_menu(&mut self) { - println!("Viewing {}", self.path.display()); + match std::fs::metadata(&self.path) { + Ok(metadata) => { + println!( + "Viewing {} (size {} bytes)", + self.path.display(), + metadata.len() + ); + + if metadata.is_file() { + self.print_file_contents(); + // let the user continue navigating from the parent dir + // after dumping the file + self.path.pop(); + self.print_menu(); + return; + } else if metadata.is_dir() { + self.print_dir_entries(); + } else { + println!("unsupported file type: {:?}", metadata.file_type()); + } + } + Err(e) => { + println!("Failed to read {}: {}", self.path.display(), e) + } + }; + println!("Start to exit, A to select an entry by number, B to go up a directory, X to set the path."); + } + + fn print_dir_entries(&mut self) { let dir_listing = std::fs::read_dir(&self.path).expect("Failed to open path"); self.entries = Vec::new(); @@ -85,25 +114,8 @@ impl<'a> FileExplorer<'a> { println!("{:2} - {}", i, entry.file_name().to_string_lossy()); self.entries.push(entry); - // Paginate the output if (i + 1) % 20 == 0 { - println!("Press A to go to next page, or Start to exit"); - - while self.apt.main_loop() { - self.hid.scan_input(); - let input = self.hid.keys_down(); - - if input.contains(KeyPad::KEY_A) { - break; - } - - if input.contains(KeyPad::KEY_START) { - self.running = false; - return; - } - - self.gfx.wait_for_vblank(); - } + self.wait_for_page_down(); } } Err(e) => { @@ -111,8 +123,54 @@ impl<'a> FileExplorer<'a> { } } } + } - println!("Start to exit, A to select an entry by number, B to go up a directory, X to set the path."); + fn print_file_contents(&mut self) { + match File::open(&self.path) { + Ok(f) => { + println!("File contents:\n{:->80}", ""); + let reader = BufReader::new(f); + for (i, line) in reader.lines().enumerate() { + match line { + Ok(line) => { + println!("{}", line); + if (i + 1) % 20 == 0 { + self.wait_for_page_down(); + } + } + Err(err) => { + println!("Error reading file: {}", err); + break; + } + } + } + println!("{:->80}", ""); + } + Err(err) => { + println!("Error reading file: {}", err); + } + } + } + + /// Paginate output + fn wait_for_page_down(&mut self) { + println!("Press A to go to next page, or Start to exit"); + + while self.apt.main_loop() { + self.hid.scan_input(); + let input = self.hid.keys_down(); + + if input.contains(KeyPad::KEY_A) { + break; + } + + if input.contains(KeyPad::KEY_START) { + self.running = false; + return; + } + + self.gfx.wait_for_vblank(); + } } fn get_input_and_run(&mut self, action: impl FnOnce(&mut Self, String)) { @@ -154,11 +212,6 @@ impl<'a> FileExplorer<'a> { } }; - if !next_entry.file_type().unwrap().is_dir() { - println!("Not a directory: {}", next_path_index); - return; - } - self.console.clear(); self.path = next_entry.path(); self.print_menu(); diff --git a/ctru-rs/examples/romfs/garbage-data b/ctru-rs/examples/romfs/garbage-data new file mode 100644 index 0000000..fbc416b --- /dev/null +++ b/ctru-rs/examples/romfs/garbage-data @@ -0,0 +1 @@ +c:7viz`nʢfb \ No newline at end of file diff --git a/ctru-rs/examples/romfs/test-file.txt b/ctru-rs/examples/romfs/test-file.txt index 30d74d2..e627701 100644 --- a/ctru-rs/examples/romfs/test-file.txt +++ b/ctru-rs/examples/romfs/test-file.txt @@ -1 +1,3 @@ -test \ No newline at end of file +test file contents + +another line