msg: add new package for CLI output.
This commit is contained in:
@@ -84,6 +84,7 @@ Contents:
|
|||||||
lib/ Commonly-useful functions for writing Go programs.
|
lib/ Commonly-useful functions for writing Go programs.
|
||||||
log/ A syslog library.
|
log/ A syslog library.
|
||||||
logging/ A logging library.
|
logging/ A logging library.
|
||||||
|
msg/ Output library for command line programs.
|
||||||
mwc/ MultiwriteCloser implementation.
|
mwc/ MultiwriteCloser implementation.
|
||||||
sbuf/ A byte buffer that can be wiped.
|
sbuf/ A byte buffer that can be wiped.
|
||||||
seekbuf/ A read-seekable byte buffer.
|
seekbuf/ A read-seekable byte buffer.
|
||||||
|
|||||||
@@ -336,6 +336,10 @@ type DummyWriteCloser struct {
|
|||||||
w io.Writer
|
w io.Writer
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func WithCloser(w io.Writer) io.WriteCloser {
|
||||||
|
return &DummyWriteCloser{w: w}
|
||||||
|
}
|
||||||
|
|
||||||
func (dwc *DummyWriteCloser) Write(p []byte) (int, error) {
|
func (dwc *DummyWriteCloser) Write(p []byte) (int, error) {
|
||||||
return dwc.w.Write(p)
|
return dwc.w.Write(p)
|
||||||
}
|
}
|
||||||
|
|||||||
127
msg/msg.go
Normal file
127
msg/msg.go
Normal file
@@ -0,0 +1,127 @@
|
|||||||
|
// Package msg is a tool for handling commandline output based on
|
||||||
|
// flags for quiet, verbose, and debug modes. The default is to
|
||||||
|
// have all modes disabled.
|
||||||
|
//
|
||||||
|
// The QPrint messages will only output messages if quiet mode is
|
||||||
|
// disabled
|
||||||
|
// The VPrint messages will only output messages if verbose mode
|
||||||
|
// is enabled.
|
||||||
|
// The DPrint messages will only output messages if debug mode
|
||||||
|
// is enabled.
|
||||||
|
package msg
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"io"
|
||||||
|
|
||||||
|
"git.wntrmute.dev/kyle/goutils/lib"
|
||||||
|
|
||||||
|
"git.wntrmute.dev/kyle/goutils/dbg"
|
||||||
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
enableQuiet bool
|
||||||
|
enableVerbose bool
|
||||||
|
debug *dbg.DebugPrinter
|
||||||
|
w io.Writer
|
||||||
|
)
|
||||||
|
|
||||||
|
func SetQuiet(q bool) {
|
||||||
|
enableQuiet = q
|
||||||
|
}
|
||||||
|
|
||||||
|
func SetVerbose(v bool) {
|
||||||
|
enableVerbose = v
|
||||||
|
}
|
||||||
|
|
||||||
|
func SetDebug(d bool) {
|
||||||
|
debug.Enabled = d
|
||||||
|
}
|
||||||
|
|
||||||
|
func Set(q, v, d bool) {
|
||||||
|
SetQuiet(q)
|
||||||
|
SetVerbose(v)
|
||||||
|
SetDebug(d)
|
||||||
|
}
|
||||||
|
|
||||||
|
func QPrint(a ...any) {
|
||||||
|
if enableQuiet {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
fmt.Fprint(w, a...)
|
||||||
|
}
|
||||||
|
|
||||||
|
func QPrintf(format string, a ...any) {
|
||||||
|
if enableQuiet {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
fmt.Fprintf(w, format, a...)
|
||||||
|
}
|
||||||
|
|
||||||
|
func QPrintln(a ...any) {
|
||||||
|
if enableQuiet {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
fmt.Fprintln(w, a...)
|
||||||
|
}
|
||||||
|
|
||||||
|
func DPrint(a ...any) {
|
||||||
|
debug.Print(a...)
|
||||||
|
}
|
||||||
|
|
||||||
|
func DPrintf(format string, a ...any) {
|
||||||
|
debug.Printf(format, a...)
|
||||||
|
}
|
||||||
|
|
||||||
|
func DPrintln(a ...any) {
|
||||||
|
debug.Println(a...)
|
||||||
|
}
|
||||||
|
|
||||||
|
func StackTrace() {
|
||||||
|
debug.StackTrace()
|
||||||
|
}
|
||||||
|
|
||||||
|
func VPrint(a ...any) {
|
||||||
|
if !enableVerbose {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
fmt.Fprint(w, a...)
|
||||||
|
}
|
||||||
|
|
||||||
|
func VPrintf(format string, a ...any) {
|
||||||
|
if !enableVerbose {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
fmt.Fprintf(w, format, a...)
|
||||||
|
}
|
||||||
|
|
||||||
|
func VPrintln(a ...any) {
|
||||||
|
if !enableVerbose {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
fmt.Fprintln(w, a...)
|
||||||
|
}
|
||||||
|
|
||||||
|
func Print(a ...any) {
|
||||||
|
fmt.Fprint(w, a...)
|
||||||
|
}
|
||||||
|
|
||||||
|
func Printf(format string, a ...any) {
|
||||||
|
fmt.Fprintf(w, format, a...)
|
||||||
|
}
|
||||||
|
|
||||||
|
func Println(a ...any) {
|
||||||
|
fmt.Fprintln(w, a...)
|
||||||
|
}
|
||||||
|
|
||||||
|
// SetWriter changes the output for messages.
|
||||||
|
func SetWriter(dst io.Writer) {
|
||||||
|
w = dst
|
||||||
|
debug = dbg.To(lib.WithCloser(w))
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user