1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465
use crate::{ColumnFormat, ColumnFormatShorthand, FormatError};
use polars::prelude::*;
const DEFAULT_TABLE_HEIGHT: usize = 30;
/// dataframe format
#[derive(Debug)]
pub struct DataFrameFormat {
/// column formats
pub column_formats: Option<Vec<ColumnFormatShorthand>>,
/// column delimiter
pub column_delimiter: String,
/// header separator delimiter
pub header_separator_delimiter: String,
/// header separator char
pub header_separator_char: char,
/// include header row
pub include_header_row: bool,
/// include header separator row
pub include_header_separator_row: bool,
/// include summary row
pub include_summary_row: bool,
/// include summary separator row
pub include_summary_separator_row: bool,
/// render height
pub render_height: Option<usize>,
/// max render width
pub max_render_width: Option<usize>,
}
impl Default for DataFrameFormat {
fn default() -> DataFrameFormat {
DataFrameFormat {
column_formats: None,
column_delimiter: " │ ".to_string(),
header_separator_delimiter: "──┼──".to_string(),
header_separator_char: '─',
include_header_row: true,
include_header_separator_row: true,
include_summary_row: false,
include_summary_separator_row: false,
render_height: None,
max_render_width: None,
}
}
}
/// finalized DataFrameFormat
#[derive(Debug)]
pub struct DataFrameFormatFinal {
/// column formats
pub column_formats: Vec<ColumnFormat>,
/// column delimiter
pub column_delimiter: String,
/// header separator delimiter
pub header_separator_delimiter: String,
/// header separator char
pub header_separator_char: char,
/// include header row
pub include_header_row: bool,
/// include header separator row
pub include_header_separator_row: bool,
/// include summary row
pub include_summary_row: bool,
/// include summary separator row
pub include_summary_separator_row: bool,
/// render height
pub render_height: usize,
/// max render width
pub max_render_width: usize,
}
impl DataFrameFormat {
/// format dataframe as String
pub fn format(&self, df: DataFrame) -> Result<String, FormatError> {
let fmt = self.finalize(df.clone())?;
fmt.format(df)
}
/// fill missing format information based on dataframe
fn finalize(&self, df: DataFrame) -> Result<DataFrameFormatFinal, FormatError> {
let schema = df.schema();
let column_formats: Vec<ColumnFormat> = match &self.column_formats {
Some(cols) => {
let mut fmts = Vec::new();
for col in cols.iter() {
let dtype = match schema.get_field(col.name.as_str()) {
Some(field) => field.dtype,
None => {
return Err(FormatError::ColumnMissing(format!(
"missing column: {}",
col.name
)))
}
};
fmts.push(col.clone().finalize(&dtype)?);
}
fmts
}
None => {
let fmts: Result<Vec<ColumnFormat>, FormatError> = schema
.iter()
.map(|(name, dtype)| ColumnFormatShorthand::new().name(name).finalize(dtype))
.collect();
fmts?
}
};
let max_render_width = match self.max_render_width {
Some(value) => value,
None => {
let max_render_width = safe_sum_with_max_on_overflow(
column_formats.iter().map(|c| c.get_max_width()).collect(),
);
safe_sum_with_max_on_overflow(vec![
max_render_width,
self.column_delimiter.chars().count() * (column_formats.len() - 1),
])
}
};
let fmt = DataFrameFormatFinal {
column_formats,
column_delimiter: self.column_delimiter.clone(),
header_separator_delimiter: self.header_separator_delimiter.clone(),
header_separator_char: self.header_separator_char,
include_header_row: self.include_header_row,
include_header_separator_row: self.include_header_separator_row,
include_summary_row: self.include_summary_row,
include_summary_separator_row: self.include_summary_separator_row,
render_height: self.render_height.unwrap_or(DEFAULT_TABLE_HEIGHT),
max_render_width,
};
Ok(fmt)
}
}
fn safe_sum_with_max_on_overflow(numbers: Vec<usize>) -> usize {
let mut sum: usize = 0;
for number in numbers {
match sum.checked_add(number) {
Some(s) => sum = s,
None => return usize::MAX,
};
}
sum
}
// get number of lines in header
impl DataFrameFormatFinal {
fn n_header_lines(&self) -> usize {
// TODO: take an n_used_columns parameter, for if only subset of columns used
self.column_formats
.iter()
.map(|f| f.display_name.chars().filter(|&c| c == '\n').count() + 1)
.max()
.unwrap_or(0)
}
fn n_data_rows(&self) -> usize {
self.render_height -
(self.include_header_row as usize) *
(self.n_header_lines() + (self.include_header_separator_row as usize)) -
(self.include_summary_row as usize) *
(1 + (self.include_summary_separator_row as usize))
}
fn total_rendered_width(&self, used_widths: &Vec<usize>) -> usize {
used_widths.iter().sum::<usize>() +
((used_widths.len() as i64 - 1).max(0) as usize) *
self.column_delimiter.chars().count()
}
fn render_header_rows(&self, used_widths: &[usize], total_width: usize) -> Vec<String> {
let n_header_lines = self.n_header_lines();
let mut rows: Vec<String> =
(0..n_header_lines).map(|_| String::with_capacity(total_width)).collect();
for (c, width) in used_widths.iter().enumerate() {
if c != 0 {
for row in rows.iter_mut() {
row.push_str(self.column_delimiter.as_str());
}
}
let name = self.column_formats[c].display_name.as_str();
let lines: Vec<String> = name.split('\n').map(|s| s.to_string()).collect();
let bound = n_header_lines - lines.len();
for row in rows.iter_mut().take(bound) {
row.push_str(" ".repeat(*width).as_str());
}
for (row, line) in rows.iter_mut().skip(bound).zip(lines) {
row.push_str(format!("{:>width$}", line, width = width).as_str());
}
}
rows
}
fn render_header_separator_row(&self, used_widths: &[usize], total_width: usize) -> String {
let mut row = String::with_capacity(total_width);
let separator = self.header_separator_char.to_string();
for (c, width) in used_widths.iter().enumerate() {
if c != 0 {
row.push_str(self.header_separator_delimiter.as_str());
}
row.push_str(separator.repeat(*width).as_str());
}
row
}
fn render_columns(&self, df: DataFrame) -> Result<(Vec<usize>, Vec<Vec<String>>), FormatError> {
// compute global sizes
let mut column_min_widths: Vec<usize> = vec![];
let mut column_max_widths: Vec<usize> = vec![];
for fmt in self.column_formats.iter() {
let min_width = fmt.header_width().max(fmt.get_min_width());
let max_width = fmt.get_max_width();
if min_width > max_width {
let msg = format!("min_width > max_width for column: {}", fmt.display_name);
return Err(FormatError::InvalidFormat(msg));
}
column_min_widths.push(min_width);
column_max_widths.push(max_width);
}
let total_min_width = column_min_widths.iter().sum::<usize>() +
self.column_delimiter.chars().count() * (self.column_formats.len() - 1);
// let total_max_width = column_max_widths.iter().sum::<usize>();
// compute how many columns to include
let n_used_columns = if total_min_width >= self.max_render_width {
let mut n_used_columns = 0;
let mut used_width = 0;
for min_width in column_min_widths.iter() {
if used_width > 0 {
used_width += self.column_delimiter.chars().count();
}
if used_width + min_width <= self.max_render_width {
n_used_columns += 1;
used_width += min_width;
} else {
break;
}
}
n_used_columns
} else {
self.column_formats.len()
};
// let column_min_widths = column_min_widths.into_iter().take(n_used_columns);
// let column_max_widths = column_max_widths.into_iter().take(n_used_columns);
// compute used widths
let mut columns = Vec::with_capacity(n_used_columns);
let mut used_widths = Vec::with_capacity(n_used_columns);
let mut spare_room: usize = self.max_render_width -
column_min_widths.iter().take(n_used_columns).sum::<usize>() -
self.column_delimiter.chars().count() * ((n_used_columns as i64 - 1).max(0) as usize);
// println!("COLUMN_MIN_WIDTHS {:?}", column_min_widths);
// println!("TOTAL_MIN_WIDTHS {}",
// column_min_widths.iter().take(n_used_columns).sum::<usize>());
// println!("SPARE_ROOM {}", spare_room);
// println!("MAX_RENDER_WIDTH {:?}", self.max_render_width);
// println!(
// "MIN_TOTAL_W_DELIM {:?}",
// column_min_widths.iter().take(n_used_columns).sum::<usize>()
// + self.column_delimiter.chars().count()
// * ((n_used_columns as i64 - 1).max(0) as usize)
// );
// println!();
for (c, column_format) in self.column_formats.iter().take(n_used_columns).enumerate() {
if let (0, _) = df.shape() {
used_widths.push(column_min_widths[c]);
columns.push(vec![]);
continue
}
let min_width = column_min_widths[c];
let max_width = column_max_widths[c].min(min_width + spare_room);
let column = column_format
.clone()
.min_width(min_width)
.max_width(max_width)
.format(df.column(column_format.name.as_str())?)?;
let used_width = column
.iter()
.map(|s| s.chars().count())
// .map(|s| unicode_width::UnicodeWidthStr::width_cjk(s.as_str()))
// .map(|s| unicode_width::UnicodeWidthStr::width(s.as_str()))
.max()
.ok_or(FormatError::EmptyData(format!("empty column: {}", column_format.name)))?;
columns.push(column);
// println!("NAME {}", column_format.name);
// println!("FORMAT {:?}", column_format);
// println!("MAX_WIDTH {}", max_width);
// println!("MIN_WIDTH {}", min_width);
// println!("SPARE_ROOM {}", spare_room);
// println!("USED_WIDTH {}", used_width);
// println!("NEW_SPARE_ROOM {}", spare_room - (used_width - min_width));
// println!();
used_widths.push(used_width);
spare_room -= used_width - min_width;
}
Ok((used_widths, columns))
}
fn assemble_rows(&self, columns: Vec<Vec<String>>, rows: &mut Vec<String>, total_width: usize) {
let n_data_rows = match columns.first() {
Some(column) => column.len(),
None => return,
};
// println!("N_DATA_ROWS: {}", n_data_rows);
for r in 0..n_data_rows {
let mut row = String::with_capacity(total_width);
for (c, column) in columns.iter().enumerate() {
if c != 0 {
row.push_str(self.column_delimiter.as_str())
}
row.push_str(column[r].as_str())
}
rows.push(row)
}
}
pub(crate) fn format(&self, df: DataFrame) -> Result<String, FormatError> {
// clip
let n_data_rows = self.n_data_rows();
let df = df.clone().slice(0, n_data_rows);
// render columns
let (used_widths, columns) = self.render_columns(df)?;
let total_width = self.total_rendered_width(&used_widths);
// assemble rows
let mut rows = Vec::with_capacity(self.render_height);
if self.include_header_row {
for row in self.render_header_rows(&used_widths, total_width) {
rows.push(row);
}
if self.include_header_separator_row {
rows.push(self.render_header_separator_row(&used_widths, total_width));
}
};
self.assemble_rows(columns, &mut rows, total_width);
if self.include_summary_row {
todo!("summary row")
}
Ok(rows.join("\n"))
}
}
// // build header row
// let n_rows = self.n_rows.unwrap_or_else(|| df.height().min(20));
// let widths = determine_widths(&df, &columns)?;
// let total_width = widths.iter().sum();
// let mut header = String::with_capacity(total_width);
// let column_delimiter = self.column_delimiter.clone().unwrap_or(" ".to_string());
// for (i, (column, width)) in columns.iter().zip(widths).enumerate() {
// header.push_str(format!("{:>width$}", column.display_name, width = width).as_str());
// if i != columns.len() - 1 {
// header.push_str(column_delimiter.as_str());
// }
// }
// // convert numeric fields to float64
// for (name, dtype) in df.schema().iter() {
// if dtype.is_numeric() {
// df = df
// .clone()
// .with_column(df.column(name)?.to_float()?)?
// .clone();
// }
// }
// // print each row
// let mut rows = vec![];
// rows.push(header);
// for r in 0..n_rows {
// let mut row = String::new();
// for (c, column_format) in columns.iter().enumerate() {
// let df = df.clone();
// let column = df.column(column_format.name.as_str())?;
// let cell = format_cell(column, column_format, r)?;
// row.push_str(cell.as_str());
// if c != columns.len() - 1 {
// row.push_str(column_delimiter.as_str());
// }
// }
// rows.push(row);
// }
// Ok(rows.join("\n"))
// fn format_cell(
// column: &Series,
// column_format: &ColumnFormat,
// r: usize,
// ) -> Result<String, FormatError> {
// match column.dtype() {
// DataType::Binary => match column.binary()?.get(r) {
// Some(binary) => Ok(column_format.binary_format()?.format(binary)?),
// None => Ok("-".into()),
// },
// DataType::Utf8 => Ok(column.str_value(r)?.to_string()),
// DataType::Float64 => match column.f64()?.get(r) {
// Some(number) => Ok(column_format.number_format()?.format(number)?),
// None => Ok("-".into()),
// },
// DataType::Boolean => match column.bool()?.get(r) {
// Some(true) => Ok("yes".to_string()),
// Some(false) => Ok("no".to_string()),
// None => Ok("-".to_string()),
// },
// dtype => {
// let message = format!("column {} has type {}", column.name(), dtype);
// Err(FormatError::UnsupportedDatatype(message))
// }
// }
// }
// pub(crate) fn determine_widths(
// df: &DataFrame,
// columns: &Vec<ColumnFormat>,
// ) -> Result<Vec<usize>, FormatError> {
// let mut widths = Vec::with_capacity(columns.len());
// for column in columns.iter() {
// match column.min_width {
// Some(min_width) => widths.push(min_width),
// None => match df.schema().get(column.name.as_str()) {
// Some(dtype) => widths.push(get_dtype_default_width(dtype)),
// None => return Err(FormatError::ColumnMissing(column.name.to_string())),
// },
// }
// }
// Ok(widths)
// }
// pub(crate) fn get_dtype_default_width(dtype: &DataType) -> usize {
// match dtype {
// DataType::Boolean => 12,
// DataType::UInt8 => 12,
// DataType::UInt16 => 12,
// DataType::UInt32 => 12,
// DataType::UInt64 => 12,
// DataType::Int8 => 12,
// DataType::Int16 => 12,
// DataType::Int32 => 12,
// DataType::Int64 => 12,
// DataType::Float32 => 12,
// DataType::Float64 => 12,
// // DataType::Decimal(_precision, _scale) => 12,
// DataType::Utf8 => 12,
// DataType::Binary => 12,
// DataType::Date => 12,
// DataType::Datetime(_, _) => 12,
// DataType::Duration(_unit) => 12,
// DataType::Time => 12,
// // DataType::Array(_datatype, _size) => 12,
// DataType::List(_datatype) => 12,
// // DataType::Object(_) => 12,
// DataType::Null => 12,
// // DataType::Categorical(_) => 12,
// DataType::Struct(_fields) => 12,
// DataType::Unknown => 12,
// }
// }