textwrap

package module
v0.0.0-...-b0a704f Latest Latest
Warning

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

Go to latest
Published: Oct 29, 2019 License: MIT Imports: 2 Imported by: 2

README

Go Text Wrapping

It is a lightweight and simple port of the textwrap Python module.

Soli Deo gloria

Features:

  • Lightweight. Uses only the Go built-in library (no external deps).
  • Simple. Easy to understand code.
  • Counts number of words and characters.
  • Splits (or groups) text based on character limit (width) per group (each wrap).
  • 100% test coverage.
  • GoDoc documentation
Note

It was only designed to work with English text; it separates words by whitespace.

Example

import (
    "fmt"
    "textwrap"
)

origText := "Jesus is God. He Saves by grace through faith alone."
result, err := textwrap.WordWrap(origText, 10, -1)
if err != nil {
    // handle error
}

// check if max word count exceeded
if result.IsValid() {
    // print text groups
    for idx, text := range result.TextGroups {
        fmt.Println("[", idx+1, "]", text)
    }
}

License

MIT

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type WordWrapResult

type WordWrapResult struct {
	// TextGroups is the full text splits.
	TextGroups []string
	// WordCount is the total number of words in the text groups.
	WordCount int
	// CharCount is the total character count across all text groups.
	CharCount int
}

WordWrapResult represents the wrap operation result.

func WordWrap

func WordWrap(fullText string, width, maxWordCount int) (WordWrapResult, error)

WordWrap wraps the specified text to the given width (num of chars), returning an array element for each wrapped text group. If the maxWordCount is one or more, then wrapping will exit, if the word count exceeds the maxWordCount value.

func (WordWrapResult) IsValid

func (w WordWrapResult) IsValid() bool

IsValid returns false, if a word count was stored, but no text groups processed, which means the max word count was exceeded.

Jump to

Keyboard shortcuts

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