Skip to content

Commit

Permalink
Remove pencil.NewFrom(*pencil.Options) function (#24)
Browse files Browse the repository at this point in the history
The `pencil.NewFrom(*pencil.Options)` [1] function was not necessary
because...

1. ...there was no way to get the actual options from a `pencil`
   instance.
2. ...new `pencil` instances with different options can be simply
   composed ny using the variadic parameter of the
   `pencil.New(pencil.Option...)` function.

Therefore the `pencil.NewFrom(*pencil.Options)` has been removed to
simply the package surface.

[1]: https://github.com/svengreb/nib/blob/2dac3395/pencil/pencil.go#L26-L27

Closes GH-23
  • Loading branch information
svengreb authored Oct 31, 2020
1 parent 2dac339 commit 175478e
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 20 deletions.
4 changes: 2 additions & 2 deletions inkpen/inkpen.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,13 @@ import (
// Inkpen composes pencil.Pencil to support colored output including automatic TTY and terminal color detection.
type Inkpen struct {
*pencil.Pencil
opts Options
opts *Options
}

// New creates and returns a new inkpen with default Options and pencil.Options.
func New(opts ...Option) *Inkpen {
opt := NewOptions(opts...)
p := pencil.NewFrom(opt.pencilOpts)
p := pencil.New(opt.pencilOpts...)
return &Inkpen{
Pencil: p,
opts: opt,
Expand Down
20 changes: 9 additions & 11 deletions inkpen/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,22 +31,24 @@ type IconColorFunc func(format string, args ...interface{}) string
type Options struct {
coloredIcons bool
iconColorFuncs map[nib.Verbosity]IconColorFunc
pencilOpts pencil.Options
pencilOpts []pencil.Option
}

// Option is a inkpen option.
type Option func(*Options)

// NewOptions creates new default inkpen Options and pencil.Options and merges them with the given list of Option.
// By default the output writer is DefaultWriter and colorization for verbosity level icons is enabled.
func NewOptions(opts ...Option) Options {
opt := Options{
func NewOptions(opts ...Option) *Options {
opt := &Options{
iconColorFuncs: getDefaultIconColorFuncs(),
coloredIcons: true,
pencilOpts: pencil.NewOptions(pencil.WithWriter(DefaultWriter)),
pencilOpts: []pencil.Option{
pencil.WithWriter(DefaultWriter),
},
}
for _, o := range opts {
o(&opt)
o(opt)
}
return opt
}
Expand All @@ -70,13 +72,9 @@ func WithIconColorFuncs(iconColorFuncs map[nib.Verbosity]IconColorFunc) Option {
}

// WithPencilOptions sets the given list of pencil.Option.
func WithPencilOptions(pencilOpts ...pencil.Option) Option {
func WithPencilOptions(opts ...pencil.Option) Option {
return func(o *Options) {
pOpts := o.pencilOpts
for _, po := range pencilOpts {
po(&pOpts)
}
o.pencilOpts = pOpts
o.pencilOpts = append(o.pencilOpts, opts...)
}
}

Expand Down
6 changes: 3 additions & 3 deletions pencil/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,16 +42,16 @@ type Option func(*Options)

// NewOptions creates new default Options and merges them with the given options.
// By default the verbosity level is DefaultVerbosity and icons are enabled with output to DefaultWriter.
func NewOptions(opts ...Option) Options {
opt := Options{
func NewOptions(opts ...Option) *Options {
opt := &Options{
Icons: GetDefaultIcons(),
Prefixes: []string{},
UseIcons: true,
Verbosity: DefaultVerbosity,
Writer: DefaultWriter,
}
for _, o := range opts {
o(&opt)
o(opt)
}
return opt
}
Expand Down
5 changes: 1 addition & 4 deletions pencil/pencil.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,12 @@ import (

// Pencil is a writer for human-facing CLI messages with support for custom prefixes and verbosity level icons.
type Pencil struct {
opts Options
opts *Options
}

// New creates new pencil with default Options and merges them with the given list of Option.
func New(opts ...Option) *Pencil { return &Pencil{opts: NewOptions(opts...)} }

// NewFrom creates new pencil from the given Options.
func NewFrom(opts Options) *Pencil { return &Pencil{opts: opts} }

// Compile compiles the format and arguments, ensuring a trailing newline, when the given verbosity level is enabled.
func (p *Pencil) Compile(v nib.Verbosity, format string, args ...interface{}) string {
if p.Enabled(v) {
Expand Down

0 comments on commit 175478e

Please sign in to comment.