Skip to content

Commit

Permalink
dataframe display: use POLARS_FMT_STR_LEN (#4088)
Browse files Browse the repository at this point in the history
  • Loading branch information
ritchie46 committed Jul 20, 2022
1 parent 0910ec3 commit b0c0c0e
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 13 deletions.
3 changes: 3 additions & 0 deletions polars/polars-core/src/config.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
pub(crate) const FMT_STR_LEN: &str = "POLARS_FMT_STR_LEN";
pub(crate) const FMT_MAX_COLS: &str = "POLARS_FMT_MAX_COLS";
pub(crate) const FMT_MAX_ROWS: &str = "POLARS_FMT_MAX_ROWS";
37 changes: 24 additions & 13 deletions polars/polars-core/src/fmt.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use crate::config::{FMT_MAX_COLS, FMT_MAX_ROWS, FMT_STR_LEN};
use crate::prelude::*;

#[cfg(feature = "timezones")]
Expand Down Expand Up @@ -35,7 +36,7 @@ macro_rules! format_array {
)?;
let truncate = matches!($a.dtype(), DataType::Utf8);
let truncate_len = if truncate {
std::env::var("POLARS_FMT_STR_LEN")
std::env::var(FMT_STR_LEN)
.as_deref()
.unwrap_or("")
.parse()
Expand Down Expand Up @@ -277,11 +278,10 @@ impl Debug for DataFrame {
}
}
#[cfg(feature = "fmt")]
fn make_str_val(v: &str) -> String {
let string_limit = 32;
fn make_str_val(v: &str, truncate: usize) -> String {
let v_trunc = &v[..v
.char_indices()
.take(string_limit)
.take(truncate)
.last()
.map(|(i, c)| i + c.len_utf8())
.unwrap_or(0)];
Expand All @@ -293,17 +293,22 @@ fn make_str_val(v: &str) -> String {
}

#[cfg(feature = "fmt")]
fn prepare_row(row: Vec<Cow<'_, str>>, n_first: usize, n_last: usize) -> Vec<String> {
fn prepare_row(
row: Vec<Cow<'_, str>>,
n_first: usize,
n_last: usize,
str_truncate: usize,
) -> Vec<String> {
let reduce_columns = n_first + n_last < row.len();
let mut row_str = Vec::with_capacity(n_first + n_last + reduce_columns as usize);
for v in row[0..n_first].iter() {
row_str.push(make_str_val(v));
row_str.push(make_str_val(v, str_truncate));
}
if reduce_columns {
row_str.push("...".to_string());
}
for v in row[row.len() - n_last..].iter() {
row_str.push(make_str_val(v));
row_str.push(make_str_val(v, str_truncate));
}
row_str
}
Expand All @@ -318,14 +323,20 @@ impl Display for DataFrame {
"The columns lengths in the DataFrame are not equal."
);

let max_n_cols = std::env::var("POLARS_FMT_MAX_COLS")
let str_truncate = std::env::var(FMT_STR_LEN)
.as_deref()
.unwrap_or("")
.parse()
.unwrap_or(32);

let max_n_cols = std::env::var(FMT_MAX_COLS)
.as_deref()
.unwrap_or("")
.parse()
.unwrap_or(8);

let max_n_rows = {
let max_n_rows = std::env::var("POLARS_FMT_MAX_ROWS")
let max_n_rows = std::env::var(FMT_MAX_ROWS)
.as_deref()
.unwrap_or("")
.parse()
Expand All @@ -346,7 +357,7 @@ impl Display for DataFrame {
let mut names = Vec::with_capacity(n_first + n_last + reduce_columns as usize);

let field_to_str = |f: &Field| {
let name = make_str_val(f.name());
let name = make_str_val(f.name(), str_truncate);
let lower_bounds = std::cmp::max(5, std::cmp::min(12, name.len()));
let s = format!("{}\n---\n{}", name, f.data_type());
(s, lower_bounds)
Expand Down Expand Up @@ -385,13 +396,13 @@ impl Display for DataFrame {
if self.height() > max_n_rows {
for i in 0..(max_n_rows / 2) {
let row = self.columns.iter().map(|s| s.str_value(i)).collect();
rows.push(prepare_row(row, n_first, n_last));
rows.push(prepare_row(row, n_first, n_last, str_truncate));
}
let dots = rows[0].iter().map(|_| "...".to_string()).collect();
rows.push(dots);
for i in (self.height() - (max_n_rows + 1) / 2)..self.height() {
let row = self.columns.iter().map(|s| s.str_value(i)).collect();
rows.push(prepare_row(row, n_first, n_last));
rows.push(prepare_row(row, n_first, n_last, str_truncate));
}
for row in rows {
table.add_row(row);
Expand All @@ -400,7 +411,7 @@ impl Display for DataFrame {
for i in 0..self.height() {
if self.width() > 0 {
let row = self.columns.iter().map(|s| s.str_value(i)).collect();
table.add_row(prepare_row(row, n_first, n_last));
table.add_row(prepare_row(row, n_first, n_last, str_truncate));
} else {
break;
}
Expand Down
1 change: 1 addition & 0 deletions polars/polars-core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ extern crate core;
#[macro_use]
pub mod utils;
pub mod chunked_array;
pub(crate) mod config;
pub mod datatypes;
#[cfg(feature = "docs")]
pub mod doc;
Expand Down

0 comments on commit b0c0c0e

Please sign in to comment.