Port of Python's "textwrap" module to Go

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.

Owner
Bo Maryniuk
What's your username?
Bo Maryniuk
Similar Resources

Port of LZ4 lossless compression algorithm to Go

go-lz4 go-lz4 is port of LZ4 lossless compression algorithm to Go. The original C code is located at: https://github.com/Cyan4973/lz4 Status Usage go

Jun 14, 2022

Port of Google's Keyczar cryptography library to Go

Important note: Keyczar is deprecated. The Keyczar developers recommend Tink. This is a port of Google's Keyczar library to Go. Copyright (c) 2011 Dam

Nov 28, 2022

Port of webcolors library from Python to Go

go-webcolors A library for working with color names and color value formats defined by the HTML and CSS specifications for use in documents on the Web

Sep 26, 2022

Port of D. J. Bernstein's primegen prime number generator to Go

primegen primegen is a Go package that generates prime numbers in order using the Sieve of Atkin instead of the traditional Sieve of Eratosthenes. It

Jul 20, 2021

Golang port of Petrovich - an inflector for Russian anthroponyms.

Golang port of Petrovich - an inflector for Russian anthroponyms.

Petrovich is the library which inflects Russian names to given grammatical case. This is the Go port of https://github.com/petrovich. Installation go

Dec 25, 2022

A Go port of the Rapid Automatic Keyword Extraction algorithm (RAKE)

A Go implementation of the Rapid Automatic Keyword Extraction (RAKE) algorithm as described in: Rose, S., Engel, D., Cramer, N., & Cowley, W. (2010).

Nov 23, 2022

Go port of Coda Hale's Metrics library

go-metrics Go port of Coda Hale's Metrics library: https://github.com/dropwizard/metrics. Documentation: http://godoc.org/github.com/rcrowley/go-metri

Dec 30, 2022

Go binding to libserialport for serial port functionality.

Go Serial Package serial provides a binding to libserialport for serial port functionality. Serial ports are commonly used with embedded systems, such

Nov 1, 2022

Port of the lemon parser generator to the Go programming language

From the golang-nuts mailing list (with few modifications): --== intro ==-- Hi. I just want to announce a simple port of the lemon parser generator

Feb 17, 2022

An experimental port of TinyRb to Google go, both as a means of learning go and exploring alternate approaches to implementing Ruby. Work is currently focused on the GoLightly VM.

tinyrb¶ ↑ A tiny subset of Ruby with a Lua'esc VM. Everything in TinyRb should run in the big Ruby. (except bugs and things that don't comply to the p

Sep 22, 2022

Interact with Chromium-based browsers' debug port to view open tabs, installed extensions, and cookies

Interact with Chromium-based browsers' debug port to view open tabs, installed extensions, and cookies

WhiteChocolateMacademiaNut Description Interacts with Chromium-based browsers' debug port to view open tabs, installed extensions, and cookies. Tested

Nov 2, 2022

A go port of numpy-financial functions and more.

go-financial This package is a go native port of the numpy-financial package with some additional helper functions. The functions in this package are

Dec 31, 2022

grobotstxt is a native Go port of Google's robots.txt parser and matcher library.

grobotstxt grobotstxt is a native Go port of Google's robots.txt parser and matcher C++ library. Direct function-for-function conversion/port Preserve

Dec 27, 2022

Router socks. One port socks for all the others.

Router socks. One port socks for all the others.

Router socks The next step after compromising a machine is to enumerate the network behind. Many tools exist to expose a socks port on the attacker's

Dec 13, 2022

A port of Rebecca Murphey's js-assessment for Go

A port of Rebecca Murphey's js-assessment for Go

go-assessment Your Job Is To Make The Tests Pass! What is this? This is a tool for assessing or practicing beginner level programming in Golang. It is

Dec 16, 2021

Port of perl5 File::RotateLogs to Go

file-rotatelogs Provide an io.Writer that periodically rotates log files from within the application. Port of File::RotateLogs from Perl to Go. SYNOPS

Jan 9, 2023

Simple HTTP tunnel using SSH remote port forwarding

Simple HTTP tunnel using SSH remote port forwarding

Nov 18, 2022

A fast port forwarding or reverse forwarding tool over HTTP1.0/HTTP1.1

A fast port forwarding or reverse forwarding tool over HTTP1.0/HTTP1.1

gogw What's gogw ? gogw is a port forwarding/reverse forwarding tool over HTTP implements by golang. port forwarding/port reverse forwarding support T

Sep 27, 2022
Comments
  • [WP] ANSI savvy text wrap

    [WP] ANSI savvy text wrap

    Add ANSI-savvy text wrapping mode. This should be explicitly turned on:

    fmt.Println(textwrap.NewTextWrap().SetWidth(80).SetANSISavvy(true).Fill(mytext))
    

    By default Text Wrap does not operates ANSI- aware and is not looking to strip them, if any. This also can be turned ON as of now:

    fmt.Println(textwrap.NewTextWrap().SetWidth(80).SetStripANSI(true).Fill(mytext))
    
A simple logging module for go, with a rotating file feature and console logging.

A simple logging module for go, with a rotating file feature and console logging. Installation go get github.com/jbrodriguez/mlog Usage Sample usage W

Dec 14, 2022
common logger utility module in go

Log & Go This is common logging utility tht is implemented only just for fun :) I will be giving the details about how to use this utility to log ever

Jan 5, 2022
A reusable logger module for basic logging, written in Go
A reusable logger module for basic logging, written in Go

logger A reusable logger module for basic logging, written in Go. Usage Client p

Jan 8, 2022
A project that adds color to golang's logger module

logger A project that adds color to golang's log module Installation go get github.com/christopher18/logger Usage // Import the module import "github

Dec 28, 2021
Go-logger - A sample go module that I stood up quickly to learn how remote modules work in go

go-logger A sample go module that I stood up quickly to learn how remote modules

Jan 9, 2022
Pragmatic and minimalistic module for collecting and sending traces from Go code 💪🏽
Pragmatic and minimalistic module for collecting and sending traces from Go code 💪🏽

tracing-go Pragmatic and minimalistic module for collecting and exporting trace data from the Go code. prometheus/client_golang but for Traces NOTE: T

Jan 6, 2023
Port-proxy - Temporary expose port for remote connections

Port proxy util Temporary expose port for remote connections. E.g. database/wind

Jan 27, 2022
A fully self-contained Nmap like parallel port scanning module in pure Golang that supports SYN-ACK (Silent Scans)

gomap What is gomap? Gomap is a fully self-contained nmap like module for Golang. Unlike other projects which provide nmap C bindings or rely on other

Dec 10, 2022
PHP functions implementation to Golang. This package is for the Go beginners who have developed PHP code before. You can use PHP like functions in your app, module etc. when you add this module to your project.

PHP Functions for Golang - phpfuncs PHP functions implementation to Golang. This package is for the Go beginners who have developed PHP code before. Y

Dec 30, 2022
A Go port of Ruby's dotenv library (Loads environment variables from `.env`.)

GoDotEnv A Go (golang) port of the Ruby dotenv project (which loads env vars from a .env file) From the original Library: Storing configuration in the

Jan 5, 2023