use ratatui::text::Text;
use std::io::Cursor;
use crate::integrations::{delimiter_from_path_for_viewer, detect_delimiter_byte};
use crate::render::viewers::pretty_tables;
fn parse_with_delimiter(raw: &str, delim: u8) -> Result<Vec<Vec<String>>, ::csv::Error> {
let mut rows = Vec::new();
let mut rdr = ::csv::ReaderBuilder::new()
.has_headers(false)
.delimiter(delim)
.from_reader(Cursor::new(raw));
for result in rdr.records() {
let record = result?;
rows.push(record.iter().map(String::from).collect());
}
Ok(rows)
}
pub fn parse_csv(raw: &str, path_hint: Option<&str>) -> Result<Vec<Vec<String>>, ::csv::Error> {
let hint = path_hint.unwrap_or("");
let delim = delimiter_from_path_for_viewer(hint).unwrap_or_else(|| detect_delimiter_byte(raw));
parse_with_delimiter(raw, delim)
}
#[must_use]
pub fn table_string_with_max_cell(
rows: &[Vec<String>],
content_width: u16,
max_cell_chars: usize,
) -> String {
pretty_tables::table_string_with_max_cell(rows, content_width, max_cell_chars)
}
#[must_use]
pub fn table_string_rows_only(
rows: &[Vec<String>],
content_width: u16,
max_cell_chars: usize,
) -> String {
pretty_tables::table_string_rows_only(rows, content_width, max_cell_chars)
}
#[must_use]
pub fn table_string(rows: &[Vec<String>], content_width: u16) -> String {
if rows.is_empty() {
return String::new();
}
let mut rows = rows.to_vec();
let header = rows.remove(0);
pretty_tables::table_string_header_body_smart_wrap(&header, &rows, content_width)
}
#[must_use]
pub fn table_to_text(rows: &[Vec<String>], content_width: u16) -> Text<'static> {
table_string_to_text(&table_string(rows, content_width))
}
#[must_use]
pub fn table_string_to_text(table_str: &str) -> Text<'static> {
pretty_tables::table_string_to_text(table_str)
}
#[must_use]
pub fn table_line_count(rows: &[Vec<String>], content_width: u16) -> usize {
table_string(rows, content_width).lines().count()
}
#[must_use]
pub fn table_string_and_line_count(rows: &[Vec<String>], content_width: u16) -> (String, usize) {
let s = table_string(rows, content_width);
let count = s.lines().count();
(s, count)
}