textbuffer

package module
v0.0.0-...-1f8f256 Latest Latest
Warning

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

Go to latest
Published: Aug 19, 2020 License: MIT Imports: 2 Imported by: 0

README

Textbuffer

GoDoc Build Status Go Report Card

Package textbuffer provides a way to buffer text data based on a number of writes.

Sometimes there is a need to use a buffer with predictable split criteria so that data stays logically solid even if only a single chunk gets its way on the final target writer. Splitting text buffers by size is opposed to such behavior as text lines cut an unpredictable way.

An example of such need is maintaining a log lines buffer before writing into in a rotated file every number of writes.

Example:

var buf bytes.Buffer

w := textbuffer.NewWriter(&buf, 2)

_, _ = w.Write([]byte("first\n"))
fmt.Println("Action#1")
fmt.Println(buf.String())

_, _ = w.WriteString("second\n")
fmt.Println("Action#2")
fmt.Println(buf.String())

_, _ = w.Write([]byte("third\n"))
fmt.Println("Action#3")
fmt.Println(buf.String())
// Output:
// Action#1
//
// Action#2
// first
// second
//
// Action#3
// first
// second

Documentation

Overview

Package textbuffer provides a way to buffer text data based on a number of writes.

Sometimes there is a need to use a buffer with predictable split criteria so that data stays logically solid even if only a single chunk gets its way on the final target writer. Splitting text buffers by size is opposed to such behavior as text lines cut an unpredictable way.

An example of such need is maintaining a log lines buffer before writing into in a rotated file every number of writes.

This package is safe for parallel execution.

Example (Basic)
package main

import (
	"bytes"
	"fmt"
	"github.com/ykhrustalev/textbuffer"
)

func main() {
	var buf bytes.Buffer

	w := textbuffer.NewWriter(&buf, 2)

	_, _ = w.Write([]byte("first\n"))
	fmt.Println("Action#1")
	fmt.Println(buf.String())

	_, _ = w.WriteString("second\n")
	fmt.Println("Action#2")
	fmt.Println(buf.String())

	_, _ = w.Write([]byte("third\n"))
	fmt.Println("Action#3")
	fmt.Println(buf.String())
}
Output:

Action#1

Action#2
first
second

Action#3
first
second

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Writer

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

Writer maintains a buffer which is rotated every number of calls to Write method.

Provides synchronized access to the underline writer

func NewWriter

func NewWriter(w io.Writer, cnt int) *Writer

NewWriter creates an instance with corresponding writer and number of calls before the rotation.

func (*Writer) Flush

func (w *Writer) Flush() error

Flush writes buffered data into underline writer.

func (*Writer) Write

func (w *Writer) Write(p []byte) (int, error)

Write provided payload.

func (*Writer) WriteString

func (w *Writer) WriteString(p string) (int, error)

WriteString provided payload as a string.

Jump to

Keyboard shortcuts

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