luceio

package
v0.0.0-...-157c9c8 Latest Latest
Warning

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

Go to latest
Published: Apr 26, 2024 License: GPL-3.0 Imports: 7 Imported by: 0

Documentation

Overview

Package luceio profides helpers that extend io in particular, helping build instances of Writer and WriterTo.

Index

Constants

This section is empty.

Variables

View Source
var WrapWidth = 80

WrapWidth allows setting a default width to be used by new instances of LineWrapperContext

Functions

This section is empty.

Types

type DefaulLineWrapperContext

type DefaulLineWrapperContext struct{}

DefaulLineWrapperContext will return the package level WrapWidth and an empty string for the padding

func (DefaulLineWrapperContext) Padding

func (DefaulLineWrapperContext) Padding() string

Padding returns an empty string

func (DefaulLineWrapperContext) WrapWidth

func (DefaulLineWrapperContext) WrapWidth() int

WrapWidth returns package level WrapWidth

type LineWrapperContext

type LineWrapperContext interface {
	WrapWidth() int
	Padding() string
}

LineWrapperContext returns contextual information to guide the writing operation. It is assumed that these values will not change with respect to an instance.

LineWrapperContext only handle Unix style line endings.

type LineWrapperContextWriter

type LineWrapperContextWriter struct {
	io.Writer
	Width int
	Pad   string
}

LineWrapperContextWriter provides a method to add Width and Padding Context to a Writer.

func (LineWrapperContextWriter) Padding

func (lwcw LineWrapperContextWriter) Padding() string

Padding fulfills LineWrapperContext providing the Padding

func (LineWrapperContextWriter) WrapWidth

func (lwcw LineWrapperContextWriter) WrapWidth() int

WrapWidth fulfills LineWrapperContext providing the Width

type LineWrappingWriter

type LineWrappingWriter struct {
	LineWrapperContext
	*SumWriter
	// contains filtered or unexported fields
}

LineWrappingWriter fulfills io.Writer.

func NewLineWrappingWriter

func NewLineWrappingWriter(w io.Writer) *LineWrappingWriter

NewLineWrappingWriter returns a LineWrappingWriter that will write to the underlying writer. It will try to upgrade the writer to LineWrapperContext but if that fails, it will use the DefaulLineWrapperContext. The LineWrapperContextWriter can be used to wrap the Writer and set Width and Padding.

func (*LineWrappingWriter) Write

func (w *LineWrappingWriter) Write(b []byte) (int, error)

func (*LineWrappingWriter) WriteNewline

func (w *LineWrappingWriter) WriteNewline() (int, error)

WriteNewline writes a new line followed by the necessary padding

func (*LineWrappingWriter) WritePadding

func (w *LineWrappingWriter) WritePadding() (int, error)

WritePadding writes the padding set by the context.

type Replacer

type Replacer interface {
	WriteString(io.Writer, string) (int, error)
}

type ReplacerWriter

type ReplacerWriter struct {
	Writer io.Writer
	Replacer
}

ReplacerWriter invokes a Replacer on any data before sending it to the underlying Writer.

func NewReplacer

func NewReplacer(w io.Writer, oldnew ...string) ReplacerWriter

func (ReplacerWriter) Write

func (rw ReplacerWriter) Write(b []byte) (int, error)

Write casts the []byte to string, calls Replacer and writes to the underlying writer.

func (ReplacerWriter) WriteString

func (rw ReplacerWriter) WriteString(str string) (int, error)

type StringWriter

type StringWriter interface {
	WriteString(string) (int, error)
}

StringWriter is the interface that wraps the WriteString method

type StringWriterTo

type StringWriterTo string

StringWriterTo fulfils the WriterTo interface and writes the string to the writer

func (StringWriterTo) WriteTo

func (s StringWriterTo) WriteTo(w io.Writer) (int64, error)

WriteTo fulfills the WriterTo interface.

type StringsWriter

type StringsWriter interface {
	WriteStrings(...string) (int, error)
}

StringWriter is the interface that wraps the WriteString method

type SumWriter

type SumWriter struct {
	io.Writer
	// Cache holds bytes that will be written before the next write operation.
	// If no write operation is executed, they will never be written.
	Cache []byte
	Sum   int64
	Err   error
}

SumWriter is helper that wraps a Writer and sums the bytes written. If it encounters an error, it will stop writing.

func BufferSumWriter

func BufferSumWriter() (*bytes.Buffer, *SumWriter)

BufferSumWriter creates a new buffer passing it into the SumWriter and returns both.

func NewSumWriter

func NewSumWriter(w io.Writer) *SumWriter

NewSumWriter takes a Writer and returns a SumWriter

func (*SumWriter) AppendCache

func (s *SumWriter) AppendCache(b []byte)

AppendCache will append a byte slice to the current Cache value.

func (*SumWriter) AppendCacheString

func (s *SumWriter) AppendCacheString(str string)

AppendCacheString will append a string to the current Cache value.

func (*SumWriter) Diff

func (s *SumWriter) Diff(d int64) (int, error)

Diff is a shorthand way to handle returns by marking the difference in the Sum.

func (*SumWriter) Fprint

func (s *SumWriter) Fprint(format string, args ...interface{}) (int, error)

Fprint wraps a call to Fprintf.

func (*SumWriter) Join

func (s *SumWriter) Join(elems []string, sep string) (int, error)

Join a list of strings using a provided seperator.

func (*SumWriter) Rets

func (s *SumWriter) Rets() (int64, error)

Rets is a shorthand helper for returns

func (*SumWriter) Wrapped

func (s *SumWriter) Wrapped() any

TODO:

func (*SumWriter) Write

func (s *SumWriter) Write(b []byte) (int, error)

Write fulfills io.Write

func (*SumWriter) WriteInt

func (s *SumWriter) WriteInt(i int) (int, error)

WriteInt uses strconv to write an int

func (*SumWriter) WriteRune

func (s *SumWriter) WriteRune(r rune)

WriteRune writes a rune to underlying Writer

func (*SumWriter) WriteString

func (s *SumWriter) WriteString(str string) (int, error)

WriteString writes a string to underlying Writer

func (*SumWriter) WriteStrings

func (s *SumWriter) WriteStrings(strs ...string) (int, error)

WriteStrings writes strings to underlying Writer

func (*SumWriter) WriterTo

func (s *SumWriter) WriterTo(w io.WriterTo) (int64, error)

WriterTo passes the SumWriter into a WriterTo and captures the character length and error.

type TemplateExecutor

type TemplateExecutor interface {
	ExecuteTemplate(io.Writer, string, interface{}) error
	Execute(io.Writer, interface{}) error
}

TemplateExecutor is an interface representing the ExecuteTemplate method on a template.

type TemplateTo

type TemplateTo struct {
	TemplateExecutor
	Name string
	Data interface{}
}

TemplateTo writes a template and fulfils WriterTo. If Name is blank, the base template is used, otherwise the named template is used.

func NewTemplateTo

func NewTemplateTo(template TemplateExecutor, name string, data interface{}) *TemplateTo

NewTemplateTo returns a TemplateTo which fulfils WriterTo

func (*TemplateTo) WriteTo

func (t *TemplateTo) WriteTo(w io.Writer) (int64, error)

WriteTo writes a template and fulfils WriterTo.

type TemplateWrapper

type TemplateWrapper struct {
	TemplateExecutor
}

TemplateWrapper allows a template to generate insances of TemplateTo

func (TemplateWrapper) TemplateTo

func (t TemplateWrapper) TemplateTo(name string, data interface{}) *TemplateTo

TemplateTo combines the template, name and data and fulfills io.WriterTo

type WriterToSeperator

type WriterToSeperator struct {
	WriterTos
	Seperator []byte
}

func (WriterToSeperator) WriteTo

func (wts WriterToSeperator) WriteTo(w io.Writer) (int64, error)

type WriterTos

type WriterTos []io.WriterTo

WriterTos takes a slice of WriterTos and provides a single WriteTo method that will call each of them.

func (WriterTos) Merge

func (wts WriterTos) Merge() WriterTos

func (WriterTos) Seperator

func (wts WriterTos) Seperator(seperator any) WriterToSeperator

func (WriterTos) WriteTo

func (wts WriterTos) WriteTo(w io.Writer) (int64, error)

WriteTo invokes each of the WriterTos.

Jump to

Keyboard shortcuts

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