cat.rs
 1  use core::fmt::Write;
 2  use alloc::string::String as AllocString;
 3  use crate::filesystems::sd::read_file_at;
 4  
 5  pub fn run(cwd: &str, name: &str) -> AllocString {
 6      if name.is_empty() {
 7          return super::fmt_usage("cat <filename>");
 8      }
 9  
10      match read_file_at::<8192>(cwd, name) {
11          Ok(contents) => {
12              let mut out = AllocString::new();
13              match core::str::from_utf8(contents.as_slice()) {
14                  Ok(text) => {
15                      for line in text.lines() {
16                          let _ = write!(out, "{}\r\n", line);
17                      }
18                  }
19                  Err(_) => {
20                      let _ = write!(out, "\x1b[2m({} bytes, binary)\x1b[0m\r\n", contents.len());
21                  }
22              }
23              out
24          }
25          Err(error) => super::fmt_error(&error),
26      }
27  }