1use anyhow::{Context, Result};
2use log::info;
3
4use crate::{cli::PreviewArgs, io_utils, table};
5
6pub fn execute(args: &PreviewArgs) -> Result<()> {
7 let delimiter = io_utils::resolve_input_delimiter(&args.input, args.delimiter);
8 let encoding = io_utils::resolve_encoding(args.input_encoding.as_deref())?;
9 let mut reader = io_utils::open_csv_reader_from_path(&args.input, delimiter, true)?;
10 let headers = io_utils::reader_headers(&mut reader, encoding)?;
11 let mut rows = Vec::new();
12
13 for (idx, record) in reader.byte_records().enumerate() {
14 if idx >= args.rows {
15 break;
16 }
17 let record = record.with_context(|| format!("Reading row {}", idx + 2))?;
18 let decoded = io_utils::decode_record(&record, encoding)?;
19 rows.push(decoded);
20 }
21
22 table::print_table(&headers, &rows);
23 info!("Displayed {} row(s) from {:?}", rows.len(), args.input);
24 Ok(())
25}