Skip to content

Commit

Permalink
feat(rust): Add "fmt_no_tty" feature for formatting support without r… (
Browse files Browse the repository at this point in the history
  • Loading branch information
ghuls committed Dec 6, 2022
1 parent 05afe5b commit b49a3b9
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 11 deletions.
1 change: 1 addition & 0 deletions polars/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ performant = ["polars-core/performant", "chunked_ids", "dtype-u8", "dtype-u16",

# Dataframe formatting.
fmt = ["polars-core/fmt"]
fmt_no_tty = ["polars-core/fmt_no_tty"]

# sort by multiple columns
sort_multiple = ["polars-core/sort_multiple"]
Expand Down
5 changes: 3 additions & 2 deletions polars/polars-core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ string_encoding = ["base64", "hex"]
# support for ObjectChunked<T> (downcastable Series of any type)
object = ["serde_json"]

fmt = ["comfy-table"]
fmt = ["comfy-table/tty"]
fmt_no_tty = ["comfy-table"]

# opt-in features
# sort by multiple columns
Expand Down Expand Up @@ -151,7 +152,7 @@ base64 = { version = "0.13", optional = true }
bitflags.workspace = true
chrono = { version = "0.4", optional = true }
chrono-tz = { version = "0.6", optional = true }
comfy-table = { version = "6.1.1", optional = true }
comfy-table = { version = "6.1.1", optional = true, default_features = false }
hashbrown.workspace = true
hex = { version = "0.4", optional = true }
indexmap = { version = "1", features = ["std"] }
Expand Down
23 changes: 14 additions & 9 deletions polars/polars-core/src/fmt.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#[cfg(feature = "fmt")]
#[cfg(any(feature = "fmt", feature = "fmt_no_tty"))]
use std::borrow::Cow;
use std::fmt;
use std::fmt::{Debug, Display, Formatter};
Expand All @@ -11,9 +11,9 @@ use std::fmt::{Debug, Display, Formatter};
use arrow::temporal_conversions::*;
#[cfg(feature = "timezones")]
use chrono::TimeZone;
#[cfg(feature = "fmt")]
#[cfg(any(feature = "fmt", feature = "fmt_no_tty"))]
use comfy_table::presets::*;
#[cfg(feature = "fmt")]
#[cfg(any(feature = "fmt", feature = "fmt_no_tty"))]
use comfy_table::*;
use num::{Num, NumCast};

Expand Down Expand Up @@ -295,7 +295,7 @@ impl Debug for DataFrame {
Display::fmt(self, f)
}
}
#[cfg(feature = "fmt")]
#[cfg(any(feature = "fmt", feature = "fmt_no_tty"))]
fn make_str_val(v: &str, truncate: usize) -> String {
let v_trunc = &v[..v
.char_indices()
Expand All @@ -310,7 +310,7 @@ fn make_str_val(v: &str, truncate: usize) -> String {
}
}

#[cfg(feature = "fmt")]
#[cfg(any(feature = "fmt", feature = "fmt_no_tty"))]
fn prepare_row(
row: Vec<Cow<'_, str>>,
n_first: usize,
Expand All @@ -337,7 +337,7 @@ fn env_is_true(varname: &str) -> bool {

impl Display for DataFrame {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
#[cfg(feature = "fmt")]
#[cfg(any(feature = "fmt", feature = "fmt_no_tty"))]
{
let height = self.height();
assert!(
Expand Down Expand Up @@ -499,7 +499,12 @@ impl Display for DataFrame {

// if no tbl_width (its not-tty && it is not explicitly set), then set default.
// this is needed to support non-tty applications
if !table.is_tty() && table.width().is_none() {
#[cfg(feature = "fmt")]
if table.width().is_none() && !table.is_tty() {
table.set_width(100);
}
#[cfg(feature = "fmt_no_tty")]
if table.width().is_none() {
table.set_width(100);
}

Expand Down Expand Up @@ -531,11 +536,11 @@ impl Display for DataFrame {
}
}

#[cfg(not(feature = "fmt"))]
#[cfg(not(any(feature = "fmt", feature = "fmt_no_tty")))]
{
write!(
f,
"shape: {:?}\nto see more, compile with the 'fmt' feature",
"shape: {:?}\nto see more, compile with the 'fmt' or 'fmt_no_tty' feature",
self.shape()
)?;
}
Expand Down

0 comments on commit b49a3b9

Please sign in to comment.