indent

package module
v1.2.1 Latest Latest
Warning

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

Go to latest
Published: Jan 24, 2023 License: Apache-2.0 Imports: 4 Imported by: 6

README

indent build statusGoDoc

The indent package indents lines of text with a prefix. It supports indent blocks of text (string or []byte) as well as providing an io.Writer interface. It is a drop-in replacement for the github.com/openconfig/goyang/pkg/indent package.

For example, indenting the text in the first block below with a >> prefix using any of the methods will result in the second block below.

Line 1
Line 2
Line 3
>>Line 1
>>Line 2
>>Line 3

The New function is used to create a writer. Writers may be nested. For example the following code produces the output that follows in the next block.

package main

import (
	"fmt"
	"os"

	"github.com/pborman/indent"
)

func main() {
	w := indent.New(os.Stdout, "// ")

	fmt.Fprintf(w, "type foo struct {\n")
	wi := indent.New(w, "\t")
	fmt.Fprintf(wi, "A string\n")
	fmt.Fprintf(wi, "B int\n")
	fmt.Fprintf(w, "}\n")
}
// type foo struct {
// 	A string
// 	B int
// }

The New function is intelligent about nesting so the written text is only processed once prior to sending to the original io.Writer.

Documentation

Overview

Package indent indents lines of text with a prefix. The New function is used to return a writer that indents the lines written to it. For example:

	var buf bytes.Buffer
	w := indent.New(&buf, "> ")
 	w.Write([]byte(`line 1
line 2
line 3
`)

will result in

> line 1
> line 2
> line 3

Indenters may be nested:

	var buf bytes.Buffer
	w := indent.New(&buf, "> ")
 	w.Write([]byte("line 1\n"))
	nw := indent.New(w, "..")
 	nw.Write([]byte("line 2\n"))
 	w.Write([]byte("line 3\n"))

will result in

> line 1
> ..line 2
> line 3

The String and Bytes functions are optimized equivalents of

var buf bytes.Buffer()
indent.New(&buf, prefix).Write(input)
return buf.String() // or buf.Bytes()

Index

Constants

This section is empty.

Variables

View Source
var NewWriter = New

NewWriter is the name used in github.com/openconfig/goyang/pkg/indent.

Functions

func Bytes

func Bytes(prefix, input []byte) []byte

Bytes returns input with each line in input prefixed by prefix.

func New

func New(w io.Writer, prefix string) io.Writer

New returns a writer that will prefix all lines written to it with prefix and then writes the results to w. New is intelligent about recursive calls to New. New return w if prefix is the empty string. When nesting, New does not assume it is at the start of a line, it maintains this information as you nest and unwind indenters. It normally is best to only transition between nested writers after a newline has been written.

func NewPostfix added in v1.2.0

func NewPostfix(w io.Writer, indent, postfix string) io.Writer

func String

func String(prefix, input string) string

String returns input with each line in input prefixed by prefix.

func Unwrap added in v1.1.0

func Unwrap(w io.Writer, n int) io.Writer

Unwrap unwraps and indenter and returns the underlying io.Writer. It will unwrap up to n times or until an io.Writer that is not an indenter is unwrapped. If n is 0 then w is returned. if n is less than zero then all indenters are unwrapped. You should only unwrap after a newline has been written. Anything written to the unwrapped io.Writer should also end with a newline.

Types

This section is empty.

Jump to

Keyboard shortcuts

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