linesbuf

package module
v0.0.0-...-ec2f57a Latest Latest
Warning

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

Go to latest
Published: Mar 30, 2021 License: MIT Imports: 5 Imported by: 0

README ¶

🌈 linesbuf

Package linesbuf implements a buffer of in memory string

Documentation

Contributing

License

MIT Licensed

© 2021 Andrea Parodi

Documentation ¶

Overview ¶

Package linesbuf implements a buffer of in memory string.

Index ¶

Examples ¶

Constants ¶

This section is empty.

Variables ¶

This section is empty.

Functions ¶

This section is empty.

Types ¶

type LinesBuf ¶

type LinesBuf struct {
	// contains filtered or unexported fields
}

LinesBuf contains a bytes.Buffer and simplify writing lines of text to it.

The empty value of a LinesBuf is already usable.

You can recycle LinesBuf multiple times, any call to WriteFile will truncate the underlying bytes.Buffer to 0.

If you want to start from scratch without having to write to a file, you can call Discard method.

Example ¶

This example show how to use a linesbuf.LinesBuf

package main

import (
	"embed"
	"io/fs"
	"os"

	"github.com/parro-it/linesbuf"
)

//go:embed fixtures
var fixtureRootFS embed.FS
var fixtureFS, _ = fs.Sub(fixtureRootFS, "fixtures")

// This example show how to use a linesbuf.LinesBuf
func main() {
	var lines linesbuf.LinesBuf
	lines.AddLine("A simple line")
	lines.AddLine("Trailing new lines are not needed\n")
	lines.AddLine("You can use %s arguments", "fmt.Printf")
	lines.Write(os.Stdout)
}
Output:

A simple line
Trailing new lines are not needed

You can use fmt.Printf arguments

func (*LinesBuf) AddLine ¶

func (lines *LinesBuf) AddLine(lineFormat string, arguments ...interface{})

AddLine writes a line of text in the buffer. The line to append is built calling `fmt.Sprintf` with `lineFormat` and `arguments...`

func (*LinesBuf) Discard ¶

func (lines *LinesBuf) Discard()

Discard truncates the lines buffer, so that you can recycle the same LinesBuf as if it was newly created.

func (*LinesBuf) Write ¶

func (lines *LinesBuf) Write(dest io.Writer) error

Write writes all lines written to the buffer so far in a io.Writer.

If if the write operation succeed, Write truncates the internal lines buffer, so that you can recycle the same LinesBuf as if it was newly created.

If an error occurrs during the writing, no truncation of the buffer is done, so that you can retry the method call later.

func (*LinesBuf) WriteFile ¶

func (lines *LinesBuf) WriteFile(filepath string) error

WriteFile writes all lines written to the buffer so far to a file with given path.

After the file is written successfully, WriteFile truncates the internal lines buffer, so that you can recycle the same LinesBuf as if it was newly created.

If an error occurrs during the writing of the file, no truncation of the buffer is done, so that you can retry the method call later.

Jump to

Keyboard shortcuts

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