rsv_lib/csv/
head.rs

1use crate::args::Head;
2use crate::utils::cli_result::CliResult;
3use crate::utils::filename::new_path;
4use crate::utils::writer::Writer;
5use std::fs::File;
6use std::io::{BufRead, BufReader};
7
8impl Head {
9    pub fn csv_run(&self) -> CliResult {
10        let path = self.path();
11        let out = new_path(&path, "-head");
12        let mut wtr = Writer::file_or_stdout(self.export, &out)?;
13
14        // show head n
15        BufReader::new(File::open(path)?)
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}