percent

package
v0.7.20 Latest Latest
Warning

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

Go to latest
Published: Mar 24, 2026 License: MIT Imports: 2 Imported by: 0

Documentation

Overview

Package percent provides options for percent field rendering in clog.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Apply

func Apply(p *Percent, opts []Option)

Apply applies the given options to p.

func EffectiveMaximum added in v0.7.0

func EffectiveMaximum(p Percent) float64

EffectiveMaximum returns the per-field maximum if set, otherwise the global maximum.

func FormatFunc

func FormatFunc() func(float64) string

FormatFunc returns the current custom format function, or nil if using default.

func Maximum added in v0.7.0

func Maximum() float64

Maximum returns the current global percent maximum (default 1.0).

func Precision

func Precision() int

Precision returns the current decimal precision.

func Restore

func Restore(s Snapshot)

Restore resets the percent configuration to a previously saved Snapshot.

func ReverseGradient

func ReverseGradient() bool

ReverseGradient reports whether the gradient is inverted.

func SetFormatFunc

func SetFormatFunc(fn func(float64) string)

SetFormatFunc configures a custom format function for percent fields. When set to nil (the default), the built-in format is used.

func SetMaximum added in v0.7.0

func SetMaximum(m float64)

SetMaximum sets the percent maximum. The default is 1.0, meaning percent values are passed as fractions (e.g. 0.75 → "75%"). Set to 100 for 0–100 input (e.g. 75 → "75%").

func SetPrecision

func SetPrecision(n int)

SetPrecision sets the number of decimal places for percent display. For example, 0 = "75%", 1 = "75.0%". Defaults to 0.

func SetReverseGradient

func SetReverseGradient(rev bool)

SetReverseGradient reverses the gradient direction for percent fields. By default the gradient runs red (0%) → green (100%) - suitable for metrics where higher is better. Set reverse=true to flip it to green (0%) → red (100%) - suitable for metrics like CPU or disk usage where lower is better.

Types

type Option

type Option func(*Percent)

Option configures how a percent field is rendered.

func WithMaximum added in v0.7.0

func WithMaximum(m float64) Option

WithMaximum returns an Option that overrides the global percent maximum for this field. For example, use WithMaximum(100) when passing values in the 0–100 range instead of the default 0–1 range.

func WithReverseGradient

func WithReverseGradient() Option

WithReverseGradient returns an Option that flips the gradient direction for this field relative to the logger default. If the logger is using the normal gradient (red=0%, green=100%), the field renders inverted (green=0%, red=100%), and vice versa.

type Percent

type Percent = core.Percent

Percent is the value type for percent fields.

type Snapshot

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

Snapshot captures the current state of all percent configuration. Use Restore to reset the state in test cleanup.

func Save

func Save() Snapshot

Save captures the current percent configuration so it can be restored later with Restore. Typical usage in tests:

snap := percent.Save()
t.Cleanup(func() { percent.Restore(snap) })

Jump to

Keyboard shortcuts

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