Skip to content

Commit 043aa34

Browse files
committed
Adding Rust set_ascii_tables
1 parent 2431104 commit 043aa34

File tree

4 files changed

+91
-2
lines changed

4 files changed

+91
-2
lines changed

__tests__/config.test.ts

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import pl from "@polars";
2+
3+
describe("config", () => {
4+
pl.Config.setAsciiTables(true);
5+
6+
test("setAsciiTables", () => {
7+
const df = pl.DataFrame({
8+
int: [1, 2],
9+
str: ["a", "b"],
10+
bool: [true, null],
11+
list: [[1, 2], [3]],
12+
});
13+
14+
const asciiTable = `shape: (2, 4)
15+
+-----+-----+------+------------+
16+
| int | str | bool | list |
17+
| --- | --- | --- | --- |
18+
| f64 | str | bool | list[f64] |
19+
+===============================+
20+
| 1.0 | a | true | [1.0, 2.0] |
21+
| 2.0 | b | null | [3.0] |
22+
+-----+-----+------+------------+`;
23+
24+
pl.Config.setAsciiTables(true);
25+
expect(df.toString()).toEqual(asciiTable);
26+
27+
pl.Config.setAsciiTables(false);
28+
29+
const utf8Table = `shape: (2, 4)
30+
┌─────┬─────┬──────┬────────────┐
31+
│ int ┆ str ┆ bool ┆ list │
32+
│ --- ┆ --- ┆ --- ┆ --- │
33+
│ f64 ┆ str ┆ bool ┆ list[f64] │
34+
╞═════╪═════╪══════╪════════════╡
35+
│ 1.0 ┆ a ┆ true ┆ [1.0, 2.0] │
36+
│ 2.0 ┆ b ┆ null ┆ [3.0] │
37+
└─────┴─────┴──────┴────────────┘`;
38+
39+
expect(df.toString()).toEqual(utf8Table);
40+
pl.Config.setAsciiTables();
41+
42+
expect(df.toString()).toEqual(utf8Table);
43+
});
44+
});

polars/cfg.ts

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import pli from "./internals/polars_internal";
2+
13
/**
24
* Configure polars; offers options for table formatting and more.
35
*/
@@ -6,12 +8,38 @@ export interface Config {
68
setUtf8Tables(): Config;
79
/** Use ascii characters to print tables */
810
setAsciiTables(): Config;
11+
setAsciiTables(active: boolean): Config;
912
/** Set the number of character used to draw the table */
1013
setTblWidthChars(width: number): Config;
1114
/** Set the number of rows used to print tables */
1215
setTblRows(n: number): Config;
1316
/** Set the number of columns used to print tables */
1417
setTblCols(n: number): Config;
18+
19+
// set_auto_structify
20+
// set_decimal_separator
21+
// set_engine_affinity
22+
// set_float_precision
23+
// set_fmt_float
24+
// set_fmt_str_lengths
25+
// set_fmt_table_cell_list_len
26+
// set_streaming_chunk_size
27+
// set_tbl_cell_alignment
28+
// set_tbl_cell_numeric_alignment
29+
// set_tbl_cols
30+
// set_tbl_column_data_type_inline
31+
// set_tbl_dataframe_shape_below
32+
// set_tbl_formatting
33+
// set_tbl_hide_column_data_types
34+
// set_tbl_hide_column_names
35+
// set_tbl_hide_dataframe_shape
36+
// set_tbl_hide_dtype_separator
37+
// set_tbl_rows
38+
// set_tbl_width_chars
39+
// set_thousands_separator
40+
// set_trim_decimal_zeros
41+
// set_verbose
42+
1543
}
1644

1745
/**
@@ -22,8 +50,8 @@ export const Config: Config = {
2250
process.env["POLARS_FMT_NO_UTF8"] = undefined;
2351
return this;
2452
},
25-
setAsciiTables() {
26-
process.env["POLARS_FMT_NO_UTF8"] = "1";
53+
setAsciiTables(active?: boolean) {
54+
pli.setAsciiTables(active);
2755
return this;
2856
},
2957
setTblWidthChars(width) {

src/cfg.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
2+
/// Set the `POLARS_FMT_TABLE_FORMATTING` environment variable.
3+
///
4+
/// - `Some(true)` => `"ASCII_FULL_CONDENSED"`
5+
/// - `Some(false)` => `"UTF8_FULL_CONDENSED"`
6+
/// - `None` => remove the variable
7+
#[napi(catch_unwind)]
8+
pub fn set_ascii_tables(enabled: Option<bool>) {
9+
const ENV_VAR: &str = "POLARS_FMT_TABLE_FORMATTING";
10+
11+
match enabled {
12+
Some(true) => std::env::set_var(ENV_VAR, "ASCII_FULL_CONDENSED"),
13+
Some(false) => std::env::set_var(ENV_VAR, "UTF8_FULL_CONDENSED"),
14+
None => std::env::remove_var(ENV_VAR),
15+
}
16+
}

src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ pub mod series;
2323
pub mod set;
2424
pub mod sql;
2525
pub mod utils;
26+
pub mod cfg;
2627

2728
pub use polars_core;
2829

0 commit comments

Comments
 (0)