summaryrefslogtreecommitdiff
path: root/src/main.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/main.rs')
-rw-r--r--src/main.rs15
1 files changed, 8 insertions, 7 deletions
diff --git a/src/main.rs b/src/main.rs
index da0e599..fb030d0 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -1,20 +1,20 @@
+use std::io::BufReader;
use std::{env, fs::File, io::Read};
fn main() -> Result<(), Box<dyn std::error::Error>> {
const CHUNK_SIZE: usize = 8;
+ let mut file: Box<dyn Read>;
- let filename = env::args().nth(1).unwrap_or_default();
- if filename.is_empty() {
- eprintln!("error missing filename");
- return Ok(());
- }
+ file = if let Some(filename) = env::args().nth(1) {
+ Box::new(BufReader::new(File::open(&filename)?))
+ } else {
+ Box::new(BufReader::new(std::io::stdin()))
+ };
- let mut file = File::open(&filename)?;
let mut chunk = Vec::with_capacity(CHUNK_SIZE);
let mut idx = 0;
while {
- chunk.clear();
file.by_ref()
.take(CHUNK_SIZE as u64)
.read_to_end(&mut chunk)?
@@ -31,6 +31,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
// write ascii
println!("{}", String::from_utf8_lossy(&chunk).replace('\n', "⏎"));
idx += CHUNK_SIZE;
+ chunk.clear();
}
Ok(())