1use clap::Parser;
2use std::{path, process, fs::File, error::Error, io::{self, BufReader, prelude::*, BufWriter, Stdout}};
3use anyhow::{Context, Result};
4
5#[derive(Parser)]
7pub struct Cli {
8 path: path::PathBuf
10}
11
12pub fn run(args: &Cli) -> Result<(), Box<dyn Error>> {
13 let handle = buffered_stdout();
14
15 let _ = read_file(&args, handle)?;
16
17
18 Ok(())
19}
20
21fn read_file(args: &Cli, mut writer: impl std::io::Write) -> Result<(), Box<dyn Error>> {
22
23 let file = File::open(&args.path)?;
24 let mut reader = BufReader::new(file);
25
26 let mut content = String::new();
27
28 while reader.read_line(&mut content)
29 .with_context(|| format!("could not read file: `{}`", &args.path.to_string_lossy()))
30 .unwrap() > 0
31 {
32 writeln!(writer, "{}", content.trim()).unwrap_or_else(|err| {
33 eprintln!("Couldn't write to the terminal, {}", err);
34 process::exit(1);
35 });
36 content.clear();
37
38 }
39
40 Ok(())
41}
42
43fn buffered_stdout() -> BufWriter<Stdout> {
44 let stdout = io::stdout();
45 let handle = io::BufWriter::new(stdout);
46
47 handle
48}