-
Notifications
You must be signed in to change notification settings - Fork 9
/
colors.go
103 lines (89 loc) · 3.06 KB
/
colors.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
// Copyright 2022 Namespace Labs Inc; All rights reserved.
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
package colors
import (
"context"
"github.com/morikuni/aec"
)
type contextKey string
var (
_styleKey = contextKey("fn.colors")
)
type Style struct {
Header Applicable // Timestamps, etc.
Comment Applicable // Faded out, at the end of a line.
CommentHighlight Applicable
Progress Applicable // Inline content that is updated w/ progress.
ErrorHeader Applicable // Error prefixes.
LessRelevant Applicable // A passing detail, like the fact that a list is empty.
Highlight Applicable // Something we should highlight, usually in bold.
ErrorWhat Applicable // When formatting errors, the kind of error.
LogError Applicable // An error displayed in a log line.
LogCategory Applicable // Log line category.
LogCachedName Applicable // Names, when cached.
LogArgument Applicable // An argument to an invocation.
LogResult Applicable // The result of an invocation.
LogErrorReason Applicable // An expected error reason.
LogScope Applicable // Highlight of a package for which an invocation is being made.
TestSuccess Applicable // It says it on the tin.
TestFailure Applicable // Here too.
}
func WithStyle(ctx context.Context, s Style) context.Context {
return context.WithValue(ctx, _styleKey, &s)
}
func Ctx(ctx context.Context) Style {
if style := ctx.Value(_styleKey); style != nil {
return *style.(*Style)
}
return NoColors
}
type Applicable interface {
Apply(string) string
}
var WithColors = Style{
Header: aec.LightBlackF,
Comment: aec.LightBlackF,
CommentHighlight: aec.LightBlackF.With(aec.Bold),
LogCategory: aec.LightBlueF,
LogCachedName: aec.LightBlackF,
Progress: aec.LightBlackF,
LogArgument: aec.CyanF,
LogResult: aec.BlueF,
LogErrorReason: aec.BlueF,
LogError: aec.RedF,
LogScope: aec.Italic,
ErrorHeader: aec.RedF.With(aec.Bold),
LessRelevant: aec.Italic,
Highlight: aec.Bold,
ErrorWhat: aec.MagentaF,
TestSuccess: aec.GreenF,
TestFailure: aec.RedF,
}
var NoColors = Style{
Header: noOpANSI,
Comment: noOpANSI,
CommentHighlight: noOpANSI,
LogCategory: noOpANSI,
LogCachedName: noOpANSI,
Progress: noOpANSI,
LogArgument: noOpANSI,
LogResult: noOpANSI,
LogErrorReason: noOpANSI,
LogError: noOpANSI,
LogScope: noOpANSI,
ErrorHeader: noOpANSI,
LessRelevant: noOpANSI,
Highlight: noOpANSI,
ErrorWhat: noOpANSI,
TestSuccess: noOpANSI,
TestFailure: noOpANSI,
}
// An implementation of aec.ANSI that does completely nothing.
// It is more appropriate to use it in for non-TTY output since
// [aec.EmptyBuilder.ANSI] inserts reset codes "ESC[0m" regardless.
type noOpANSIImpl struct{}
func (noOpANSIImpl) Apply(s string) string {
return s
}
var noOpANSI = noOpANSIImpl{}