| 1 |
[](https://crates.io/crates/uutils-term-grid) |
| 2 |
[](https://deps.rs/repo/github/uutils/uutils-term-grid) |
| 3 |
[](https://codecov.io/gh/uutils/uutils-term-grid) |
| 4 |
[](https://codspeed.io/uutils/uutils-term-grid) |
| 5 |
|
| 6 |
# uutils-term-grid |
| 7 |
|
| 8 |
This library arranges textual data in a grid format suitable for fixed-width |
| 9 |
fonts, using an algorithm to minimise the amount of space needed. |
| 10 |
|
| 11 |
--- |
| 12 |
|
| 13 |
This library is forked from the unmaintained |
| 14 |
[`rust-term-grid`](https://github.com/ogham/rust-term-grid) library. The core |
| 15 |
functionality has remained the same, with some additional bugfixes, performance |
| 16 |
improvements and a new API. |
| 17 |
|
| 18 |
--- |
| 19 |
|
| 20 |
# Installation |
| 21 |
|
| 22 |
This crate works with `cargo`. Add the following to your `Cargo.toml` |
| 23 |
dependencies section: |
| 24 |
|
| 25 |
```toml |
| 26 |
[dependencies] |
| 27 |
uutils_term_grid = "0.7" |
| 28 |
``` |
| 29 |
|
| 30 |
The Minimum Supported Rust Version is 1.70. |
| 31 |
|
| 32 |
## Creating a grid |
| 33 |
|
| 34 |
To add data to a grid, first create a new [`Grid`] value with a list of strings |
| 35 |
and a set of options. |
| 36 |
|
| 37 |
There are three options that must be specified in the [`GridOptions`] value that |
| 38 |
dictate how the grid is formatted: |
| 39 |
|
| 40 |
- [`filling`][filling]: how to fill empty space between columns: |
| 41 |
- [`Filling::Spaces`][Spaces] number of spaces between columns; |
| 42 |
- [`Filling::Text`][Text] text string separator between columns; |
| 43 |
- [`Filling::Tabs`][Tabs] special option which allows to set number of spaces between columns and set the size of `\t` character. |
| 44 |
- [`direction`][direction]: specifies whether the cells should go along rows, or |
| 45 |
columns: |
| 46 |
- [`Direction::LeftToRight`][LeftToRight] starts them in the top left and |
| 47 |
moves _rightwards_, going to the start of a new row after reaching the final |
| 48 |
column; |
| 49 |
- [`Direction::TopToBottom`][TopToBottom] starts them in the top left and |
| 50 |
moves _downwards_, going to the top of a new column after reaching the final |
| 51 |
row. |
| 52 |
- [`width`][width]: the width to fill the grid into. Usually, this should be the |
| 53 |
width of the terminal. |
| 54 |
|
| 55 |
In practice, creating a grid can be done as follows: |
| 56 |
|
| 57 |
```rust |
| 58 |
use term_grid::{Grid, GridOptions, Direction, Filling}; |
| 59 |
|
| 60 |
// Create a `Vec` of text to put in the grid |
| 61 |
let cells = vec![ |
| 62 |
"one", "two", "three", "four", "five", "six", |
| 63 |
"seven", "eight", "nine", "ten", "eleven", "twelve" |
| 64 |
]; |
| 65 |
|
| 66 |
// Then create a `Grid` with those cells. |
| 67 |
// The grid requires several options: |
| 68 |
// - The filling determines the string used as separator |
| 69 |
// between the columns. |
| 70 |
// - The direction specifies whether the layout should |
| 71 |
// be done row-wise or column-wise. |
| 72 |
// - The width is the maximum width that the grid might |
| 73 |
// have. |
| 74 |
let grid = Grid::new( |
| 75 |
cells, |
| 76 |
GridOptions { |
| 77 |
filling: Filling::Spaces(1), |
| 78 |
direction: Direction::LeftToRight, |
| 79 |
width: 24, |
| 80 |
} |
| 81 |
); |
| 82 |
|
| 83 |
// A `Grid` implements `Display` and can be printed directly. |
| 84 |
println!("{grid}"); |
| 85 |
``` |
| 86 |
|
| 87 |
Produces the following tabular result: |
| 88 |
|
| 89 |
```text |
| 90 |
one two three four |
| 91 |
five six seven eight |
| 92 |
nine ten eleven twelve |
| 93 |
``` |
| 94 |
|
| 95 |
[filling]: https://docs.rs/uutils_term_grid/latest/term_grid/struct.GridOptions.html#structfield.filling |
| 96 |
[direction]: https://docs.rs/uutils_term_grid/latest/term_grid/struct.GridOptions.html#structfield.direction |
| 97 |
[width]: https://docs.rs/uutils_term_grid/latest/term_grid/struct.GridOptions.html#structfield.width |
| 98 |
[LeftToRight]: https://docs.rs/uutils_term_grid/latest/term_grid/enum.Direction.html#variant.LeftToRight |
| 99 |
[TopToBottom]: https://docs.rs/uutils_term_grid/latest/term_grid/enum.Direction.html#variant.TopToBottom |
| 100 |
[Spaces]: https://docs.rs/uutils_term_grid/latest/term_grid/enum.Filling.html#variant.Spaces |
| 101 |
[Text]: https://docs.rs/uutils_term_grid/latest/term_grid/enum.Filling.html#variant.Text |
| 102 |
[Tabs]:https://docs.rs/uutils_term_grid/latest/term_grid/enum.Filling.html#variant.Tabs |
| 103 |
|
| 104 |
## Width of grid cells |
| 105 |
|
| 106 |
This library calculates the width of strings as displayed in the terminal using |
| 107 |
the [`ansi-width`][ansi-width] crate. This takes into account the width of |
| 108 |
characters and ignores ANSI codes. |
| 109 |
|
| 110 |
The width calculation is currently not configurable. If you have a use-case for |
| 111 |
which this calculation is wrong, please open an issue. |
| 112 |
|
| 113 |
[ansi-width]: https://docs.rs/ansi-width |