All Projects β†’ mgeisler β†’ Textwrap

mgeisler / Textwrap

Licence: mit
An efficient and powerful Rust library for word wrapping text.

Programming Languages

rust
11053 projects

Projects that are alternatives of or similar to Textwrap

Text
An efficient packed, immutable Unicode text type for Haskell, with a powerful loop fusion optimization framework.
Stars: ✭ 248 (+51.22%)
Mutual labels:  text, unicode
Prestyler
Elegant text formatting tool in Swift πŸ”₯
Stars: ✭ 36 (-78.05%)
Mutual labels:  text, formatting
Stringi
THE String Processing Package for R (with ICU)
Stars: ✭ 204 (+24.39%)
Mutual labels:  text, unicode
Lingo
Text encoding for modern C++
Stars: ✭ 28 (-82.93%)
Mutual labels:  unicode, text
humanize
A collection of utility functions, with built-in localization, for humanizing various types of data input
Stars: ✭ 73 (-55.49%)
Mutual labels:  text, formatting
Decoro
Android library designed for automatic formatting of text input by custom rules
Stars: ✭ 325 (+98.17%)
Mutual labels:  text, formatting
unicode-formatter
Convert portions of text to fancy text using unicode fonts for use on Twitter and other sites that don't support rich text
Stars: ✭ 31 (-81.1%)
Mutual labels:  unicode, formatting
AutoFormatInputWatcher
This repository contains input watcher for auto formatting digits in edit text
Stars: ✭ 15 (-90.85%)
Mutual labels:  text, formatting
stringx
Drop-in replacements for base R string functions powered by stringi
Stars: ✭ 14 (-91.46%)
Mutual labels:  unicode, text
Csconsoleformat
.NET C# library for advanced formatting of console output [Apache]
Stars: ✭ 296 (+80.49%)
Mutual labels:  text, formatting
Js Codepage
πŸ’± Codepages for JS
Stars: ✭ 119 (-27.44%)
Mutual labels:  text, unicode
East icpr
Forked from argman/EAST for the ICPR MTWI 2018 CHALLENGE
Stars: ✭ 154 (-6.1%)
Mutual labels:  text
Baffle
A tiny javascript library for obfuscating and revealing text in DOM elements. 😲
Stars: ✭ 1,721 (+949.39%)
Mutual labels:  text
Peep
The CLI text viewer tool that works like less command on small pane within the terminal window.
Stars: ✭ 139 (-15.24%)
Mutual labels:  text
Idna
Internationalized Domain Names for Python (IDNA 2008 and UTS #46)
Stars: ✭ 138 (-15.85%)
Mutual labels:  unicode
Wgpu glyph
A fast text renderer for wgpu (https://github.com/gfx-rs/wgpu)
Stars: ✭ 159 (-3.05%)
Mutual labels:  text
Aeneas
aeneas is a Python/C library and a set of tools to automagically synchronize audio and text (aka forced alignment)
Stars: ✭ 1,942 (+1084.15%)
Mutual labels:  text
Guide To Swift Strings Sample Code
Xcode Playground Sample Code for the Flight School Guide to Swift Strings
Stars: ✭ 136 (-17.07%)
Mutual labels:  unicode
Notepanda
πŸ“ƒ A simple cross-platform notepad. Based on Qt and C++.
Stars: ✭ 134 (-18.29%)
Mutual labels:  text
Typesettable
πŸ“ A typesetting library for SVG and Canvas
Stars: ✭ 134 (-18.29%)
Mutual labels:  text

Textwrap

Textwrap is a library for wrapping and indenting text. It is most often used by command-line programs to format dynamic output nicely so it looks good in a terminal. However, you can use the library to wrap arbitrary things by implementing the Fragment trait β€” an example would be wrapping text for PDF files.

Usage

To use the textwrap crate, add this to your Cargo.toml file:

[dependencies]
textwrap = "0.13"

By default, this enables word wrapping with support for Unicode strings. Extra features can be enabled with Cargo features β€” and the Unicode support can be disabled if needed. This allows you slim down the library and so you will only pay for the features you actually use. Please see the Cargo Features in the crate documentation for a full list of the available features.

Documentation

API documentation

Getting Started

Word wrapping is easy using the fill function:

fn main() {
    let text = "textwrap: an efficient and powerful library for wrapping text.";
    println!("{}", textwrap::fill(text, 28));
}

The output is wrapped within 28 columns:

textwrap: an efficient
and powerful library for
wrapping text.

Sharp-eyed readers will notice that the first line is 22 columns wide. So why is the word β€œand” put in the second line when there is space for it in the first line?

The explanation is that textwrap does not just wrap text one line at a time. Instead, it uses an optimal-fit algorithm which looks ahead and chooses line breaks which minimize the gaps left at ends of lines.

Without look-ahead, the first line would be longer and the text would look like this:

textwrap: an efficient and
powerful library for
wrapping text.

The second line is now shorter and the text is more ragged. The kind of wrapping can be configured via Option::wrap_algorithm.

If you enable the hyphenation Cargo feature, you get support for automatic hyphenation for about 70 languages via high-quality TeX hyphenation patterns.

Your program must load the hyphenation pattern and configure Options::splitter to use it:

use hyphenation::{Language, Load, Standard};
use textwrap::Options;

fn main() {
    let hyphenator = Standard::from_embedded(Language::EnglishUS).unwrap();
    let options = Options::new(28).splitter(hyphenator);
    let text = "textwrap: an efficient and powerful library for wrapping text.";
    println!("{}", fill(text, &options);
}

The output now looks like this:

textwrap: an efficient and
powerful library for wrap-
ping text.

The US-English hyphenation patterns are embedded when you enable the hyphenation feature. They are licensed under a permissive license and take up about 88 KB in your binary. If you need hyphenation for other languages, you need to download a precompiled .bincode file and load it yourself. Please see the hyphenation documentation for details.

Wrapping Strings at Compile Time

If your strings are known at compile time, please take a look at the procedural macros from the textwrap-macros crate.

Examples

The library comes with a collection of small example programs that shows various features. You’re invited to clone the repository and try them out for yourself!

Of special note is the interactive example. This is a demo program which demonstrates most of the available features: you can enter text and adjust the width at which it is wrapped interactively. You can also adjust the Options used to see the effect of different WordSplitters and wrap algorithms.

Run the demo with

$ cargo run --example interactive

The demo needs a Linux terminal to function.

Release History

Please see the CHANGELOG file for details on the changes made in each release.

License

Textwrap can be distributed according to the MIT license. Contributions will be accepted under the same license.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].