From 74992326e6f89284c78477b51d007cff50c2bc1f Mon Sep 17 00:00:00 2001 From: Ian Chamberlain Date: Sat, 12 Feb 2022 23:37:52 -0500 Subject: [PATCH] Use `fs::read_to_string()` and add st_mode Just for fun, display file mode as well as size, and simplify code with `fs::read_to_string()`, which works with the corresponding libc changes. --- ctru-rs/examples/file-explorer.rs | 33 ++++++++++--------------------- 1 file changed, 10 insertions(+), 23 deletions(-) diff --git a/ctru-rs/examples/file-explorer.rs b/ctru-rs/examples/file-explorer.rs index 4389c31..5a638ee 100644 --- a/ctru-rs/examples/file-explorer.rs +++ b/ctru-rs/examples/file-explorer.rs @@ -6,8 +6,8 @@ use ctru::console::Console; use ctru::services::hid::KeyPad; use ctru::services::{Apt, Hid}; use ctru::Gfx; -use std::fs::{DirEntry, File}; -use std::io::{BufRead, BufReader}; +use std::fs::DirEntry; +use std::os::horizon::fs::MetadataExt; use std::path::{Path, PathBuf}; fn main() { @@ -78,9 +78,10 @@ impl<'a> FileExplorer<'a> { match std::fs::metadata(&self.path) { Ok(metadata) => { println!( - "Viewing {} (size {} bytes)", + "Viewing {} (size {} bytes, mode {:#o})", self.path.display(), - metadata.len() + metadata.len(), + metadata.st_mode(), ); if metadata.is_file() { @@ -126,25 +127,11 @@ impl<'a> FileExplorer<'a> { } 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}", ""); + match std::fs::read_to_string(&self.path) { + Ok(contents) => { + println!("File contents:\n{0:->80}", ""); + println!("{contents}"); + println!("{0:->80}", ""); } Err(err) => { println!("Error reading file: {}", err);