From eac59fd5a6e43a685e630f6304bb3ac72ed653aa Mon Sep 17 00:00:00 2001 From: Kyle Isom Date: Thu, 20 Nov 2025 18:20:00 -0800 Subject: [PATCH] msg: add new package for CLI output. --- README.md | 1 + lib/lib.go | 4 ++ msg/msg.go | 127 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 132 insertions(+) create mode 100644 msg/msg.go diff --git a/README.md b/README.md index fa5f18e..808a37f 100644 --- a/README.md +++ b/README.md @@ -84,6 +84,7 @@ Contents: lib/ Commonly-useful functions for writing Go programs. log/ A syslog library. logging/ A logging library. + msg/ Output library for command line programs. mwc/ MultiwriteCloser implementation. sbuf/ A byte buffer that can be wiped. seekbuf/ A read-seekable byte buffer. diff --git a/lib/lib.go b/lib/lib.go index 34a6bf5..57e8b0c 100644 --- a/lib/lib.go +++ b/lib/lib.go @@ -336,6 +336,10 @@ type DummyWriteCloser struct { w io.Writer } +func WithCloser(w io.Writer) io.WriteCloser { + return &DummyWriteCloser{w: w} +} + func (dwc *DummyWriteCloser) Write(p []byte) (int, error) { return dwc.w.Write(p) } diff --git a/msg/msg.go b/msg/msg.go new file mode 100644 index 0000000..41eef08 --- /dev/null +++ b/msg/msg.go @@ -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)) +}