stdres

package module
v0.0.0-...-5c10e33 Latest Latest
Warning

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

Go to latest
Published: Nov 25, 2015 License: MIT Imports: 1 Imported by: 2

README

StdRes

Print action results to STDOUT

For license information, see LICENSE.

Introduction

Package stdres provides the means to select colors for STDOUT based on action results. By using this package you are able to print text to the buffer, and postpone the color selection until action has finished.

What can StdRes be used for?

If you write a compiler or test framework using colorized output, but don't want to deal with text later, instead you want to record text before action has finished and reduce if-statements and clutter in your code.

Note that this library will not update already printed text, it only buffers output till later, when the result is known.

Installation

go get github.com/dekelund/stdres

Usage

See documentation and test file for more information.

Documentation

Overview

Package stdres provides the means to select colors for STDOUT based on action results. By using this package you are able to print text to the buffer, and postpone the color selection until action has finished.

Typical scenario: The tool decides what to do, and the tool informs the user what's going to happen. The tool starts to run the command and print more information. During execution of the action, the tool fails and sets the result to failure.

Before the tool starts to exuecute the next action, it calls method Buffer.Flush and the existing records and flushed in corresponding colors, in this case red text due to failure.

Example
package main

import (
	"github.com/dekelund/stdres"
)

func main() {
	var buffer stdres.Buffer
	buffer = stdres.Buffer{}

	firstAction := buffer.Println("Executing first action")
	secondAction := buffer.Println("Executing second action")
	thirdAction := buffer.Println("Executing third action")
	fourthAction := buffer.Printf("Executing fourth action, printed with number one - %d\n", 1)
	buffer.Print("Executing fifth action") // Never updated, printed with UNKNOWN state which result in Cyan color

	firstAction.Result = stdres.SUCCESS  // First action succeeded, line printed in green
	secondAction.Result = stdres.FAILURE // Second action failed, line printed in red
	thirdAction.Result = stdres.INFO     // Third action was just information, line printed in blue
	fourthAction.Result = stdres.PLAIN   // Fourth action printed with white text

	stdres.DisableColor() // Use this method to remove color output
	buffer.Flush()        // Only print if and when we flush, update result before flush.

}
Output:

Executing first action
Executing second action
Executing third action
Executing fourth action, printed with number one - 1
Executing fifth action

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func DisableColor

func DisableColor()

func EnableColor

func EnableColor()

Types

type Buffer

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

func (*Buffer) Flush

func (outBuffer *Buffer) Flush()

Flash iterates through all records in Buffer and flush it to stdout preceded by color matching current status. Each record will be followed by color reset if it was preceded by a color.

func (*Buffer) Print

func (outBuffer *Buffer) Print(message string) *Record

Println records message to buffer to be printed later. Text not printed until Buffer.Flush has been called. It returns current status and text string as Record.

func (*Buffer) Printf

func (outBuffer *Buffer) Printf(format string, a ...interface{}) *Record

Printf records formatted message to buffer to be printed later Printf formats according to a format specifier and returns current status and text as Record.

func (*Buffer) Println

func (outBuffer *Buffer) Println(message string) *Record

Println records message followed by newline to buffer to be printed later Text not printed until Buffer.Flush has been called. It returns current status and text string as Record.

type Record

type Record struct {
	Result
	Message string
}

Record struct keeps record of message to print and current result

func (*Record) String

func (out *Record) String() string

type Result

type Result uint
const (
	UNKNOWN Result = iota
	PENDING
	FAILURE
	SUCCESS
	INFO
	PLAIN
)

Notes

Bugs

  • Current version is not thread-safe

Jump to

Keyboard shortcuts

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