| 1 | # Comfy-table |
| 2 | |
| 3 | [](https://github.com/Nukesor/comfy-table/actions/workflows/test.yml) |
| 4 | [](https://docs.rs/comfy-table/) |
| 5 | [](https://github.com/nukesor/comfy-table/blob/main/LICENSE) |
| 6 | [](https://crates.io/crates/comfy-table) |
| 7 | [](https://codecov.io/gh/nukesor/comfy-table) |
| 8 | |
| 9 |  |
| 10 | |
| 11 | <!--- [](https://deps.rs/repo/github/nukesor/comfy-table) --> |
| 12 | |
| 13 | Comfy-table is designed as a library for building beautiful terminal tables, while being easy to use. |
| 14 | |
| 15 | ## Table of Contents |
| 16 | |
| 17 | - [Features](#features) |
| 18 | - [Examples](#examples) |
| 19 | - [Feature Flags](#feature-flags) |
| 20 | - [Contributing](#contributing) |
| 21 | - [Usage of unsafe](#unsafe) |
| 22 | - [Comparison with other libraries](#comparison-with-other-libraries) |
| 23 | |
| 24 | ## State of the Project |
| 25 | |
| 26 | Comfy-table can be considered "finished". |
| 27 | It contains all major features that were planned and received a lot of additional features along the way! |
| 28 | As far as I'm aware, there're no lingering bugs and the project has a lot of test coverage. |
| 29 | |
| 30 | "Finished" means: |
| 31 | |
| 32 | - Comfy-table still receives regular version bumps for its few dependencies. |
| 33 | - Pull requests for wished for and approved features will be reviewed and eventually merged. |
| 34 | - New releases are pushed when new features are added or when requested. |
| 35 | |
| 36 | ## Features |
| 37 | |
| 38 | - Dynamic arrangement of content depending on a given width. |
| 39 | - ANSI content styling for terminals (Colors, Bold, Blinking, etc.). |
| 40 | - Styling Presets and preset modifiers to get you started. |
| 41 | - Pretty much every part of the table is customizable (borders, lines, padding, alignment). |
| 42 | - Constraints on columns that allow some additional control over how to arrange content. |
| 43 | - Cross platform (Linux, macOS, Windows). |
| 44 | - It's fast enough. |
| 45 | - Benchmarks show that a pretty big table with complex constraints is build in `470μs` or `~0.5ms`. |
| 46 | - The table seen at the top of the readme takes `~30μs`. |
| 47 | - These numbers are from a overclocked `i7-8700K` with a max single-core performance of 4.9GHz. |
| 48 | - To run the benchmarks yourselves, install criterion via `cargo install cargo-criterion` and run `cargo criterion` afterwards. |
| 49 | |
| 50 | Comfy-table is written for the current `stable` Rust version. |
| 51 | Older Rust versions may work but aren't officially supported. |
| 52 | |
| 53 | ## Examples |
| 54 | |
| 55 | ```rust |
| 56 | use comfy_table::Table; |
| 57 | |
| 58 | fn main() { |
| 59 | let mut table = Table::new(); |
| 60 | table |
| 61 | .set_header(vec!["Header1", "Header2", "Header3"]) |
| 62 | .add_row(vec![ |
| 63 | "This is a text", |
| 64 | "This is another text", |
| 65 | "This is the third text", |
| 66 | ]) |
| 67 | .add_row(vec![ |
| 68 | "This is another text", |
| 69 | "Now\nadd some\nmulti line stuff", |
| 70 | "This is awesome", |
| 71 | ]); |
| 72 | |
| 73 | println!("{table}"); |
| 74 | } |
| 75 | ``` |
| 76 | |
| 77 | Create a very basic table.\ |
| 78 | This table will become as wide as your content. Nothing fancy happening here. |
| 79 | |
| 80 | ```text,ignore |
| 81 | +----------------------+----------------------+------------------------+ |
| 82 | | Header1 | Header2 | Header3 | |
| 83 | +======================================================================+ |
| 84 | | This is a text | This is another text | This is the third text | |
| 85 | |----------------------+----------------------+------------------------| |
| 86 | | This is another text | Now | This is awesome | |
| 87 | | | add some | | |
| 88 | | | multi line stuff | | |
| 89 | +----------------------+----------------------+------------------------+ |
| 90 | ``` |
| 91 | |
| 92 | ### More Features |
| 93 | |
| 94 | ```rust |
| 95 | use comfy_table::modifiers::UTF8_ROUND_CORNERS; |
| 96 | use comfy_table::presets::UTF8_FULL; |
| 97 | use comfy_table::*; |
| 98 | |
| 99 | fn main() { |
| 100 | let mut table = Table::new(); |
| 101 | table |
| 102 | .load_preset(UTF8_FULL) |
| 103 | .apply_modifier(UTF8_ROUND_CORNERS) |
| 104 | .set_content_arrangement(ContentArrangement::Dynamic) |
| 105 | .set_width(40) |
| 106 | .set_header(vec!["Header1", "Header2", "Header3"]) |
| 107 | .add_row(vec![ |
| 108 | Cell::new("Center aligned").set_alignment(CellAlignment::Center), |
| 109 | Cell::new("This is another text"), |
| 110 | Cell::new("This is the third text"), |
| 111 | ]) |
| 112 | .add_row(vec![ |
| 113 | "This is another text", |
| 114 | "Now\nadd some\nmulti line stuff", |
| 115 | "This is awesome", |
| 116 | ]); |
| 117 | |
| 118 | // Set the default alignment for the third column to right |
| 119 | let column = table.column_mut(2).expect("Our table has three columns"); |
| 120 | column.set_cell_alignment(CellAlignment::Right); |
| 121 | |
| 122 | println!("{table}"); |
| 123 | } |
| 124 | ``` |
| 125 | |
| 126 | Create a table with UTF8 styling, and apply a modifier that gives the table round corners.\ |
| 127 | Additionally, the content will dynamically wrap to maintain a given table width.\ |
| 128 | If the table width isn't explicitly set and the program runs in a terminal, the terminal size will be used. |
| 129 | |
| 130 | On top of this, we set the default alignment for the right column to `Right` and the alignment of the left top cell to `Center`. |
| 131 | |
| 132 | ```text,ignore |
| 133 | ╭────────────┬────────────┬────────────╮ |
| 134 | │ Header1 ┆ Header2 ┆ Header3 │ |
| 135 | ╞════════════╪════════════╪════════════╡ |
| 136 | │ This is a ┆ This is ┆ This is │ |
| 137 | │ text ┆ another ┆ the third │ |
| 138 | │ ┆ text ┆ text │ |
| 139 | ├╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌┤ |
| 140 | │ This is ┆ Now ┆ This is │ |
| 141 | │ another ┆ add some ┆ awesome │ |
| 142 | │ text ┆ multi line ┆ │ |
| 143 | │ ┆ stuff ┆ │ |
| 144 | ╰────────────┴────────────┴────────────╯ |
| 145 | ``` |
| 146 | |
| 147 | ### Styling |
| 148 | |
| 149 | ```rust |
| 150 | use comfy_table::presets::UTF8_FULL; |
| 151 | use comfy_table::*; |
| 152 | |
| 153 | fn main() { |
| 154 | let mut table = Table::new(); |
| 155 | table.load_preset(UTF8_FULL) |
| 156 | .set_content_arrangement(ContentArrangement::Dynamic) |
| 157 | .set_width(80) |
| 158 | .set_header(vec![ |
| 159 | Cell::new("Header1").add_attribute(Attribute::Bold), |
| 160 | Cell::new("Header2").fg(Color::Green), |
| 161 | Cell::new("Header3"), |
| 162 | ]) |
| 163 | .add_row(vec![ |
| 164 | Cell::new("This is a bold text").add_attribute(Attribute::Bold), |
| 165 | Cell::new("This is a green text").fg(Color::Green), |
| 166 | Cell::new("This one has black background").bg(Color::Black), |
| 167 | ]) |
| 168 | .add_row(vec![ |
| 169 | Cell::new("Blinky boi").add_attribute(Attribute::SlowBlink), |
| 170 | Cell::new("This table's content is dynamically arranged. The table is exactly 80 characters wide.\nHere comes a reallylongwordthatshoulddynamicallywrap"), |
| 171 | Cell::new("COMBINE ALL THE THINGS") |
| 172 | .fg(Color::Green) |
| 173 | .bg(Color::Black) |
| 174 | .add_attributes(vec![ |
| 175 | Attribute::Bold, |
| 176 | Attribute::SlowBlink, |
| 177 | ]) |
| 178 | ]); |
| 179 | |
| 180 | println!("{table}"); |
| 181 | } |
| 182 | ``` |
| 183 | |
| 184 | This code generates the table that can be seen at the top of this document. |
| 185 | |
| 186 | ### Code Examples |
| 187 | |
| 188 | A few examples can be found in the `example` folder. |
| 189 | To test an example, run `cargo run --example $name`. E.g.: |
| 190 | |
| 191 | ```bash |
| 192 | cargo run --example readme_table |
| 193 | ``` |
| 194 | |
| 195 | If you're looking for more information, take a look at the [tests folder](https://github.com/Nukesor/comfy-table/tree/main/tests). |
| 196 | There are tests for almost every feature including a visual view for each resulting table. |
| 197 | |
| 198 | ## Feature Flags |
| 199 | |
| 200 | ### `tty` (enabled) |
| 201 | |
| 202 | This flag enables support for terminals. In detail this means: |
| 203 | |
| 204 | - Automatic detection whether we're in a terminal environment. |
| 205 | Only used when no explicit `Table::set_width` is provided. |
| 206 | - Support for ANSI Escape Code styling for terminals. |
| 207 | |
| 208 | ### `custom_styling` (disabled) |
| 209 | |
| 210 | This flag enables support for custom styling of text inside of cells. |
| 211 | |
| 212 | - Text formatting still works, even if you roll your own ANSI escape sequences. |
| 213 | - Rainbow text |
| 214 | - Makes comfy-table 30-50% slower |
| 215 | |
| 216 | ### `reexport_crossterm` (disabled) |
| 217 | |
| 218 | With this flag, comfy-table re-exposes crossterm's [`Attribute`](https://docs.rs/crossterm/latest/crossterm/style/enum.Attribute.html) and [`Color`](https://docs.rs/crossterm/latest/crossterm/style/enum.Color.html) enum. |
| 219 | By default, a mirrored type is exposed, which internally maps to the crossterm type. |
| 220 | |
| 221 | This feature is very convenient if you use both comfy-table and crossterm in your code and want to use crossterm's types for everything interchangeably. |
| 222 | |
| 223 | **BUT** if you enable this feature, you opt-in for breaking changes on minor/patch versions. |
| 224 | Meaning, you have to update crossterm whenever you update comfy-table and you **cannot** update crossterm until comfy-table released a new version with that crossterm version. |
| 225 | |
| 226 | ## Contributing |
| 227 | |
| 228 | Comfy-table's main focus is on being minimalistic and reliable. |
| 229 | A fixed set of features that just work for "normal" use-cases: |
| 230 | |
| 231 | - Normal tables (columns, rows, one cell per column/row). |
| 232 | - Dynamic arrangement of content to a given width. |
| 233 | - Some kind of manual intervention in the arrangement process. |
| 234 | |
| 235 | If you come up with an idea or an improvement that fits into the current scope of the project, feel free to create an issue :)! |
| 236 | |
| 237 | Some things however will most likely not be added to the project since they drastically increase the complexity of the library or cover very specific edge-cases. |
| 238 | |
| 239 | Such features are: |
| 240 | |
| 241 | - Nested tables |
| 242 | - Cells that span over multiple columns/rows |
| 243 | - CSV to table conversion and vice versa |
| 244 | |
| 245 | ## Unsafe |
| 246 | |
| 247 | Comfy-table doesn't allow `unsafe` code in its code-base. |
| 248 | As it's a "simple" formatting library it also shouldn't be needed in the future. |
| 249 | |
| 250 | If one disables the `tty` feature flag, this is also true for all of its dependencies. |
| 251 | |
| 252 | However, when enabling `tty`, Comfy-table uses one unsafe function call in its dependencies. \ |
| 253 | It can be circumvented by explicitly calling [Table::force_no_tty](https://docs.rs/comfy-table/latest/comfy_table/struct.Table.html#method.force_no_tty). |
| 254 | |
| 255 | 1. `crossterm::terminal::size`. This function is necessary to detect the current terminal width if we're on a tty. |
| 256 | This is only called if no explicit width is provided via `Table::set_width`. |
| 257 | |
| 258 | <http://rosettacode.org/wiki/Terminal_control/Dimensions#Library:_BSD_libc> |
| 259 | This is another libc call which is used to communicate with `/dev/tty` via a file descriptor. |
| 260 | |
| 261 | ```rust,ignore |
| 262 | ... |
| 263 | if wrap_with_result(unsafe { ioctl(fd, TIOCGWINSZ.into(), &mut size) }).is_ok() { |
| 264 | Ok((size.ws_col, size.ws_row)) |
| 265 | } else { |
| 266 | tput_size().ok_or_else(|| std::io::Error::last_os_error().into()) |
| 267 | } |
| 268 | ... |
| 269 | ``` |
| 270 | |
| 271 | ## Comparison with other libraries |
| 272 | |
| 273 | The following are official statements of the other crate authors. |
| 274 | [This ticket](https://github.com/Nukesor/comfy-table/issues/76) can be used as an entry to find all other sibling tickets in the other projects. |
| 275 | |
| 276 | ### Cli-table |
| 277 | |
| 278 | The main focus of [`cli-table`](https://crates.io/crates/cli-table) is to support all platforms and at the same time limit the dependencies to keep the compile times and crate size low. |
| 279 | |
| 280 | Currently, this crate only pulls two external dependencies (other than cli-table-derive): |
| 281 | |
| 282 | - termcolor |
| 283 | - unicode-width |
| 284 | |
| 285 | With csv feature enabled, it also pulls csv crate as dependency. |
| 286 | |
| 287 | ### Term-table |
| 288 | |
| 289 | [`term-table`](https://crates.io/crates/term-table) is pretty basic in terms of features. |
| 290 | My goal with the project is to provide a good set of tools for rendering CLI tables, while also allowing users to bring their own tools for things like colours. |
| 291 | One thing that is unique to `term-table` (as far as I'm aware) is the ability to have different number of columns in each row of the table. |
| 292 | |
| 293 | ### Prettytables-rs |
| 294 | |
| 295 | [`prettytables-rs`](https://crates.io/crates/prettytable-rs) provides functionality for formatting and aligning tables. |
| 296 | It his however abandoned since over three years and a [rustsec/advisory-db](https://github.com/rustsec/advisory-db/issues/1173) entry has been requested. |
| 297 | |
| 298 | ### Comfy-table |
| 299 | |
| 300 | One of [`comfy-table`](https://crates.io/crates/comfy-table)'s big foci is on providing a minimalistic, but rock-solid library for building text-based tables. |
| 301 | This means that the code is very well tested, no usage of `unsafe` and `unwrap` is only used if we can be absolutely sure that it's safe. |
| 302 | There're only one occurrence of `unsafe` in all of comfy-table's dependencies, to be exact inside the `tty` communication code, which can be explicitly disabled. |
| 303 | |
| 304 | The other focus is on dynamic-length content arrangement. |
| 305 | This means that a lot of work went into building an algorithm that finds a (near) optimal table layout for any given text and terminal width. |