linesprinter

package module
v0.0.1 Latest Latest
Warning

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

Go to latest
Published: Nov 19, 2021 License: MIT Imports: 1 Imported by: 1

README

linesprinter

golang library to print string into fixed length lines

Usage
go get github.com/gonejack/linesprinter

Setting line length to 76 and sep as \r\n

func main() {
    p := linesprinter.NewLinesPrinter(os.Stdout, 76, []byte("\r\n"))
    defer p.Close()
    w := base64.NewEncoder(base64.StdEncoding, p)
    defer w.Close()
    
    t := "this is some random string this is some random string this is some random string"
    w.Write([]byte(t))
}

Output

dGhpcyBpcyBzb21lIHJhbmRvbSBzdHJpbmcgdGhpcyBpcyBzb21lIHJhbmRvbSBzdHJpbmcgdGhp
cyBpcyBzb21lIHJhbmRvbSBzdHJpbmc=

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type LinesPrinter

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

LinesPrinter break string into fixed length lines. caller must call Close to flush remaining bytes and an extra ending sep after use.

func NewLinesPrinter

func NewLinesPrinter(w io.Writer, lineLen int, sep []byte) *LinesPrinter

NewLinesPrinter new a printer with line length limit and separator

Example
package main

import (
	"encoding/base64"
	"os"

	"github.com/gonejack/linesprinter"
)

func main() {
	p := linesprinter.NewLinesPrinter(os.Stdout, 76, []byte("\r\n"))
	defer p.Close()
	w := base64.NewEncoder(base64.StdEncoding, p)
	defer w.Close()

	t := "this is some random string this is some random string this is some random string"
	w.Write([]byte(t))

	// stdout:
	// dGhpcyBpcyBzb21lIHJhbmRvbSBzdHJpbmcgdGhpcyBpcyBzb21lIHJhbmRvbSBzdHJpbmcgdGhp
	// cyBpcyBzb21lIHJhbmRvbSBzdHJpbmc=
}
Output:

func NewLinesPrinterN

func NewLinesPrinterN(w io.Writer, lineLen int, memLine int, sep []byte) *LinesPrinter

NewLinesPrinterN new a printer with control of how many lines could stay in memory thus users can control internal buffer size.

func (*LinesPrinter) Close

func (x *LinesPrinter) Close() (e error)

Close the caller must call Close to flush any partially line, and ending sep.

func (*LinesPrinter) Write

func (x *LinesPrinter) Write(p []byte) (n int, err error)

Jump to

Keyboard shortcuts

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