textwrap

package
v0.9.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 10, 2026 License: BSD-2-Clause Imports: 3 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package textwrap provides dependency-free helpers for reflowing plain text to a fixed column width.

Index

Constants

View Source
const Ellipsis = "…"

Ellipsis is appended to text that Truncate shortens.

Variables

This section is empty.

Functions

func Dedent

func Dedent(text string) string

Dedent removes the longest common leading whitespace shared by all non-blank lines of text. Blank lines never constrain the calculation, and lines without the common prefix are returned unchanged.

func Indent

func Indent(text, prefix string) string

Indent prefixes every non-empty line of text with prefix. Blank lines are left untouched so paragraph structure stays visible.

func IndentLines

func IndentLines(lines []string, prefix string) []string

IndentLines returns a copy of lines with prefix prepended to every non-empty entry. The input slice is never modified.

func Truncate

func Truncate(ctx context.Context, text string, width int) (string, error)

Truncate shortens text to at most width runes. Text that already fits is returned unchanged. Shortened text ends with Ellipsis, which counts toward width; when width is not larger than the ellipsis itself the ellipsis is cut down to width.

Truncate returns an error when width is smaller than one.

func TruncateLines

func TruncateLines(ctx context.Context, lines []string, width int) ([]string, error)

TruncateLines returns a copy of lines with every entry truncated to at most width runes. The input slice is never modified.

func Wrap

func Wrap(ctx context.Context, text string, width int) ([]string, error)

Wrap reflows text so that every returned line is at most width runes long. Every line break in text starts a new output line and blank input lines stay blank. Runs of spaces, tabs, and other whitespace collapse to a single space. Words longer than width are hard-split so the width guarantee always holds.

Wrap returns an error when width is smaller than one.

func WrapString

func WrapString(ctx context.Context, text string, width int) (string, error)

WrapString reflows text exactly like Wrap and joins the resulting lines with newline characters.

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL