Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Rust library for putting things in a grid

License

NotificationsYou must be signed in to change notification settings

ogham/rust-term-grid

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

53 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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

Installation

This crate works withCargo. Add the following to yourCargo.toml dependencies section:

[dependencies]term_grid ="0.2"

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

Usage

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};letmut grid =Grid::new(GridOptions{filling:Filling::Spaces(1),direction:Direction::LeftToRight,});for sin&["one","two","three","four","five","six","seven","eight","nine","ten","eleven","twelve"]{    grid.add(Cell::from(*s));}println!("{}", grid.fit_into_width(24).unwrap());

Produces the following tabular result:

one  two three  fourfive six seven  eightnine ten eleven twelve

Creating a grid

To add data to a grid, first create a newGrid value, and then add cells to them with theadd method.

There are two options that must be specified in theGridOptions 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 movesrightwards, going to the start of a new row after reaching the final column;
    • Direction::TopToBottom starts them in the top left and movesdownwards, 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 everyn cells - is achieved with thefit_into_columns method on aGrid 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 thefit_into_width method.It takes the maximum allowed width, including separators, as its argument.However, it returns anoptionalDisplay 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 takeStrings or&strs - they takeCells.

ACell 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 theUnicode width of the string to be used for this, so you can turn aString into aCell 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 theCell values are public, meaning you can construct your own instances as necessary.

About

Rust library for putting things in a grid

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Contributors3

  •  
  •  
  •  

Languages


[8]ページ先頭

©2009-2025 Movatter.jp