p36lib

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

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

Go to latest
Published: Nov 7, 2019 License: GPL-3.0 Imports: 12 Imported by: 4

README

p36lib-go

p36lib-go is a library that has various utility functions that I tend to use over and over. This package lets me collect them into one place. It's considered a perpetual work-in-progress.

Most of the utility functions are geared towards binary I/O, or formatting console output, but there are a few others as well.

p36lib-go is basically the Go counterpart to my p36-lib Common Lisp library P36-LIB.

How do I get set up?

Just run this in your workspace:

go get gitlab.com/RemiliaScarlet/p36lib-go

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	StdoutColored = colorable.NewColorableStdout()
	StderrColored = colorable.NewColorableStderr()
)
View Source
var SizeSuffixes = [9]string{"Bytes", "KiB", "MiB", "GiB", "TiB", "PiB", "EiB", "ZiB", "YiB"}

The suffixes used by HumanSized()

Functions

func Copy

func Copy(source, dest string, flags int, perm os.FileMode) error

Copies a file from source to dest. Both source and dest are filenames. If an error occurs, that error is returned. Otherwise nil is returned. flags and perm are the same as in os.OpenFile().

func DebugMsg

func DebugMsg(header string, msg string, args ...interface{})

func ErrFormatColored

func ErrFormatColored(color FormatColor, msg string, fmtArgs ...interface{})

Formats and prints colored text to the terminal's standard error stream.

func ErrorMsg

func ErrorMsg(header string, msg string, args ...interface{})

func FormatBold

func FormatBold(msg string, fmtArgs ...interface{})

A convenience function that formats and prints bold white text to the terminal's standard output stream. This is equivalent to calling, as an example:

FormatColored(FormatWhiteBold, "My Message\n")

func FormatColored

func FormatColored(color FormatColor, msg string, fmtArgs ...interface{})

Formats and prints colored text to the terminal's standard output stream.

func FormatWrap

func FormatWrap(out io.Writer, width int, indent int, str string, fmtArgs ...interface{})

Convenience function that formats and prints text to an io.Writer, wrapping the text with WrapString first.

func FormatWrapSpecial

func FormatWrapSpecial(out io.Writer, width int, indent int, firstLineOffset int, str string, fmtArgs ...interface{})

Convenience function that formats and prints text to an io.Writer, wrapping the text with WrapStringSpecial first.

func HumanSized

func HumanSized(num int) string

Takes an integer and returns a string containing its representation as a floating point number, appending one of the strings in SuffixSizes depending on its magnitude. This basically pretty prints a size (for example, 1024 bytes is returned as "1.00 KiB").

func InfoMsg

func InfoMsg(header string, msg string, args ...interface{})

func NewColoredStream

func NewColoredStream(file *os.File) io.Writer

func PadString

func PadString(str string, desiredLen int) string

Takes str and adds desiredLen null bytes to the end of it, then returns the new string.

func PadStringL

func PadStringL(str string, pad string, length int) string

Takes str and prepends pad onto it until it is length bytes long, then returns the new string. If appending the padding onto the string makes it longer than length, the result is first truncated so that it is exactly length bytes.

func PadStringR

func PadStringR(str string, pad string, length int) string

Takes str and appends pad onto it until it is length bytes long, then returns the new string. If appending the padding onto the string makes it longer than length, the result is first truncated so that it is exactly length bytes.

func ReadByte

func ReadByte(inStream io.Reader) (byte, error)

Reads a single byte from inStream and returns it and nil. If an error occurs, 0 and that error is returned. If it could not read a byte, 0 and a new error is returned.

func ReadInt16BE

func ReadInt16BE(inStream io.Reader) (int16, error)

Reads a 16-bit signed big endian integer from inStream and returns it and nil. If an error occurs, 0 and an error is returned.

func ReadInt16LE

func ReadInt16LE(inStream io.Reader) (int16, error)

Reads a 16-bit signedf little endian integer from inStream and returns it and nil. If an error occurs, 0 and an error is returned.

func ReadInt32BE

func ReadInt32BE(inStream io.Reader) (int32, error)

Reads a 32-bit signed big endian integer from inStream and returns it and nil. If an error occurs, 0 and an error is returned.

func ReadInt32LE

func ReadInt32LE(inStream io.Reader) (int32, error)

Reads a 32-bit signedf little endian integer from inStream and returns it and nil. If an error occurs, 0 and an error is returned.

func ReadInt64LE

func ReadInt64LE(inStream io.Reader) (int64, error)

Reads a 64-bit signedf little endian integer from inStream and returns it and nil. If an error occurs, 0 and an error is returned.

func ReadNullString

func ReadNullString(inStream io.Reader, length uint) (string, error)

Reads a null-terminated string of at most length bytes from inStream and returns it and nil. If an error occurs, an empty string and an error is returned.

func ReadString

func ReadString(inStream io.Reader, length uint) (string, error)

Reads a string of length bytes from inStream and returns it and nil. If an error occurs, an empty string and an error is returned.

func ReadTrimString

func ReadTrimString(inStream io.Reader, length uint, keepNulls bool, fromFirstNull bool) (string, error)

Reads a string of length bytes from inStream, optinally removing null bytes. If keepNulls is true, no nulls are removed. If fromFirstNull is true, all bytes starting from the first null byte are removed, whether the remaining bytes are null or not.. If an error occurs, an empty string and an error is returned.

func ReadUint16LE

func ReadUint16LE(inStream io.Reader) (uint16, error)

Reads a 16-bit unsignedf little endian integer from inStream and returns it and nil. If an error occurs, 0 and an error is returned.

func ReadUint32LE

func ReadUint32LE(inStream io.Reader) (uint32, error)

Reads a 32-bit unsignedf little endian integer from inStream and returns it and nil. If an error occurs, 0 and an error is returned.

func ReadUint64LE

func ReadUint64LE(inStream io.Reader) (uint64, error)

Reads a 64-bit unsignedf little endian integer from inStream and returns it and nil. If an error occurs, 0 and an error is returned.

func StreamFormatColored

func StreamFormatColored(out io.Writer, color FormatColor, msg string, fmtArgs ...interface{})

func WarnMsg

func WarnMsg(header string, msg string, args ...interface{})

func WrapString

func WrapString(str string, lineWidth int, indentWidth int) string

Reformats str so that none of its lines are longer than lineWidth. indentWidth spaces are also is prepended onto each reformatted line. Returns the newly formatted string.

func WrapStringSpecial

func WrapStringSpecial(str string, lineWidth int, indentWidth int, firstLineOffset int) string

Reformats str so that none of its lines are longer than lineWidth. indentWidth spaces are prepended onto each reformatted line. Additionally, the first line's width is reduced by firstLineOffset before indentation is applied to it. Returns the newly formatted string.

func WriteByte

func WriteByte(num byte, outStr io.Writer) error

Writes a single byte to outStr. On success, nil is returned. If an error occurs, an error is returned.

func WriteIndent

func WriteIndent(out io.Writer, width int)

Writes width spaces to out.

func WriteInt16LE

func WriteInt16LE(num int16, outStr io.Writer) error

Writes a 16-bit signed little-endian integer to outStr. On success, nil is returned. If an error occurs, an error is returned.

func WriteInt32LE

func WriteInt32LE(num int32, outStr io.Writer) error

Writes a 32-bit signed little-endian integer to outStr. On success, nil is returned. If an error occurs, an error is returned.

func WriteInt64LE

func WriteInt64LE(num int64, outStr io.Writer) error

Writes a 64-bit signed little-endian integer to outStr. On success, nil is returned. If an error occurs, an error is returned.

func WritePadding

func WritePadding(length int, outStr io.Writer)

Writes length null bytes to outStr.

func WriteUint16LE

func WriteUint16LE(num uint16, outStr io.Writer) error

Writes a 16-bit unsigned little-endian integer to outStr. On success, nil is returned. If an error occurs, an error is returned.

func WriteUint32LE

func WriteUint32LE(num uint32, outStr io.Writer) error

Writes a 32-bit unsigned little-endian integer to outStr. On success, nil is returned. If an error occurs, an error is returned.

func WriteUint64LE

func WriteUint64LE(num uint64, outStr io.Writer) error

Writes a 64-bit unsigned little-endian integer to outStr. On success, nil is returned. If an error occurs, an error is returned.

func YesOrNo

func YesOrNo(question string, fullResponse bool) bool

Prints question to the terminal's standard output, then either "(y/n)" or "(yes/no)" depending on if fullResponse is false or true, respectively. This then waits for the user to respond with a "y" or "n" (if fullResponse is false), a full "yes" or "no". Input is not case sensitive.

Types

type FormatColor

type FormatColor int
const (
	FormatRed FormatColor = iota
	FormatYellow
	FormatWhite
	FormatBlue
	FormatGreen
	FormatBlack
	FormatMagenta
	FormatCyan

	FormatRedBold
	FormatYellowBold
	FormatWhiteBold
	FormatBlueBold
	FormatGreenBold
	FormatMagentaBold
	FormatCyanBold
)

Jump to

Keyboard shortcuts

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