termdash/widgets/segmentdisplay/options.go

113 lines
3.3 KiB
Go
Raw Normal View History

2019-02-04 12:57:32 +08:00
// 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
2019-02-06 13:29:14 +08:00
import (
"fmt"
"github.com/mum4k/termdash/align"
)
2019-02-04 12:57:32 +08:00
// 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
2019-02-06 13:29:14 +08:00
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
2019-02-04 12:57:32 +08:00
}
// newOptions returns options with the default values set.
func newOptions() *options {
return &options{
2019-02-06 13:29:14 +08:00
hAlign: align.HorizontalCenter,
vAlign: align.VerticalMiddle,
gapPercent: DefaultGapPercent,
2019-02-04 12:57:32 +08:00
}
}
// 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 {
2019-02-04 12:57:32 +08:00
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 {
2019-02-04 12:57:32 +08:00
return option(func(opts *options) {
opts.maximizeSegSize = false
})
}
2019-02-05 13:17:25 +08:00
2019-02-06 13:29:14 +08:00
// 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
})
}