textwrap

package module
v0.0.0-...-22edad1 Latest Latest
Warning

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

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

README

Text Wrap

This is a port of Python's "textwrap" module for Go. Well, sort of...

Limitations

This modules (at least for now) is not wrapping on whitespaces and right after hyphens in compound words, as it is customary in English. That said, break_on_hyphens and break_long_words are not yet supported.

Also fix_sentence_endings is not supported as well for now, which doesn't work reliably in Python anyways (since it requires two spaces and other conditions nobody cares of).

The implementation for hyphens support is planned however, while fix_sentence_endings is not (but! your PRs are welcome and free to implement it).

Usage

The usage is quite similar as in Python:

import (
	"fmt"
	"github.com/isbm/textwrap"
)

...

text := "Your very long text here"
wrapper := textwrap.NewTextWrap() // Defaults to 70
fmt.Println(wrapper.Fill(text))      // Returns string

// Get each line
for idx, line := range wrapper.Wrap(text) {
	fmt.Println(idx, line)
}

De-dent is also implemented and works exactly the same as in Python:

multilineText := `
    There is some multiline text
  with different identation
      everywhere. So it will be
    aligned to the minimal.
`

// This will remove two leading spaces from each line
fmt.Println(wrapper.Dedent(multilineText))

Configuration

You can setup wrapper object constructor the following way (given values are its defaults, so you can change it to whatever you want):

wrapper := textwrap.NewTextWrap().
	SetNewLine("\n").
	SetWidth(70),
	SetTabSpacesWidth(4).
	SetDropWhitespace(true).
	SetInitialIndent("").
	SetReplaceWhitespace(true)

Have fun.

Bonus Functions

While it is possible to do it differently, this module also gives you string whitespace trimming for only leading whitespace (TrimLeft) or only trailing (TrimRight), as contrary to strings.TrimSpace that trims everything.

The whitespace is the same as defined in Python's strings.whitespace.

Documentation

Overview

Package textwrap is a semi-port of Python's equivalent module "textwrap". Most of the functionality is similar (or in a progress to get there). The textwrap module provides two convenience methods: Wrap() and Fill(). All of them are methods of textWrap class that does all the work. Package also provides an utility function Dedent() as well as TrimLeft() and TrimRight() to strip a whitespace respectively.

Index

Constants

View Source
const (
	// WHITESPACE is a string of symbols that are subject to be stripped
	// in the incoming data
	WHITESPACE = " \t\n\r\x0b\x0c"
)

Variables

This section is empty.

Functions

func NewTextWrap

func NewTextWrap() *textWrap

NewTextWrap function returns text wrapper instance object. Constructor does not accept any params, however can be configured in chain methods setting configuration settings.

Types

This section is empty.

Jump to

Keyboard shortcuts

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