indent

package module
v1.1.2 Latest Latest
Warning

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

Go to latest
Published: Sep 11, 2019 License: MIT Imports: 10 Imported by: 1

README

go-indent

Package indent provides tools for indentation of UTF-8 text, for the Go programming language.

For example, if you take some text, package indent can make it so it puts tabs, or spaces for indentation in front of each non-empty line.

So, for example, this:

Hello world!

I came; I saw; I conquered.

Can becomes this:

	Hello world!

	I came; I saw; I conquered.

Or:

		Hello world!

		I came; I saw; I conquered.

Or:

-----Hello world!

-----I came; I saw; I conquered.

You can specify how much indentation you want, and what you want the indentation to be (ex: tabs, spaces, or any other symbol, or string).

Documention

Online documentation, which includes examples, can be found at: http://godoc.org/github.com/reiver/go-indent

GoDoc

Example

import "github.com/reiver/go-indent"

// ...

var writer io.Writer

// ...

var indentationWriter indent.Writer

indentationWriter.Indentation = "\t\t"
indentationWriter.Writer      = writer

// ...

var s string =
"This is a sentence.\n"+
"This is another sentence.\n"+
"\n"+
"This is a new paragraph.\n"

// ...

io.WriteString(&indentationWriter, s) // <---- Note we write to the ‘indentationWriter’, and not ‘writer’.

// Result will be:
//
// "\t\tThis is a sentence.\n"+
// "\t\tThis is another sentence.\n"+
// "\n"+
// "\t\tThis is a new paragraph.\n"
//
// Note the "\t\t" indentation in front of each non-empty line.
//
// Note the empty line (i.e., "\n") did not get indentation.

Documentation

Overview

Package indent provides tools for creating, and detecting indentation of UTF-8 text, for the Go programming language.

For example, if you take some text, package indent can make it so it puts tabs, or spaces for indentation in front of each non-empty line.

So, for example, this:

.
Hello world!

I came; I saw; I conquered.

Can becomes this:

.
        Hello world!

        I came; I saw; I conquered.

Or:

.
-----Hello world!

-----I came; I saw; I conquered.

You can specify how much indentation you want, and what you want the indentation to be (ex: tabs, spaces, or any other symbol, or string).

Example

Here is how one might use indent.Writer:

import "github.com/reiver/go-indent"

// ...

var writer io.Writer

// ...

var indentationWriter indent.Writer

indentationWriter.Indentation = "\t\t"
indentationWriter.Writer      = writer

// ...

var s string =
"This is a sentence.\n"+
"This is another sentence.\n"+
"\n"+
"This is a new paragraph.\n"

// ...

io.WriteString(&indentationWriter, s) // <---- Note we write to the ‘indentationWriter’, and not ‘writer’.

// Result will be:
//
// "\t\tThis is a sentence.\n"+
// "\t\tThis is another sentence.\n"+
// "\n"+
// "\t\tThis is a new paragraph.\n"
//
// Note the "\t\t" indentation in front of each non-empty line.
//
// Note the empty line (i.e., "\n") did not get indentation.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Detect added in v1.1.0

func Detect(dst interface{}, src interface{}) error

Detect detects the indentation in ‘src’ and stores what it detected in ‘dst’.

‘dst’ can be an io.Writer, or a []byte.

‘src’ can be an io.ReadSeeker, or an io.ReaderAt, or a string, or a []byte.

Example
package main

import (
	"github.com/reiver/go-indent"

	"fmt"
	"strings"
)

func main() {

	var src string = `		if 5 = x then
			publish "Hello world!"
		end
`

	var dst strings.Builder

	err := indent.Detect(&dst, src)

	if nil != err {
		fmt.Printf("ERROR: something went wrong when trying to detect the indentation: %s\n", err)
		return
	}

	var indentation string = dst.String()

	fmt.Printf("Indentation: %q\n", indentation)

}
Output:
Indentation: "\t\t"

Types

type Writer

type Writer struct {
	Indentation string
	Writer      io.Writer
	// contains filtered or unexported fields
}
Example
package main

import (
	"github.com/reiver/go-indent"

	"bytes"
	"fmt"
	"io"
)

func main() {

	var buffer bytes.Buffer

	var writer io.Writer = &buffer

	var indentationWriter indent.Writer

	indentationWriter.Indentation = "\t\t"
	indentationWriter.Writer = writer

	var s string = "This is a sentence.\n" +
		"This is another sentence.\n" +
		"\n" +
		"This is a new paragraph.\n"

	io.WriteString(&indentationWriter, s) // <---- Note we write to the ‘indentationWriter’, and not ‘writer’.

	fmt.Printf("%q", buffer.String())

}
Output:
"\t\tThis is a sentence.\n\t\tThis is another sentence.\n\n\t\tThis is a new paragraph.\n"

func (*Writer) Write

func (receiver *Writer) Write(bytes []byte) (int, error)

Write makes indent.Writer fit the io.Writer interface.

The returned integer returned from indent.Writer.Write() represents how many bytes of ‘bytes’ was written. I.e., it does not count the indentation that was written.

I.e., so for this example:

var indenter indent.Writer = indent.Writer{
	Indentation: "\t\t",
	Writer:      writer,
}

// ...

var p []byte = []byte("Hello world!") // The length of this string is: 12

n, err := indenter.Write(p)

If everything was successful, then ‘n’ would 12, not 14.

Jump to

Keyboard shortcuts

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