formatter

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Nov 2, 2019 License: Apache-2.0 Imports: 1 Imported by: 3

README

formatter

Build Status codecov GoDoc goreadme

formatter is a library for formatting text.

The Formatter wraps an io.Writer and formats text written to it. It enables text indenting and line width wrapping.

Examples
// Create a new formatter.
f := Formatter{
    // For formatter wraps a writer. The writer is where the output will be written to.
    Writer: os.Stdout,
    // Define text indentation. This could be spaces, tabs or any other character set. It will
    // be inserted in the beginning of the text and after every new line. Leave it nil for not
    // indenting the text.
    Indent: []byte("  "),
    // Width defines line width for applying line wrap. Any non-positive number will be ignored.
    Width: 100,
}
// Writing into the formatter will result in the formatted text being written into the defined
// `Formatter.Writer`.
_, err := f.Write([]byte(loremIpsum))
if err != nil {
    // Handle error :-)
    panic("failed writing Lorem Ipsum")
}

// The result is the same text formatted with indentation of two spaces and 100 characters line
// width:

Created by goreadme

Documentation

Overview

formatter is a library for formatting text.

The `Formatter` wraps an `io.Writer` and formats text written to it. It enables text indenting and line width wrapping.

Example
// Create a new formatter.
f := Formatter{
	// For formatter wraps a writer. The writer is where the output will be written to.
	Writer: os.Stdout,
	// Define text indentation. This could be spaces, tabs or any other character set. It will
	// be inserted in the beginning of the text and after every new line. Leave it nil for not
	// indenting the text.
	Indent: []byte("  "),
	// Width defines line width for applying line wrap. Any non-positive number will be ignored.
	Width: 100,
}
// Writing into the formatter will result in the formatted text being written into the defined
// `Formatter.Writer`.
_, err := f.Write([]byte(loremIpsum))
if err != nil {
	// Handle error :-)
	panic("failed writing Lorem Ipsum")
}

// The result is the same text formatted with indentation of two spaces and 100 characters line
// width:
Output:
  Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut
  labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris
  nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit
  esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt
  in culpa qui officia deserunt mollit anim id est laborum.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Formatter

type Formatter struct {
	// Writer to write the formatted text.
	io.Writer
	// Indent will be prefixed to every new line. An empty slice will be ignored.
	Indent []byte
	// Width defines the line size for wrapping the text. A non positive value will be ignored.
	Width int
	// contains filtered or unexported fields
}

Formatter formats text that is written to it and write the formatted text to a given `io.Writer`.

func (*Formatter) Write

func (f *Formatter) Write(b []byte) (int, error)

Jump to

Keyboard shortcuts

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