forked from mum4k/termdash
-
Notifications
You must be signed in to change notification settings - Fork 0
/
options.go
112 lines (96 loc) · 3.26 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
// Copyright 2019 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 segmentdisplay
import (
"fmt"
"github.com/mum4k/termdash/align"
)
// options.go contains configurable options for SegmentDisplay.
// 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 {
hAlign align.Horizontal
vAlign align.Vertical
maximizeSegSize bool
gapPercent int
}
// validate validates the provided options.
func (o *options) validate() error {
if min, max := 0, 100; o.gapPercent < min || o.gapPercent > max {
return fmt.Errorf("invalid GapPercent %d, must be %d <= value <= %d", o.gapPercent, min, max)
}
return nil
}
// newOptions returns options with the default values set.
func newOptions() *options {
return &options{
hAlign: align.HorizontalCenter,
vAlign: align.VerticalMiddle,
gapPercent: DefaultGapPercent,
}
}
// AlignHorizontal sets the horizontal alignment for the individual display
// segments. Defaults to alignment in the center.
func AlignHorizontal(h align.Horizontal) Option {
return option(func(opts *options) {
opts.hAlign = h
})
}
// AlignVertical sets the vertical alignment for the individual display
// segments. Defaults to alignment in the middle
func AlignVertical(v align.Vertical) Option {
return option(func(opts *options) {
opts.vAlign = v
})
}
// MaximizeSegmentHeight tells the widget to maximize the height of the
// individual display segments.
// When this option is set and the user has provided more text than we can fit
// on the canvas, the widget will prefer to maximize height of individual
// characters which will result in earlier trimming of the text.
func MaximizeSegmentHeight() Option {
return option(func(opts *options) {
opts.maximizeSegSize = true
})
}
// MaximizeDisplayedText tells the widget to maximize the amount of characters
// that are displayed.
// When this option is set and the user has provided more text than we can fit
// on the canvas, the widget will prefer to decrease the height of individual
// characters and fit more of them on the canvas.
// This is the default behavior.
func MaximizeDisplayedText() Option {
return option(func(opts *options) {
opts.maximizeSegSize = false
})
}
// DefaultGapPercent is the default value for the GapPercent option.
const DefaultGapPercent = 20
// GapPercent sets the size of the horizontal gap between individual segments
// (characters) expressed as a percentage of the segment height.
func GapPercent(perc int) Option {
return option(func(opts *options) {
opts.gapPercent = perc
})
}