# cols
Smart adaptive formatting of columnar data for Rust.
`cols` is designed for CLI tools that need terminal-aware table output with
tree rendering, multiple output formats, and built-in filtering. Think
`lsblk`, `lsmem`, or `pstree`.
[Documentation](https://rustutils.gitlab.io/cols/) |
[API Reference](https://rustutils.gitlab.io/cols/api/cols/) |
[Crates.io](https://crates.io/crates/cols)
## Quick start
```toml
[dependencies]
cols = "0.2"
```
```rust
use cols::{Table, Column, print_table_to_string};
let mut table = Table::new();
table.add_column(Column::new("NAME").width_fixed(10));
table.add_column(Column::new("SIZE").width_fixed(6).right(true));
let l1 = table.new_line(None);
table.line_mut(l1).data_set(0, "sda").data_set(1, "100G");
let l2 = table.new_line(None);
table.line_mut(l2).data_set(0, "sdb").data_set(1, "50G");
let output = print_table_to_string(&table).unwrap();
```
```text
NAME SIZE
sda 100G
sdb 50G
```
## Tree rendering
```rust
use cols::{Table, Column, print_table_to_string};
let mut table = Table::new();
table.add_column(Column::new("NAME").tree(true));
table.add_column(Column::new("SIZE").right(true));
let root = table.new_line(None);
table.line_mut(root).data_set(0, "root").data_set(1, "100");
let c1 = table.new_line(Some(root));
table.line_mut(c1).data_set(0, "child1").data_set(1, "50");
let gc = table.new_line(Some(c1));
table.line_mut(gc).data_set(0, "grandchild").data_set(1, "20");
let c2 = table.new_line(Some(root));
table.line_mut(c2).data_set(0, "child2").data_set(1, "30");
```
```text
NAME SIZE
root 100
├─child1 50
│ └─grandchild 20
└─child2 30
```
## Multiple output modes
The same table can be rendered in seven formats without rebuilding it:
```rust
use cols::OutputMode;
table.output_mode_set(OutputMode::Json);
```
**Normal** — human-readable with column alignment and padding.
**Raw** — compact, separator-delimited.
**Export** — `NAME="value"` pairs.
**ShellVar** — like Export with shell-safe column names.
**JSON** — typed JSON array of objects.
**CSV** — RFC 4180 with automatic quoting.
**Markdown** — pipe-delimited with alignment support.
## Features
- **Adaptive column widths** with fixed, fractional, or content-sized hints
- **Tree rendering** with Unicode or ASCII connectors
- **7 output modes**: Normal, Raw, Export, JSON, ShellVar, CSV, Markdown
- **Group brackets** for M:N member/child relationships
- **Filter expressions** with boolean logic, comparisons, regex, and integer
suffixes (K/M/G/T)
- **Sorting** with custom comparators
- **Column alignment**: left, center, right (column default with per-cell
override)
- **Streaming mode**: output rows incrementally without buffering the whole table
- **Builder API**: `Column::new("NAME").width_fixed(10).right(true).truncate(true)`
## Examples
The `examples/` directory contains runnable demos:
```sh
cargo run --example ls # simple file listing
cargo run --example tree # directory tree
cargo run --example json # all output modes
cargo run --example filter # filter expressions
cargo run --example groups # group bracket rendering
cargo run --example sort # alphabetic vs numeric sorting
cargo run --example wrap # column wrapping
```
## License
Licensed under either of [Apache License, Version 2.0](LICENSE-APACHE.md) or
[MIT License](LICENSE-MIT.md) at your option.