Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/isbm/textwrap
Port of Python's "textwrap" module to Go
https://github.com/isbm/textwrap
Last synced: about 2 months ago
JSON representation
Port of Python's "textwrap" module to Go
- Host: GitHub
- URL: https://github.com/isbm/textwrap
- Owner: isbm
- License: mit
- Created: 2019-07-26T17:57:55.000Z (over 5 years ago)
- Default Branch: master
- Last Pushed: 2019-08-03T19:01:29.000Z (over 5 years ago)
- Last Synced: 2024-07-31T20:53:11.317Z (5 months ago)
- Language: Go
- Size: 25.4 KB
- Stars: 5
- Watchers: 3
- Forks: 4
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
- awesome-go - textwrap - Wraps text at end of lines. Implementation of `textwrap` module from Python. (Text Processing / Formatters)
- awesome-go-extra - textwrap - 07-26T17:57:55Z|2019-08-03T19:01:29Z| (Bot Building / Formatters)
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:
```go
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:
```go
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):```go
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`.