Browse Source

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.
pull/47/head
Ian Chamberlain 3 years ago
parent
commit
74992326e6
No known key found for this signature in database
GPG Key ID: AE5484D09405AA60
  1. 33
      ctru-rs/examples/file-explorer.rs

33
ctru-rs/examples/file-explorer.rs

@ -6,8 +6,8 @@ use ctru::console::Console;
use ctru::services::hid::KeyPad; use ctru::services::hid::KeyPad;
use ctru::services::{Apt, Hid}; use ctru::services::{Apt, Hid};
use ctru::Gfx; use ctru::Gfx;
use std::fs::{DirEntry, File}; use std::fs::DirEntry;
use std::io::{BufRead, BufReader}; use std::os::horizon::fs::MetadataExt;
use std::path::{Path, PathBuf}; use std::path::{Path, PathBuf};
fn main() { fn main() {
@ -78,9 +78,10 @@ impl<'a> FileExplorer<'a> {
match std::fs::metadata(&self.path) { match std::fs::metadata(&self.path) {
Ok(metadata) => { Ok(metadata) => {
println!( println!(
"Viewing {} (size {} bytes)", "Viewing {} (size {} bytes, mode {:#o})",
self.path.display(), self.path.display(),
metadata.len() metadata.len(),
metadata.st_mode(),
); );
if metadata.is_file() { if metadata.is_file() {
@ -126,25 +127,11 @@ impl<'a> FileExplorer<'a> {
} }
fn print_file_contents(&mut self) { fn print_file_contents(&mut self) {
match File::open(&self.path) { match std::fs::read_to_string(&self.path) {
Ok(f) => { Ok(contents) => {
println!("File contents:\n{:->80}", ""); println!("File contents:\n{0:->80}", "");
let reader = BufReader::new(f); println!("{contents}");
for (i, line) in reader.lines().enumerate() { println!("{0:->80}", "");
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) => { Err(err) => {
println!("Error reading file: {}", err); println!("Error reading file: {}", err);

Loading…
Cancel
Save