message

package
v0.5.0 Latest Latest
Warning

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

Go to latest
Published: Jan 10, 2025 License: Apache-2.0 Imports: 12 Imported by: 0

Documentation

Overview

Package message contains functions to print messages to the screen

Package message contains functions to print messages to the screen

Package message provides a rich set of functions for displaying messages to the user.

Package message provides a rich set of functions for displaying messages to the user.

Index

Constants

This section is empty.

Variables

View Source
var NewProgressSpinner = func(format string, a ...any) helpers.ProgressWriter {
	if activeSpinner != nil {
		activeSpinner.Updatef(format, a...)
		debugPrinter(2, "Active spinner already exists")
		return activeSpinner
	}

	var spinner *pterm.SpinnerPrinter
	if NoProgress {
		infof(format, a...)
	} else {
		text := pterm.Sprintf(format, a...)
		spinner, _ = pterm.DefaultSpinner.
			WithRemoveWhenDone(false).
			WithSequence(sequence...).
			Start(text)
	}

	activeSpinner = &Spinner{
		spinner:   spinner,
		termWidth: pterm.GetTerminalWidth(),
	}

	return activeSpinner
}

NewProgressSpinner creates a new progress spinner.

View Source
var NoProgress bool

NoProgress sets whether the default spinners and progress bars should use fancy animations

View Source
var (
	// SLog sets the default structured log handler for messages
	SLog = slog.New(MaruHandler{})
)

Functions

func Fatalf

func Fatalf(err any, format string, a ...any)

Fatalf prints a fatal error message and exits with a 1 with a given format.

func LogFileLocation

func LogFileLocation() string

LogFileLocation returns the location of the log file.

func SetLogLevel

func SetLogLevel(lvl LogLevel)

SetLogLevel sets the log level.

func UseLogFile

func UseLogFile(dir string) (io.Writer, error)

UseLogFile writes output to stderr and a logFile.

Types

type LogLevel

type LogLevel int

LogLevel is the level of logging to display.

const (

	// TraceLevel level. Effectively the same as Debug but with line numbers.
	//
	// NOTE: There currently is no Trace() function in the log/slog package. In
	// order to use this level, you must use message.SLog.Log() and specify the
	// level. Maru currently uses the Trace level specifically for adding line
	// numbers to logs from calls to message.SLog.Debug(). Because of this,
	// Trace is effectively the same as Debug but with line numbers.
	TraceLevel LogLevel = -8
	// DebugLevel level. Usually only enabled when debugging. Very verbose logging.
	DebugLevel LogLevel = -4
	// InfoLevel level. General operational entries about what's going on inside the
	// application.
	InfoLevel LogLevel = 0
	// WarnLevel level. Non-critical entries that deserve eyes.
	WarnLevel LogLevel = 4
	// ErrorLevel level. Errors only.
	ErrorLevel LogLevel = 8
)

func GetLogLevel

func GetLogLevel() LogLevel

GetLogLevel returns the current log level.

type MaruHandler

type MaruHandler struct{}

MaruHandler is a simple handler that implements the slog.Handler interface

func (MaruHandler) Enabled

func (z MaruHandler) Enabled(_ context.Context, level slog.Level) bool

Enabled determines if the handler is enabled for the given level. This function is called for every log message and will compare the level of the message to the log level set (default is info). Log levels are defined in src/message/logging.go and match the levels used in the underlying log/slog package. Logs with a level below the set log level will be ignored.

Examples:

SetLogLevel(TraceLevel) // show everything, with file names and line numbers
SetLogLevel(DebugLevel) // show everything
SetLogLevel(InfoLevel)  // show info and above (does not show debug logs)
SetLogLevel(WarnLevel)  // show warn and above (does not show debug/info logs)
SetLogLevel(ErrorLevel)  // show only errors (does not show debug/info/warn logs)

func (MaruHandler) Handle

func (z MaruHandler) Handle(_ context.Context, record slog.Record) error

Handle prints the respective logging function in Maru This function ignores any key pairs passed through the record

func (MaruHandler) WithAttrs

func (z MaruHandler) WithAttrs(_ []slog.Attr) slog.Handler

WithAttrs is not suppported

func (MaruHandler) WithGroup

func (z MaruHandler) WithGroup(_ string) slog.Handler

WithGroup is not supported

type Spinner

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

Spinner is a wrapper around pterm.SpinnerPrinter.

func (*Spinner) Close

func (p *Spinner) Close() error

Close stops the spinner.

func (*Spinner) Failf

func (p *Spinner) Failf(format string, a ...any)

Failf prints an error message with the spinner.

func (*Spinner) Successf

func (p *Spinner) Successf(format string, a ...any)

Successf prints a success message with the spinner and stops it.

func (*Spinner) Updatef

func (p *Spinner) Updatef(format string, a ...any)

Updatef updates the spinner text.

func (*Spinner) Write

func (p *Spinner) Write(raw []byte) (int, error)

Write the given text to the spinner.

Jump to

Keyboard shortcuts

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