Skip to content

Commit

Permalink
fix: make colorized prefix logger standard
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexanderArvidsson committed Apr 29, 2024
1 parent ed15628 commit 983262c
Show file tree
Hide file tree
Showing 8 changed files with 14 additions and 77 deletions.
7 changes: 0 additions & 7 deletions internal/flags/flags.go
Expand Up @@ -97,7 +97,6 @@ func init() {
pflag.StringVar(&Output.Group.Begin, "output-group-begin", "", "Message template to print before a task's grouped output.")
pflag.StringVar(&Output.Group.End, "output-group-end", "", "Message template to print after a task's grouped output.")
pflag.BoolVar(&Output.Group.ErrorOnly, "output-group-error-only", false, "Swallow output from successful tasks.")
pflag.BoolVar(&Output.Prefix.Color, "output-prefix-color", false, "Use colors for task prefixes.")
pflag.BoolVarP(&Color, "color", "c", true, "Colored output. Enabled by default. Set flag to false or use NO_COLOR=1 to disable.")
pflag.IntVarP(&Concurrency, "concurrency", "C", 0, "Limit number tasks to run concurrently.")
pflag.DurationVarP(&Interval, "interval", "I", 0, "Interval to watch for changes.")
Expand Down Expand Up @@ -144,11 +143,5 @@ func Validate() error {
}
}

if Output.Name != "prefixed" {
if Output.Prefix.Color {
return errors.New("task: You can't set --output-prefix-color without --output=prefixed")
}
}

return nil
}
2 changes: 1 addition & 1 deletion internal/output/output.go
Expand Up @@ -33,7 +33,7 @@ func BuildFor(o *ast.Output, logger *logger.Logger) (Output, error) {
if err := checkOutputGroupUnset(o); err != nil {
return nil, err
}
return NewPrefixed(logger, o.Prefix.Color), nil
return NewPrefixed(logger), nil
default:
return nil, fmt.Errorf(`task: output style %q not recognized`, o.Name)
}
Expand Down
4 changes: 2 additions & 2 deletions internal/output/output_test.go
Expand Up @@ -113,7 +113,7 @@ func TestPrefixed(t *testing.T) {
Color: false,
}

var o output.Output = output.NewPrefixed(l, false)
var o output.Output = output.NewPrefixed(l)
w, _, cleanup := o.WrapWriter(&b, io.Discard, "prefix", nil)

t.Run("simple use cases", func(t *testing.T) {
Expand Down Expand Up @@ -147,7 +147,7 @@ func TestPrefixedWithColor(t *testing.T) {
Color: true,
}

var o output.Output = output.NewPrefixed(l, true)
var o output.Output = output.NewPrefixed(l)

writers := make([]io.Writer, 16)
for i := range writers {
Expand Down
14 changes: 3 additions & 11 deletions internal/output/prefixed.go
Expand Up @@ -14,17 +14,15 @@ type Prefixed struct {
logger *logger.Logger
seen map[string]uint
counter *uint
color bool
}

func NewPrefixed(logger *logger.Logger, color bool) Prefixed {
func NewPrefixed(logger *logger.Logger) Prefixed {
var counter uint

return Prefixed{
seen: make(map[string]uint),
counter: &counter,
logger: logger,
color: color,
}
}

Expand Down Expand Up @@ -100,14 +98,8 @@ func (pw *prefixWriter) writeLine(line string) error {
return nil
}

if pw.prefixed.color {
color := PrefixColorSequence[idx%uint(len(PrefixColorSequence))]
pw.prefixed.logger.FOutf(pw.writer, color, pw.prefix)
} else {
if _, err := fmt.Fprint(pw.writer, pw.prefix); err != nil {
return nil
}
}
color := PrefixColorSequence[idx%uint(len(PrefixColorSequence))]
pw.prefixed.logger.FOutf(pw.writer, color, pw.prefix)

if _, err := fmt.Fprint(pw.writer, "] "); err != nil {
return nil
Expand Down
37 changes: 8 additions & 29 deletions taskfile/ast/output.go
Expand Up @@ -12,8 +12,6 @@ type Output struct {
Name string `yaml:"-"`
// Group specific style
Group OutputGroup
// Prefix specific style
Prefix OutputPrefix
}

// IsSet returns true if and only if a custom output style is set.
Expand All @@ -34,33 +32,19 @@ func (s *Output) UnmarshalYAML(node *yaml.Node) error {

case yaml.MappingNode:
var tmp struct {
Group *OutputGroup
Prefixed *OutputPrefix
Group *OutputGroup
}

if err := node.Decode(&tmp); err != nil {
return fmt.Errorf("task: output style must be a string or mapping with a \"group\" or \"prefixed\" key: %w", err)
return fmt.Errorf("task: output style must be a string or mapping with a \"group\" key: %w", err)
}

if tmp.Group != nil {
*s = Output{
Name: "group",
Group: *tmp.Group,
}

return nil
if tmp.Group == nil {
return fmt.Errorf("task: output style must have the \"group\" key when in mapping form")
}

if tmp.Prefixed != nil {
*s = Output{
Name: "prefixed",
Prefix: *tmp.Prefixed,
}

return nil
*s = Output{
Name: "group",
Group: *tmp.Group,
}

return fmt.Errorf("task: output style must have either \"group\" or \"prefixed\" key when in mapping form")
return nil
}

return fmt.Errorf("yaml: line %d: cannot unmarshal %s into output", node.Line, node.ShortTag())
Expand All @@ -79,8 +63,3 @@ func (g *OutputGroup) IsSet() bool {
}
return g.Begin != "" || g.End != ""
}

// OutputPrefix is the style options specific to the Prefix style.
type OutputPrefix struct {
Color bool `yaml:"color"`
}
1 change: 0 additions & 1 deletion website/docs/api_reference.mdx
Expand Up @@ -42,7 +42,6 @@ If `--` is given, all remaining arguments will be assigned to a special
| | `--output-group-begin` | `string` | | Message template to print before a task's grouped output. |
| | `--output-group-end` | `string` | | Message template to print after a task's grouped output. |
| | `--output-group-error-only` | `bool` | `false` | Swallow command output on zero exit code. |
| | `--output-prefix-color` | `bool` | `false` | Use colors for task prefixes. |
| `-p` | `--parallel` | `bool` | `false` | Executes tasks provided on command line in parallel. |
| `-s` | `--silent` | `bool` | `false` | Disables echoing. |
| `-y` | `--yes` | `bool` | `false` | Assume "yes" as answer to all prompts. |
Expand Down
16 changes: 0 additions & 16 deletions website/docs/usage.mdx
Expand Up @@ -1847,22 +1847,6 @@ $ task default
[print-baz] baz
```

When using the `prefix` output, you can optionally configure it to colorize each prefix.
The chosen color is deterministic for a given set of tasks, where the color will cycle
through a predefined sequence of colors. If more tasks are run than colors available, the
color will cycle back and repeat. This can be useful for visually distinguishing tasks in
the output when using the `prefix` output.
```yaml
version: '3'

output:
prefixed:
color: true

tasks:
# ...
```

:::tip

The `output` option can also be specified by the `--output` or `-o` flags.
Expand Down
10 changes: 0 additions & 10 deletions website/static/schema.json
Expand Up @@ -544,16 +544,6 @@
"default": false
}
}
},
"prefixed": {
"type": "object",
"properties": {
"color": {
"description": "Use colors for task prefixes.",
"type": "boolean",
"default": false
}
}
}
}
},
Expand Down

0 comments on commit 983262c

Please sign in to comment.