2018-06-23 04:30:50 +08:00
|
|
|
// Copyright 2018 Google Inc.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
2018-06-22 05:04:19 +08:00
|
|
|
|
2018-06-23 04:30:50 +08:00
|
|
|
package sparkline
|
2018-06-22 05:04:19 +08:00
|
|
|
|
|
|
|
// options.go contains configurable options for SparkLine.
|
|
|
|
|
2019-02-15 13:05:45 +08:00
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"github.com/mum4k/termdash/cell"
|
|
|
|
)
|
2018-06-23 04:30:50 +08:00
|
|
|
|
2018-06-22 05:04:19 +08:00
|
|
|
// Option is used to provide options.
|
|
|
|
type Option interface {
|
|
|
|
// set sets the provided option.
|
|
|
|
set(*options)
|
|
|
|
}
|
|
|
|
|
|
|
|
// option implements Option.
|
|
|
|
type option func(*options)
|
|
|
|
|
|
|
|
// set implements Option.set.
|
|
|
|
func (o option) set(opts *options) {
|
|
|
|
o(opts)
|
|
|
|
}
|
|
|
|
|
|
|
|
// options holds the provided options.
|
|
|
|
type options struct {
|
|
|
|
label string
|
|
|
|
labelCellOpts []cell.Option
|
|
|
|
height int
|
|
|
|
color cell.Color
|
|
|
|
}
|
|
|
|
|
|
|
|
// newOptions returns options with the default values set.
|
|
|
|
func newOptions() *options {
|
2018-06-22 21:40:41 +08:00
|
|
|
return &options{
|
|
|
|
color: DefaultColor,
|
|
|
|
}
|
2018-06-22 05:04:19 +08:00
|
|
|
}
|
|
|
|
|
2019-02-15 13:05:45 +08:00
|
|
|
// validate validates the provided options.
|
|
|
|
func (o *options) validate() error {
|
|
|
|
if got, min := o.height, 0; got < min {
|
|
|
|
return fmt.Errorf("invalid Height %d, must be %d <= Height", got, min)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-06-22 05:04:19 +08:00
|
|
|
// Label adds a label above the SparkLine.
|
|
|
|
func Label(text string, cOpts ...cell.Option) Option {
|
|
|
|
return option(func(opts *options) {
|
|
|
|
opts.label = text
|
|
|
|
opts.labelCellOpts = cOpts
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// Height sets a fixed height for the SparkLine.
|
2019-02-15 13:05:45 +08:00
|
|
|
// If not provided or set to zero, the SparkLine takes all the available
|
|
|
|
// vertical space in the container. Must be a positive or zero integer.
|
2018-06-22 05:04:19 +08:00
|
|
|
func Height(h int) Option {
|
|
|
|
return option(func(opts *options) {
|
|
|
|
opts.height = h
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// DefaultColor is the default value for the Color option.
|
2018-07-03 10:28:17 +08:00
|
|
|
const DefaultColor = cell.ColorGreen
|
2018-06-22 05:04:19 +08:00
|
|
|
|
|
|
|
// Color sets the color of the SparkLine.
|
|
|
|
// Defaults to DefaultColor if not set.
|
|
|
|
func Color(c cell.Color) Option {
|
|
|
|
return option(func(opts *options) {
|
|
|
|
opts.color = c
|
|
|
|
})
|
|
|
|
}
|