toolprinter

package module
v1.0.12 Latest Latest
Warning

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

Go to latest
Published: Nov 9, 2023 License: MIT Imports: 8 Imported by: 1

README

Tool Printer

CLI tools typically need to show progress status temporarily in the midst of regular console output. This Tool Printer interface was created as a way to separate "progress bar" printing from "final output".

The interface is:

type ToolPrinter interface {
	Status(text ...interface{})
	Statusf(format string, args ...interface{})
	Clear()
	ChattyStatus(text ...interface{})
	ChattyStatusf(format string, args ...interface{})
	SetCounterMax(max int, text ...interface{})
	UpdateCountStatus(extraStatusText ...interface{})
	Count()
	PauseStatus()
	ResumeStatus()
	Println(text ...interface{})
	Printlnf(format string, args ...interface{})
	BeginPrint(text ...interface{})
	ContinuePrint(text ...interface{})
	ContinuePrintf(format string, args ...interface{})
	EndPrint(text ...interface{})
	EndPrintIfStarted()
	DateRangeStatus(from time.Time, to time.Time, purpose ...interface{})
	VerbosePrintln(text ...interface{})
	VerbosePrintlnf(format string, args ...interface{})
	EnableVerbose(enabled bool)
}

A ttl implementation is provided via toolprinter.NewToolPrinter(). This implmentation will print status neatly to a ttl, while suppressing it if stdout is redirected to a non-ttl device such as a file.

Example Use:

package main

import (
	"github.com/jimsnab/go-toolprinter"
)

func main() {
	prn := toolprinter.NewToolPrinter()

	for i := 0; i < 100; i++ {
		prn.Statusf("Processing i at %d", i)
		if i%9 == 0 {
			prn.Printlnf("i is divisble by 9 at %d", i)
		}
	}

	prn.Clear()
}

try it

Status

Status() and Statusf() print a single line status message, without a new line. Clear() erases the status message. ChattyStatus() and ChattyStatusf() print status once every 250 ms; for status within high speed processing.

PauseStatus() clears the status message (if any), typically because regular printing is desired. ResumeStatus() re-prints the latest status message.

Date range status can be printed with DateRangeStatus().

Progress Counter

SetCounterMax() starts a progress status line, such as:

  prn.SetCounterMax("Reading", 1000)

produces the following status message:

Reading 1 of 1000 0%

Calling Count() increments the counter, and the status message with progress percentage is updated. It won't go beyond 100%.

Suppose your program is processing three items, and the processing of each item takes several minutes, and the processing of one item can be broken down into individual named steps. For example, let's say they are "querying", "sorting", "analyzing", "generating output". The program can call UpdateCountStatus() to print querying, sorting, etc., to achieve status such as:

Processing 1 of 3 33% querying

then

Processing 1 of 3 33% sorting

then

Processing 1 of 3 33% analyzing

and so on.

Printing Regular Output

To use fmt, log or something similar that produces stdout output, first call PauseStatus(), then print to stdout, then call ResumeStatus().

When you can avoid fmt and log, use the following ToolPrinter functions to print regular output:

  • Println() and Printlnf() - prints a single line of regular output. Any status message is cleared before printing the line, and restored after printing the line.
  • BeginPrint(), ContinuePrint(), ContinuePrintf() and EndPrint() - prints regular output in multiple print calls. BeginPrint() erases the status message, if any, and ContinuePrint()/ContinuePrintf() print text exactly as it is specified and does not add a line break. EndPrint() prints a line break, then reprints the status message, if any.
  • EndPrintIfStarted() will perform EndPrint() only if printing was started with BeginPrint() but not ended.

Verbose Printing

Tools often have different levels of printing. This library supports two - regular and verbose.

  • VerbosePrintln() and VerbosePrintlnf() only print when verbose is enabled.
  • EnableVerbose() controls whether verbose printing is on or off

String Printer

Print to a string buffer using NewStringPrinter(). Get the content via sp.Text() where sp is your string printer instance.

Test Printer

For unit tests, a test version of the ToolPrinter interface is provided. Instead of printing, it captures output lines into an array, giving unit tests an easy way to check for expected output. Status text is not captured, except for the current status line that a unit test can examine.

Obtain a test printer instance with NewTestPrinter().

Additional methods of a test printer are:

  • GetLines() returns a string array of printed lines
  • String() returns the line-parsed input as a single string
  • GetStatusText() returns the current status text

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type StringPrinter added in v1.0.12

type StringPrinter struct {
	TestPrinter
}

func NewStringPrinter added in v1.0.12

func NewStringPrinter() *StringPrinter

func (*StringPrinter) BeginPrint added in v1.0.12

func (dp *StringPrinter) BeginPrint(args ...interface{})

func (*StringPrinter) ChattyStatus added in v1.0.12

func (dp *StringPrinter) ChattyStatus(args ...interface{})

func (*StringPrinter) ChattyStatusf added in v1.0.12

func (dp *StringPrinter) ChattyStatusf(format string, args ...interface{})

func (*StringPrinter) Clear added in v1.0.12

func (dp *StringPrinter) Clear()

func (*StringPrinter) ContinuePrint added in v1.0.12

func (dp *StringPrinter) ContinuePrint(args ...interface{})

func (*StringPrinter) ContinuePrintf added in v1.0.12

func (dp *StringPrinter) ContinuePrintf(format string, args ...interface{})

func (*StringPrinter) Count added in v1.0.12

func (dp *StringPrinter) Count()

func (*StringPrinter) DateRangeStatus added in v1.0.12

func (dp *StringPrinter) DateRangeStatus(from time.Time, to time.Time, args ...interface{})

func (*StringPrinter) EnableVerbose added in v1.0.12

func (dp *StringPrinter) EnableVerbose(enable bool)

func (*StringPrinter) EndPrint added in v1.0.12

func (dp *StringPrinter) EndPrint(args ...interface{})

func (*StringPrinter) EndPrintIfStarted added in v1.0.12

func (dp *StringPrinter) EndPrintIfStarted()

func (*StringPrinter) PauseStatus added in v1.0.12

func (dp *StringPrinter) PauseStatus()

func (*StringPrinter) Println added in v1.0.12

func (dp *StringPrinter) Println(args ...interface{})

func (*StringPrinter) Printlnf added in v1.0.12

func (dp *StringPrinter) Printlnf(format string, args ...interface{})

func (*StringPrinter) ResumeStatus added in v1.0.12

func (dp *StringPrinter) ResumeStatus()

func (*StringPrinter) SetCounterMax added in v1.0.12

func (dp *StringPrinter) SetCounterMax(max int, args ...interface{})

func (*StringPrinter) Status added in v1.0.12

func (dp *StringPrinter) Status(args ...interface{})

func (*StringPrinter) Statusf added in v1.0.12

func (dp *StringPrinter) Statusf(format string, args ...interface{})

func (*StringPrinter) Text added in v1.0.12

func (sp *StringPrinter) Text() string

func (*StringPrinter) UpdateCountStatus added in v1.0.12

func (dp *StringPrinter) UpdateCountStatus(args ...interface{})

func (*StringPrinter) VerbosePrintln added in v1.0.12

func (dp *StringPrinter) VerbosePrintln(args ...interface{})

func (*StringPrinter) VerbosePrintlnf added in v1.0.12

func (dp *StringPrinter) VerbosePrintlnf(format string, args ...interface{})

type TestPrinter added in v1.0.4

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

func NewTestPrinter added in v1.0.4

func NewTestPrinter() *TestPrinter

func (*TestPrinter) BeginPrint added in v1.0.4

func (dp *TestPrinter) BeginPrint(args ...interface{})

func (*TestPrinter) ChattyStatus added in v1.0.4

func (dp *TestPrinter) ChattyStatus(args ...interface{})

func (*TestPrinter) ChattyStatusf added in v1.0.4

func (dp *TestPrinter) ChattyStatusf(format string, args ...interface{})

func (*TestPrinter) Clear added in v1.0.4

func (dp *TestPrinter) Clear()

func (*TestPrinter) ContinuePrint added in v1.0.4

func (dp *TestPrinter) ContinuePrint(args ...interface{})

func (*TestPrinter) ContinuePrintf added in v1.0.4

func (dp *TestPrinter) ContinuePrintf(format string, args ...interface{})

func (*TestPrinter) Count added in v1.0.4

func (dp *TestPrinter) Count()

func (*TestPrinter) DateRangeStatus added in v1.0.4

func (dp *TestPrinter) DateRangeStatus(from time.Time, to time.Time, args ...interface{})

func (*TestPrinter) EnableVerbose added in v1.0.4

func (dp *TestPrinter) EnableVerbose(enable bool)

func (*TestPrinter) EndPrint added in v1.0.4

func (dp *TestPrinter) EndPrint(args ...interface{})

func (*TestPrinter) EndPrintIfStarted added in v1.0.4

func (dp *TestPrinter) EndPrintIfStarted()

func (*TestPrinter) GetLines added in v1.0.4

func (tp *TestPrinter) GetLines() []string

func (*TestPrinter) GetStatusText added in v1.0.4

func (tp *TestPrinter) GetStatusText() string

func (*TestPrinter) PauseStatus added in v1.0.4

func (dp *TestPrinter) PauseStatus()

func (*TestPrinter) Println added in v1.0.4

func (dp *TestPrinter) Println(args ...interface{})

func (*TestPrinter) Printlnf added in v1.0.4

func (dp *TestPrinter) Printlnf(format string, args ...interface{})

func (*TestPrinter) ResumeStatus added in v1.0.4

func (dp *TestPrinter) ResumeStatus()

func (*TestPrinter) SetCounterMax added in v1.0.4

func (dp *TestPrinter) SetCounterMax(max int, args ...interface{})

func (*TestPrinter) Status added in v1.0.4

func (dp *TestPrinter) Status(args ...interface{})

func (*TestPrinter) Statusf added in v1.0.4

func (dp *TestPrinter) Statusf(format string, args ...interface{})

func (*TestPrinter) String added in v1.0.9

func (tp *TestPrinter) String() string

func (*TestPrinter) UpdateCountStatus added in v1.0.4

func (dp *TestPrinter) UpdateCountStatus(args ...interface{})

func (*TestPrinter) VerbosePrintln added in v1.0.4

func (dp *TestPrinter) VerbosePrintln(args ...interface{})

func (*TestPrinter) VerbosePrintlnf added in v1.0.4

func (dp *TestPrinter) VerbosePrintlnf(format string, args ...interface{})

type ToolPrinter

type ToolPrinter interface {
	Status(text ...interface{})
	Statusf(format string, args ...interface{})
	Clear()
	ChattyStatus(text ...interface{})
	ChattyStatusf(format string, args ...interface{})
	SetCounterMax(max int, text ...interface{})
	UpdateCountStatus(extraStatusText ...interface{})
	Count()
	PauseStatus()
	ResumeStatus()
	Println(text ...interface{})
	Printlnf(format string, args ...interface{})
	BeginPrint(text ...interface{})
	ContinuePrint(text ...interface{})
	ContinuePrintf(format string, args ...interface{})
	EndPrint(text ...interface{})
	EndPrintIfStarted()
	DateRangeStatus(from time.Time, to time.Time, purpose ...interface{})
	VerbosePrintln(text ...interface{})
	VerbosePrintlnf(format string, args ...interface{})
	EnableVerbose(enabled bool)
}

func NewToolPrinter

func NewToolPrinter() ToolPrinter

Jump to

Keyboard shortcuts

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