lazydocker/vendor/github.com/carlosms/asciigraph/options.go
Jesse Duffield ad9b62e95e Constrain asciigraph dependency to revision
Turns out if you don't constrain a dependency to a revision, it picks the latest version (which I'm guessing corresponds to a git tag)
This means if you're using a fork for the sake of an extra commit that you want, you need to point at that revision (or perhaps just specify branch="master")
Otherwise we'll end up pointing at an older commit
2019-05-18 20:08:33 +10:00

80 lines
1.8 KiB
Go

package asciigraph
import (
"strings"
)
// Option represents a configuration setting.
type Option interface {
apply(c *config)
}
// config holds various graph options
type config struct {
Width, Height int
Min, Max *float64
Offset int
Caption string
}
// An optionFunc applies an option.
type optionFunc func(*config)
// apply implements the Option interface.
func (of optionFunc) apply(c *config) { of(c) }
func configure(defaults config, options []Option) *config {
for _, o := range options {
o.apply(&defaults)
}
return &defaults
}
// Width sets the graphs width. By default, the width of the graph is
// determined by the number of data points. If the value given is a
// positive number, the data points are interpolated on the x axis.
// Values <= 0 reset the width to the default value.
func Width(w int) Option {
return optionFunc(func(c *config) {
if w > 0 {
c.Width = w
} else {
c.Width = 0
}
})
}
// Height sets the graphs height.
func Height(h int) Option {
return optionFunc(func(c *config) {
if h > 0 {
c.Height = h
} else {
c.Height = 0
}
})
}
// Min sets the graph's minimum value for the vertical axis. It will be ignored
// if the series contains a lower value.
func Min(min float64) Option {
return optionFunc(func(c *config) { c.Min = &min })
}
// Max sets the graph's maximum value for the vertical axis. It will be ignored
// if the series contains a bigger value.
func Max(max float64) Option {
return optionFunc(func(c *config) { c.Max = &max })
}
// Offset sets the graphs offset.
func Offset(o int) Option {
return optionFunc(func(c *config) { c.Offset = o })
}
// Caption sets the graphs caption.
func Caption(caption string) Option {
return optionFunc(func(c *config) {
c.Caption = strings.TrimSpace(caption)
})
}