5 unstable releases
Uses new Rust 2024
| 0.3.2 | Apr 2, 2026 |
|---|---|
| 0.3.1 | Apr 1, 2026 |
| 0.3.0 | Mar 31, 2026 |
| 0.2.0 | Mar 30, 2026 |
| 0.1.0 | Mar 30, 2026 |
#269 in Text processing
32 downloads per month
Used in 11 crates
(10 directly)
190KB
4.5K
SLoC
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 | API Reference | Crates.io
Quick start
[dependencies]
cols = "0.2"
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();
NAME SIZE
sda 100G
sdb 50G
Tree rendering
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");
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:
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:
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 or MIT License at your option.
Dependencies
~1.4–3.5MB
~61K SLoC