| 1 |
// |
| 2 |
// Copyright (c) 2016-2022 The Aurora Authors. All rights reserved. |
| 3 |
// This program is free software. It comes without any warranty, |
| 4 |
// to the extent permitted by applicable law. You can redistribute |
| 5 |
// it and/or modify it under the terms of the Unlicense. See LICENSE |
| 6 |
// file for more details or see below. |
| 7 |
// |
| 8 |
|
| 9 |
// |
| 10 |
// This is free and unencumbered software released into the public domain. |
| 11 |
// |
| 12 |
// Anyone is free to copy, modify, publish, use, compile, sell, or |
| 13 |
// distribute this software, either in source code form or as a compiled |
| 14 |
// binary, for any purpose, commercial or non-commercial, and by any |
| 15 |
// means. |
| 16 |
// |
| 17 |
// In jurisdictions that recognize copyright laws, the author or authors |
| 18 |
// of this software dedicate any and all copyright interest in the |
| 19 |
// software to the public domain. We make this dedication for the benefit |
| 20 |
// of the public at large and to the detriment of our heirs and |
| 21 |
// successors. We intend this dedication to be an overt act of |
| 22 |
// relinquishment in perpetuity of all present and future rights to this |
| 23 |
// software under copyright law. |
| 24 |
// |
| 25 |
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
| 26 |
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
| 27 |
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. |
| 28 |
// IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR |
| 29 |
// OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, |
| 30 |
// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR |
| 31 |
// OTHER DEALINGS IN THE SOFTWARE. |
| 32 |
// |
| 33 |
// For more information, please refer to <http://unlicense.org/> |
| 34 |
// |
| 35 |
|
| 36 |
package aurora |
| 37 |
|
| 38 |
import "flag" |
| 39 |
|
| 40 |
// Config represents configurations of a colorizer. |
| 41 |
type Config struct { |
| 42 |
// Colors feature. Enable colors if true. |
| 43 |
Colors bool `json:"colors" yaml:"colors" toml:"colors" mapstructure:"colors"` |
| 44 |
// Hyperlinks feature. Enable hyperlinks if true. |
| 45 |
Hyperlinks bool `json:"hyperlinks" yaml:"hyperlinks" toml:"hyperlinks" mapstructure:"hyperlinks"` |
| 46 |
} |
| 47 |
|
| 48 |
// NewConfig returns new default Config. |
| 49 |
func NewConfig() (conf Config) { |
| 50 |
conf.Colors = true |
| 51 |
conf.Hyperlinks = true |
| 52 |
return |
| 53 |
} |
| 54 |
|
| 55 |
// AddFlags to given *flag.FlagSet. The prefix used as prefix for flags. |
| 56 |
// It may be used to parse commandline flags. For example |
| 57 |
// |
| 58 |
// var conf Config |
| 59 |
// conf.AddFlags(flag.CommandLine, "colors.") |
| 60 |
// flag.Parse() |
| 61 |
// |
| 62 |
// for a main package, and use with flags commandline flags, |
| 63 |
// |
| 64 |
// go run main.go -colors.colors -colors.hyperlinks |
| 65 |
// |
| 66 |
// to enable or disable features. A colorizer can be created, for example, |
| 67 |
// |
| 68 |
// var colorizer = New(conf.Options()...) |
| 69 |
func (c *Config) AddFlags(fset *flag.FlagSet, prefix string) { |
| 70 |
fset.BoolVar(&c.Colors, |
| 71 |
prefix+"colors", |
| 72 |
c.Colors, |
| 73 |
"enable colors") |
| 74 |
fset.BoolVar(&c.Hyperlinks, |
| 75 |
prefix+"hyperlinks", |
| 76 |
c.Hyperlinks, |
| 77 |
"enable hyperlinks") |
| 78 |
} |
| 79 |
|
| 80 |
// Apply given options for the Config. |
| 81 |
func (c *Config) Apply(opts ...Option) { |
| 82 |
for _, opt := range opts { |
| 83 |
opt(c) |
| 84 |
} |
| 85 |
} |
| 86 |
|
| 87 |
// Options by the Config. |
| 88 |
func (c *Config) Options() (opts []Option) { |
| 89 |
return []Option{ |
| 90 |
WithColors(c.Colors), |
| 91 |
WithHyperlinks(c.Hyperlinks), |
| 92 |
} |
| 93 |
} |
| 94 |
|
| 95 |
func (c *Config) colorConfig() (cc colorConfig) { |
| 96 |
if c.Colors { |
| 97 |
cc |= colorPin |
| 98 |
} |
| 99 |
if c.Hyperlinks { |
| 100 |
cc |= hyperlinksPin |
| 101 |
} |
| 102 |
return |
| 103 |
} |
| 104 |
|
| 105 |
// An Option function. |
| 106 |
type Option func(*Config) |
| 107 |
|
| 108 |
// WithColors is an Option that used to enable or disable colors. |
| 109 |
func WithColors(t bool) Option { |
| 110 |
return func(c *Config) { |
| 111 |
c.Colors = t |
| 112 |
} |
| 113 |
} |
| 114 |
|
| 115 |
// WithHyperlinks is an Option that used to enable or disable links. |
| 116 |
func WithHyperlinks(t bool) Option { |
| 117 |
return func(c *Config) { |
| 118 |
c.Hyperlinks = t |
| 119 |
} |
| 120 |
} |