{"id":13440088,"url":"https://github.com/mgeisler/textwrap","last_synced_at":"2025-05-13T22:08:57.461Z","repository":{"id":37271612,"uuid":"76690728","full_name":"mgeisler/textwrap","owner":"mgeisler","description":"An efficient and powerful Rust library for word wrapping text.","archived":false,"fork":false,"pushed_at":"2025-05-01T21:10:33.000Z","size":3621,"stargazers_count":491,"open_issues_count":29,"forks_count":47,"subscribers_count":7,"default_branch":"master","last_synced_at":"2025-05-08T00:08:53.085Z","etag":null,"topics":["formatting","hyphenation","rust","text","textwrap","unicode","wrapping"],"latest_commit_sha":null,"homepage":"","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/mgeisler.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2016-12-16T22:47:20.000Z","updated_at":"2025-05-06T10:13:39.000Z","dependencies_parsed_at":"2023-02-12T04:03:14.803Z","dependency_job_id":"dd4af9e9-878c-48df-a0a4-77dc4cd5ae5a","html_url":"https://github.com/mgeisler/textwrap","commit_stats":{"total_commits":700,"total_committers":30,"mean_commits":"23.333333333333332","dds":0.1171428571428571,"last_synced_commit":"0e581b8e7794043eed5aa07511f2b8ce6516ede7"},"previous_names":[],"tags_count":27,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mgeisler%2Ftextwrap","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mgeisler%2Ftextwrap/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mgeisler%2Ftextwrap/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mgeisler%2Ftextwrap/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/mgeisler","download_url":"https://codeload.github.com/mgeisler/textwrap/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":253650947,"owners_count":21942232,"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":["formatting","hyphenation","rust","text","textwrap","unicode","wrapping"],"created_at":"2024-07-31T03:01:19.687Z","updated_at":"2025-05-13T22:08:57.416Z","avatar_url":"https://github.com/mgeisler.png","language":"Rust","readme":"# Textwrap\n\n[![](https://github.com/mgeisler/textwrap/workflows/build/badge.svg)][build-status]\n[![](https://codecov.io/gh/mgeisler/textwrap/branch/master/graph/badge.svg)][codecov]\n[![](https://img.shields.io/crates/v/textwrap.svg)][crates-io]\n[![](https://docs.rs/textwrap/badge.svg)][api-docs]\n\nTextwrap is a library for wrapping and indenting text. It is most often used by\ncommand-line programs to format dynamic output nicely so it looks good in a\nterminal. You can also use Textwrap to wrap text set in a proportional font—such\nas text used to generate PDF files, or drawn on a\n[HTML5 canvas using WebAssembly][wasm-demo].\n\n## Usage\n\nTo use the textwrap crate, add this to your `Cargo.toml` file:\n\n```toml\n[dependencies]\ntextwrap = \"0.16\"\n```\n\nBy default, this enables word wrapping with support for Unicode strings. Extra\nfeatures can be enabled with Cargo features—and the Unicode support can be\ndisabled if needed. This allows you slim down the library and so you will only\npay for the features you actually use.\n\nPlease see the\n[_Cargo Features_ in the crate\ndocumentation](https://docs.rs/textwrap/#cargo-features) for a full list of the\navailable features as well as their impact on the size of your binary.\n\n## Documentation\n\n**[API documentation][api-docs]**\n\n## Getting Started\n\nWord wrapping is easy using the `wrap` and `fill` functions:\n\n```rust\n#[cfg(feature = \"smawk\")] {\nlet text = \"textwrap: an efficient and powerful library for wrapping text.\";\nassert_eq!(\n    textwrap::wrap(text, 28),\n    vec![\n        \"textwrap: an efficient\",\n        \"and powerful library for\",\n        \"wrapping text.\",\n    ]\n);\n}\n```\n\nSharp-eyed readers will notice that the first line is 22 columns wide. So why is\nthe word “and” put in the second line when there is space for it in the first\nline?\n\nThe explanation is that textwrap does not just wrap text one line at a time.\nInstead, it uses an optimal-fit algorithm which looks ahead and chooses line\nbreaks which minimize the gaps left at ends of lines. This is controlled with\nthe `smawk` Cargo feature, which is why the example is wrapped in the\n`cfg`-block.\n\nWithout look-ahead, the first line would be longer and the text would look like\nthis:\n\n```rust\n#[cfg(not(feature = \"smawk\"))] {\nlet text = \"textwrap: an efficient and powerful library for wrapping text.\";\nassert_eq!(\n    textwrap::wrap(text, 28),\n    vec![\n        \"textwrap: an efficient and\",\n        \"powerful library for\",\n        \"wrapping text.\",\n    ]\n);\n}\n```\n\nThe second line is now shorter and the text is more ragged. The kind of wrapping\ncan be configured via `Options::wrap_algorithm`.\n\nIf you enable the `hyphenation` Cargo feature, you get support for automatic\nhyphenation for [about 70 languages][patterns] via high-quality TeX hyphenation\npatterns.\n\nYour program must load the hyphenation pattern and configure\n`Options::word_splitter` to use it:\n\n```rust\n#[cfg(feature = \"hyphenation\")] {\nuse hyphenation::{Language, Load, Standard};\nuse textwrap::{fill, Options, WordSplitter};\n\nlet dictionary = Standard::from_embedded(Language::EnglishUS).unwrap();\nlet options = textwrap::Options::new(28).word_splitter(WordSplitter::Hyphenation(dictionary));\nlet text = \"textwrap: an efficient and powerful library for wrapping text.\";\n\nassert_eq!(\n    textwrap::wrap(text, \u0026options),\n    vec![\n        \"textwrap: an efficient and\",\n        \"powerful library for wrap-\",\n        \"ping text.\"\n    ]\n);\n}\n```\n\nThe US-English hyphenation patterns are embedded when you enable the\n`hyphenation` feature. They are licensed under a\n[permissive license][en-us license] and take up about 88 KB in your binary. If\nyou need hyphenation for other languages, you need to download a\n[precompiled `.bincode` file][bincode] and load it yourself. Please see the\n[`hyphenation` documentation] for details.\n\n## Wrapping Strings at Compile Time\n\nIf your strings are known at compile time, please take a look at the procedural\nmacros from the [`textwrap-macros` crate].\n\n## Examples\n\nThe library comes with\n[a collection](https://github.com/mgeisler/textwrap/tree/master/examples) of\nsmall example programs that shows various features.\n\nIf you want to see Textwrap in action right away, then take a look at\n[`examples/wasm/`], which shows how to wrap sans-serif, serif, and monospace\ntext. It uses WebAssembly and is automatically deployed to\nhttps://mgeisler.github.io/textwrap/.\n\nFor the command-line examples, you’re invited to clone the repository and try\nthem out for yourself! Of special note is [`examples/interactive.rs`]. This is a\ndemo program which demonstrates most of the available features: you can enter\ntext and adjust the width at which it is wrapped interactively. You can also\nadjust the `Options` used to see the effect of different `WordSplitter`s and\nwrap algorithms.\n\nRun the demo with\n\n```sh\n$ cargo run --example interactive\n```\n\nThe demo needs a Linux terminal to function.\n\n## Release History\n\nPlease see the [CHANGELOG file] for details on the changes made in each release.\n\n## License\n\nTextwrap can be distributed according to the [MIT license][mit]. Contributions\nwill be accepted under the same license.\n\n[crates-io]: https://crates.io/crates/textwrap\n[build-status]: https://github.com/mgeisler/textwrap/actions?query=workflow%3Abuild+branch%3Amaster\n[codecov]: https://codecov.io/gh/mgeisler/textwrap\n[wasm-demo]: https://mgeisler.github.io/textwrap/\n[`textwrap-macros` crate]: https://crates.io/crates/textwrap-macros\n[`hyphenation` example]: https://github.com/mgeisler/textwrap/blob/master/examples/hyphenation.rs\n[`termwidth` example]: https://github.com/mgeisler/textwrap/blob/master/examples/termwidth.rs\n[patterns]: https://github.com/tapeinosyne/hyphenation/tree/master/patterns\n[en-us license]: https://github.com/hyphenation/tex-hyphen/blob/master/hyph-utf8/tex/generic/hyph-utf8/patterns/tex/hyph-en-us.tex\n[bincode]: https://github.com/tapeinosyne/hyphenation/tree/master/dictionaries\n[`hyphenation` documentation]: http://docs.rs/hyphenation\n[`examples/wasm/`]: https://github.com/mgeisler/textwrap/tree/master/examples/wasm\n[`examples/interactive.rs`]: https://github.com/mgeisler/textwrap/tree/master/examples/interactive.rs\n[api-docs]: https://docs.rs/textwrap/\n[CHANGELOG file]: https://github.com/mgeisler/textwrap/blob/master/CHANGELOG.md\n[mit]: LICENSE\n","funding_links":[],"categories":["Libraries","Rust","库 Libraries","库"],"sub_categories":["Text processing","文本处理 Text processing","文本处理"],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmgeisler%2Ftextwrap","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmgeisler%2Ftextwrap","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmgeisler%2Ftextwrap/lists"}