#grid #term


Library for formatting strings into a grid layout

9 releases

Uses old Rust 2015

0.2.0 Apr 26, 2020
0.1.7 Mar 10, 2018
0.1.6 Aug 12, 2017
0.1.5 Oct 5, 2016
0.1.1 Jun 28, 2015

#586 in Rust patterns

Download history 5769/week @ 2023-08-17 6856/week @ 2023-08-24 6161/week @ 2023-08-31 8513/week @ 2023-09-07 7673/week @ 2023-09-14 6517/week @ 2023-09-21 6070/week @ 2023-09-28 6095/week @ 2023-10-05 2951/week @ 2023-10-12 1915/week @ 2023-10-19 2158/week @ 2023-10-26 2206/week @ 2023-11-02 2092/week @ 2023-11-09 2133/week @ 2023-11-16 1939/week @ 2023-11-23 1472/week @ 2023-11-30

7,974 downloads per month
Used in 16 crates (14 directly)

MIT license

443 lines

rust-term-grid term-grid on crates.io Minimum Rust Version 1.31.0 Build status

This library arranges textual data in a grid format suitable for fixed-width fonts, using an algorithm to minimise the amount of space needed.

View the Rustdoc


This crate works with Cargo. Add the following to your Cargo.toml dependencies section:

term_grid = "0.2"

The earliest version of Rust that this crate is tested against is Rust v1.31.0.


This library arranges textual data in a grid format suitable for fixed-width fonts, using an algorithm to minimise the amount of space needed. For example:

use term_grid::{Grid, GridOptions, Direction, Filling, Cell};

let mut grid = Grid::new(GridOptions {
    filling:     Filling::Spaces(1),
    direction:   Direction::LeftToRight,

for s in &["one", "two", "three", "four", "five", "six", "seven",
           "eight", "nine", "ten", "eleven", "twelve"]

println!("{}", grid.fit_into_width(24).unwrap());

Produces the following tabular result:

one  two three  four
five six seven  eight
nine ten eleven twelve

Creating a grid

To add data to a grid, first create a new Grid value, and then add cells to them with the add method.

There are two options that must be specified in the GridOptions value that dictate how the grid is formatted:

  • filling: what to put in between two columns - either a number of spaces, or a text string;
  • direction, which specifies whether the cells should go along rows, or columns:
    • Direction::LeftToRight starts them in the top left and moves rightwards, going to the start of a new row after reaching the final column;
    • Direction::TopToBottom starts them in the top left and moves downwards, going to the top of a new column after reaching the final row.

Displaying a grid

When display a grid, you can either specify the number of columns in advance, or try to find the maximum number of columns that can fit in an area of a given width.

Splitting a series of cells into columns - or, in other words, starting a new row every n cells - is achieved with the fit_into_columns method on a Grid value. It takes as its argument the number of columns.

Trying to fit as much data onto one screen as possible is the main use case for specifying a maximum width instead. This is achieved with the fit_into_width method. It takes the maximum allowed width, including separators, as its argument. However, it returns an optional Display value, depending on whether any of the cells actually had a width greater than the maximum width! If this is the case, your best bet is to just output the cells with one per line.

Cells and data

Grids do not take Strings or &strs - they take Cells.

A Cell is a struct containing an individual cell’s contents, as a string, and its pre-computed length, which gets used when calculating a grid’s final dimensions. Usually, you want the Unicode width of the string to be used for this, so you can turn a String into a Cell with the .into() method.

However, you may also want to supply your own width: when you already know the width in advance, or when you want to change the measurement, such as skipping over terminal control characters. For cases like these, the fields on the Cell values are public, meaning you can construct your own instances as necessary.