{"id":13438673,"url":"https://github.com/ogham/rust-term-grid","last_synced_at":"2025-04-07T07:12:35.137Z","repository":{"id":1013054,"uuid":"37865925","full_name":"ogham/rust-term-grid","owner":"ogham","description":"Rust library for putting things in a grid","archived":false,"fork":false,"pushed_at":"2023-05-08T00:11:24.000Z","size":41,"stargazers_count":67,"open_issues_count":7,"forks_count":15,"subscribers_count":5,"default_branch":"master","last_synced_at":"2025-03-31T06:02:07.535Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"Rust","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/ogham.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null}},"created_at":"2015-06-22T16:13:06.000Z","updated_at":"2025-01-06T20:34:14.000Z","dependencies_parsed_at":"2023-07-11T17:47:04.331Z","dependency_job_id":null,"html_url":"https://github.com/ogham/rust-term-grid","commit_stats":{"total_commits":50,"total_committers":5,"mean_commits":10.0,"dds":0.12,"last_synced_commit":"1cbc10f4fd641e49c20e72afbad3f850af51ab61"},"previous_names":[],"tags_count":7,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ogham%2Frust-term-grid","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ogham%2Frust-term-grid/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ogham%2Frust-term-grid/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ogham%2Frust-term-grid/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/ogham","download_url":"https://codeload.github.com/ogham/rust-term-grid/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247608153,"owners_count":20965952,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-07-31T03:01:07.433Z","updated_at":"2025-04-07T07:12:35.103Z","avatar_url":"https://github.com/ogham.png","language":"Rust","readme":"# rust-term-grid [![term-grid on crates.io][crates-badge]][crates-url] [![Minimum Rust Version 1.31.0][rustc-badge]][rustc-url] [![Build status][travis-badge]][travis-url]\n\n[crates-badge]: https://meritbadge.herokuapp.com/term-grid\n[crates-url]: https://crates.io/crates/term-grid\n[travis-badge]: https://travis-ci.org/ogham/rust-term-grid.svg?branch=master\n[travis-url]: https://travis-ci.org/github/ogham/rust-term-grid\n[rustc-badge]: https://img.shields.io/badge/rustc-1.31+-lightgray.svg\n[rustc-url]: https://blog.rust-lang.org/2018/12/06/Rust-1.31-and-rust-2018.html\n\nThis library arranges textual data in a grid format suitable for fixed-width fonts, using an algorithm to minimise the amount of space needed.\n\n### [View the Rustdoc](https://docs.rs/term_grid)\n\n\n# Installation\n\nThis crate works with [Cargo](https://crates.io). Add the following to your `Cargo.toml` dependencies section:\n\n```toml\n[dependencies]\nterm_grid = \"0.2\"\n```\n\nThe earliest version of Rust that this crate is tested against is [Rust v1.31.0][rustc-url].\n\n\n## Usage\n\nThis library arranges textual data in a grid format suitable for fixed-width fonts, using an algorithm to minimise the amount of space needed.\nFor example:\n\n```rust\nuse term_grid::{Grid, GridOptions, Direction, Filling, Cell};\n\nlet mut grid = Grid::new(GridOptions {\n    filling:     Filling::Spaces(1),\n    direction:   Direction::LeftToRight,\n});\n\nfor s in \u0026[\"one\", \"two\", \"three\", \"four\", \"five\", \"six\", \"seven\",\n           \"eight\", \"nine\", \"ten\", \"eleven\", \"twelve\"]\n{\n    grid.add(Cell::from(*s));\n}\n\nprintln!(\"{}\", grid.fit_into_width(24).unwrap());\n```\n\nProduces the following tabular result:\n\n```\none  two three  four\nfive six seven  eight\nnine ten eleven twelve\n```\n\n\n## Creating a grid\n\nTo add data to a grid, first create a new `Grid` value, and then add cells to them with the `add` method.\n\nThere are two options that must be specified in the `GridOptions` value that dictate how the grid is formatted:\n\n- `filling`: what to put in between two columns - either a number of spaces, or a text string;\n- `direction`, which specifies whether the cells should go along rows, or columns:\n    - `Direction::LeftToRight` starts them in the top left and moves *rightwards*, going to the start of a new row after reaching the final column;\n    - `Direction::TopToBottom` starts them in the top left and moves *downwards*, going to the top of a new column after reaching the final row.\n\n\n## Displaying a grid\n\nWhen 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.\n\nSplitting 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.\nIt takes as its argument the number of columns.\n\nTrying to fit as much data onto one screen as possible is the main use case for specifying a maximum width instead.\nThis is achieved with the `fit_into_width` method.\nIt takes the maximum allowed width, including separators, as its argument.\nHowever, it returns an *optional* `Display` value, depending on whether any of the cells actually had a width greater than the maximum width!\nIf this is the case, your best bet is to just output the cells with one per line.\n\n\n## Cells and data\n\nGrids do not take `String`s or `\u0026str`s - they take `Cells`.\n\nA **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.\nUsually, 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.\n\nHowever, 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.\nFor cases like these, the fields on the `Cell` values are public, meaning you can construct your own instances as necessary.\n","funding_links":[],"categories":["Libraries","库 Libraries","库"],"sub_categories":["Command-line","命令行 Command-line","命令行"],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fogham%2Frust-term-grid","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fogham%2Frust-term-grid","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fogham%2Frust-term-grid/lists"}