decowriter

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Sep 8, 2024 License: Apache-2.0 Imports: 4 Imported by: 0

README

decowriter

Package decowriter provides a writer that add a prefix and a suffix to each line.

Go Reference Go Report Card

A line is defines as a sequence of zero or more non-'\n' bytes followed by a '\n'.

A prefix is written before one or more bytes of a line.

A suffix is written just before the trailing '\n'.

A string without a trailing '\n' is not recognized as a line, so no suffix is written. This allows you to split a single line writing across two Write calls.

Features

  • Adds a specified prefix to the beginning of each line
  • Adds a specified suffix to the end of each line
  • Implements the io.Writer interface
  • Tracks total bytes written

Usage

package main

import (
	"fmt"
	"os"

	"github.com/goaux/decowriter"
)

func main() {
	w := decowriter.New(bufio.NewWriter(os.Stdout), []byte(">>"), []byte("<<"))
	fmt.Fprintln(w, "This is a log message")
	fmt.Fprintln(w, "Another log message")
}

OUTPUT:

>>This is a log message<<
>>Another log message<<

Documentation

Overview

Package decowriter provides a writer that add a prefix and a suffix to each line.

This package is the successor of github.com/goaux/prefixwriter. Compared to prefixwriter, it is the caller's responsibility to use bufio with decowriter. decowriter uses the underlying io.Writer directly.

Example
package main

import (
	"bufio"
	"fmt"
	"os"

	"github.com/goaux/decowriter"
)

func main() {
	w := decowriter.New(bufio.NewWriter(os.Stdout), []byte(">>"), []byte("<<"))
	fmt.Fprint(w, "hello")
	fmt.Fprintln(w, " world")
	fmt.Fprintln(w, "hello")
	fmt.Fprint(w, "world")
	fmt.Printf("\ntotal: %d\n", w.Written())
}
Output:

>>hello world<<
>>hello<<
>>world
total: 33
Example (Readme)
package main

import (
	"bufio"
	"fmt"
	"os"

	"github.com/goaux/decowriter"
)

func main() {
	w := decowriter.New(bufio.NewWriter(os.Stdout), []byte(">>"), []byte("<<"))
	fmt.Fprintln(w, "This is a log message")
	fmt.Fprintln(w, "Another log message")
}
Output:

>>This is a log message<<
>>Another log message<<

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 is the writer that add a prefix and a suffix to each line. It implements the io.Writer interface.

Writer defines a line as a sequence of zero or more non-'\n' bytes followed by a '\n'. A prefix is written before one or more bytes of a line. A suffix is written just before the trailing '\n'. A string without a trailing '\n' is not recognized as a line, so no suffix is written. This allows you to split a single line write across two Write calls.

func New

func New(w io.Writer, prefix, suffix []byte) *Writer

New creates a new Writer that wraps the given io.Writer.

func (*Writer) Flush

func (w *Writer) Flush() error

Flush calls the Flush method of underlying writer if it has, otherwise returns nil.

func (*Writer) Write

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

Write implements the io.Writer interface. It writes the given byte slice to the underlying writer, adding the prefix at the beginning of each line, adding the suffix at the end of each line.

The returned int n represents the number of bytes from the input slice p that were processed, not including any added prefixes nor suffixes. This means that n <= len(p), even though the actual number of bytes written to the underlying writer may be larger due to the added prefixes and suffixes.

If p contains no data (len(p) == 0), Write will not perform any operation and will return n = 0 and a nil error.

An error is returned if the underlying writer returns an error, or if the Write operation cannot be completed fully.

If one or more bytes are written to the underlying writer and the processing is completed normally, and if the underlying writer has `Flush() error` method, Flush is called once at the end.

func (*Writer) Written

func (w *Writer) Written() int64

Written returns the total number of bytes written, including prefixes.

Jump to

Keyboard shortcuts

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