-
Notifications
You must be signed in to change notification settings - Fork 138
/
options.go
147 lines (126 loc) · 4.32 KB
/
options.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
// 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.
package barchart
// options.go contains configurable options for BarChart.
import (
"github.com/mum4k/termdash/cell"
"github.com/mum4k/termdash/draw"
)
// 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 {
barChar rune
barWidth int
barGap int
showValues bool
barColors []cell.Color
labelColors []cell.Color
valueColors []cell.Color
labels []string
}
// newOptions returns options with the default values set.
func newOptions() *options {
return &options{
barChar: DefaultChar,
barGap: DefaultBarGap,
}
}
// DefaultChar is the default value for the Char option.
const DefaultChar = draw.DefaultRectChar
// Char sets the rune that is used when drawing the rectangle representing the
// bars.
func Char(ch rune) Option {
return option(func(opts *options) {
opts.barChar = ch
})
}
// BarWidth sets the width of the bars. If not set, the bars use all the space
// available to the widget.
func BarWidth(width int) Option {
return option(func(opts *options) {
opts.barWidth = width
})
}
// DefaultBarGap is the default value for the BarGap option.
const DefaultBarGap = 1
// BarGap sets the width of the space between the bars.
// Defaults to DefaultBarGap.
func BarGap(width int) Option {
return option(func(opts *options) {
opts.barGap = width
})
}
// ShowValues tells the bar chart to display the actual values inside each of the bars.
func ShowValues() Option {
return option(func(opts *options) {
opts.showValues = true
})
}
// DefaultBarColor is the default color of a bar, unless specified otherwise
// via the BarColors option.
const DefaultBarColor = cell.ColorRed
// BarColors sets the colors of each of the bars.
// Bars are created on a call to Values(), each value ends up in its own Bar.
// The first supplied color applies to the bar displaying the first value.
// Any bars that don't have a color specified use the DefaultBarColor.
func BarColors(colors []cell.Color) Option {
return option(func(opts *options) {
opts.barColors = colors
})
}
// DefaultLabelColor is the default color of a bar label, unless specified
// otherwise via the LabelColors option.
const DefaultLabelColor = cell.ColorGreen
// LabelColors sets the colors of each of the labels under the bars.
// Bars are created on a call to Values(), each value ends up in its own Bar.
// The first supplied color applies to the label of the bar displaying the
// first value. Any labels that don't have a color specified use the
// DefaultLabelColor.
func LabelColors(colors []cell.Color) Option {
return option(func(opts *options) {
opts.labelColors = colors
})
}
// Labels sets the labels displayed under each bar,
// Bars are created on a call to Values(), each value ends up in its own Bar.
// The first supplied label applies to the bar displaying the first value.
// If not specified, the corresponding bar (or all the bars) don't have a
// label.
func Labels(labels []string) Option {
return option(func(opts *options) {
opts.labels = labels
})
}
// DefaultValueColor is the default color of a bar value, unless specified
// otherwise via the ValueColors option.
const DefaultValueColor = cell.ColorYellow
// ValueColors sets the colors of each of the values in the bars. Bars are
// created on a call to Values(), each value ends up in its own Bar. The first
// supplied color applies to the bar displaying the first value. Any values
// that don't have a color specified use the DefaultValueColor.
func ValueColors(colors []cell.Color) Option {
return option(func(opts *options) {
opts.valueColors = colors
})
}