rsv_lib/io/
head.rs

1use crate::{
2    args::Head,
3    utils::{cli_result::CliResult, filename::new_file, writer::Writer},
4};
5use std::io::{stdin, BufRead};
6
7impl Head {
8    pub fn io_run(&self) -> CliResult {
9        let out = new_file("sorted.csv");
10        let mut wtr = Writer::file_or_stdout(self.export, &out)?;
11
12        // show head n
13        // open file and header
14        stdin()
15            .lock()
16            .lines()
17            .take(self.n + 1 - self.no_header as usize)
18            .for_each(|r| {
19                if let Ok(r) = r {
20                    wtr.write_str_unchecked(&r);
21                }
22            });
23
24        if self.export {
25            println!("Saved to file: {}", out.display())
26        }
27
28        Ok(())
29    }
30}