GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — GitHub - uutils/uutils-term-grid

BranchesTags
Open more actions menu

Latest commit

 

History

193 Commits
193 Commits

Folders and files

NameName
Last commit message
Last commit date
.github/workflows
.github/workflows
 
 
benches
benches
 
 
examples
examples
 
 
src
src
 
 
tests
tests
 
 
.gitignore
.gitignore
 
 
Cargo.lock
Cargo.lock
 
 
Cargo.toml
Cargo.toml
 
 
Justfile
Justfile
 
 
LICENSE
LICENSE
 
 
README.md
README.md
 
 
renovate.json
renovate.json
 
 

Repository files navigation

Crates.io dependency status CodeCov CodSpeed Badge

uutils-term-grid

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


This library is forked from the unmaintained rust-term-grid library. The core functionality has remained the same, with some additional bugfixes, performance improvements and a new API.


Installation

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

[dependencies]
uutils_term_grid = "0.7"

The Minimum Supported Rust Version is 1.70.

Creating a grid

To add data to a grid, first create a new [Grid] value with a list of strings and a set of options.

There are four options that can be configured in [GridOptions]. GridOptions implements Default, providing sensible defaults for all fields:

  • filling: how to fill empty space between columns (defaults to Filling::Spaces(2)):
    • Filling::Spaces number of spaces between columns;
    • Filling::Text text string separator between columns;
    • Filling::Tabs special option which allows to set number of spaces between columns and set the size of \t character.
  • direction: specifies whether the cells should go along rows, or columns (defaults to Direction::LeftToRight):
    • 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.
  • width: the width to fill the grid into (defaults to 80). Usually, this should be the actual width of the terminal.
  • line_separator: the string to put at the end of each line/row in the grid (defaults to "\n").

In practice, creating a grid can be done as follows:

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

// Create a `Vec` of text to put in the grid
let cells = vec![
    "one", "two", "three", "four", "five", "six",
    "seven", "eight", "nine", "ten", "eleven", "twelve"
];

// Then create a `Grid` with those cells.
// The grid requires several options:
//  - The filling determines the string used as separator
//    between the columns.
//  - The direction specifies whether the layout should
//    be done row-wise or column-wise.
//  - The width is the maximum width that the grid might
//    have.
let grid = Grid::new(
    cells,
    GridOptions {
        filling: Filling::Spaces(1),
        direction: Direction::LeftToRight,
        width: 24,
        ..Default::default()
    }
);

// A `Grid` implements `Display` and can be printed directly.
println!("{grid}");

Produces the following tabular result:

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

By default, each row ends with a newline ("\n"). A custom line separator (such as "\0" or "\r\n") can be configured using line_separator:

use term_grid::{Grid, GridOptions, Direction, Filling};
let cells = vec!["one", "two", "three", "four"];
let options = GridOptions {
    filling: Filling::Spaces(1),
    direction: Direction::LeftToRight,
    width: 24,
    line_separator: "\0".into(),
    ..Default::default()
};
let grid = Grid::new(cells, options);

Width of grid cells

This library calculates the width of strings as displayed in the terminal using the ansi-width crate. This takes into account the width of characters and ignores ANSI codes.

The width calculation is currently not configurable. If you have a use-case for which this calculation is wrong, please open an issue.

About

No description, website, or topics provided.

Resources

Readme
MIT license
Activity
Custom properties

Stars

13 stars

Watchers

2 watching

Forks

13 forks
Report repository

Releases

Packages

Used by

Contributors

Languages

You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.