Skip to content

Commit

Permalink
highlight filename and line num (#35)
Browse files Browse the repository at this point in the history
* highlight filename and line num

but also the following text in theline lose colors.

* fix: remove unnecessary followin new-line

* fix: use previousStyle to highlight following text of the filenamne

* generate

* fix test ouput

* generate

* use [ \t] instead of \s

* Revert "use [ \t] instead of \s"

This reverts commit 8e416a2.

* use [ \t] instead of \s

* use [^ \t] instead of \S

* fix test resource

* fix test resource
  • Loading branch information
kyoh86 authored Jun 3, 2021
1 parent 66aa79c commit f080aca
Show file tree
Hide file tree
Showing 7 changed files with 66 additions and 61 deletions.
1 change: 0 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ COMMIT := `git rev-parse HEAD`

gen:
go run github.com/rakyll/statik -src=./sample -dest editor/test -include='*.txt' -f
gofmt -w editor/test/output_test.go

lint: gen
golangci-lint run
Expand Down
56 changes: 30 additions & 26 deletions config/concat.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,19 +31,21 @@ func concatConfig(base, other *Config) *Config {
other = &Config{}
}
return &Config{
LabelType: concatLabelType(base.LabelType, other.LabelType),
BuildStyle: concatStyle(base.BuildStyle, other.BuildStyle),
StartStyle: concatStyle(base.StartStyle, other.StartStyle),
PassStyle: concatStyle(base.PassStyle, other.PassStyle),
FailStyle: concatStyle(base.FailStyle, other.FailStyle),
SkipStyle: concatStyle(base.SkipStyle, other.SkipStyle),
FileStyle: concatStyle(base.FileStyle, other.FileStyle),
LineStyle: concatStyle(base.LineStyle, other.LineStyle),
CoverThreshold: concatInt(base.CoverThreshold, other.CoverThreshold),
CoveredStyle: concatStyle(base.CoveredStyle, other.CoveredStyle),
UncoveredStyle: concatStyle(base.UncoveredStyle, other.UncoveredStyle),
Removals: append(base.Removals, other.Removals...),
LeaveTestPrefix: concatBool(base.LeaveTestPrefix, other.LeaveTestPrefix),
LabelType: concatLabelType(base.LabelType, other.LabelType),
BuildStyle: concatStyle(base.BuildStyle, other.BuildStyle),
StartStyle: concatStyle(base.StartStyle, other.StartStyle),
PassStyle: concatStyle(base.PassStyle, other.PassStyle),
FailStyle: concatStyle(base.FailStyle, other.FailStyle),
PassPackageStyle: concatStyle(base.PassPackageStyle, other.PassPackageStyle),
FailPackageStyle: concatStyle(base.FailPackageStyle, other.FailPackageStyle),
SkipStyle: concatStyle(base.SkipStyle, other.SkipStyle),
FileStyle: concatStyle(base.FileStyle, other.FileStyle),
LineStyle: concatStyle(base.LineStyle, other.LineStyle),
CoverThreshold: concatInt(base.CoverThreshold, other.CoverThreshold),
CoveredStyle: concatStyle(base.CoveredStyle, other.CoveredStyle),
UncoveredStyle: concatStyle(base.UncoveredStyle, other.UncoveredStyle),
Removals: append(base.Removals, other.Removals...),
LeaveTestPrefix: concatBool(base.LeaveTestPrefix, other.LeaveTestPrefix),
}
}

Expand All @@ -52,19 +54,21 @@ func actualConfig(config *Config) *Config {
config = &Config{}
}
return &Config{
LabelType: actualLabelType(config.LabelType),
BuildStyle: actualStyle(config.BuildStyle),
StartStyle: actualStyle(config.StartStyle),
PassStyle: actualStyle(config.PassStyle),
FailStyle: actualStyle(config.FailStyle),
SkipStyle: actualStyle(config.SkipStyle),
FileStyle: actualStyle(config.FileStyle),
LineStyle: actualStyle(config.LineStyle),
CoverThreshold: actualInt(config.CoverThreshold),
CoveredStyle: actualStyle(config.CoveredStyle),
UncoveredStyle: actualStyle(config.UncoveredStyle),
Removals: config.Removals,
LeaveTestPrefix: actualBool(config.LeaveTestPrefix),
LabelType: actualLabelType(config.LabelType),
BuildStyle: actualStyle(config.BuildStyle),
StartStyle: actualStyle(config.StartStyle),
PassStyle: actualStyle(config.PassStyle),
FailStyle: actualStyle(config.FailStyle),
PassPackageStyle: actualStyle(config.PassPackageStyle),
FailPackageStyle: actualStyle(config.FailPackageStyle),
SkipStyle: actualStyle(config.SkipStyle),
FileStyle: actualStyle(config.FileStyle),
LineStyle: actualStyle(config.LineStyle),
CoverThreshold: actualInt(config.CoverThreshold),
CoveredStyle: actualStyle(config.CoveredStyle),
UncoveredStyle: actualStyle(config.UncoveredStyle),
Removals: config.Removals,
LeaveTestPrefix: actualBool(config.LeaveTestPrefix),
}
}

Expand Down
22 changes: 16 additions & 6 deletions config/style.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,10 @@ type Style struct {
Background *Color `json:"background,omitempty" yaml:"background,omitempty"`
}

// Apply style To string
func (s *Style) Apply(str string) string {
// ANSI get the ANSI string
func (s *Style) ANSI() aec.ANSI {
if s == nil {
return str // when a prevLineStyle is not set, editor/test/test.go calls it in nil
}
if s.Hide != nil && *s.Hide {
return ""
return emptyColor // when a prevLineStyle is not set, editor/test/test.go calls it in nil
}

ansi := s.Background.B()
Expand All @@ -72,6 +69,19 @@ func (s *Style) Apply(str string) string {
ansi = ansi.With(style.ansi)
}
}
return ansi
}

// Apply style To string
func (s *Style) Apply(str string) string {
if s.Hide != nil && *s.Hide {
return ""
}

ansi := s.ANSI()
if ansi == emptyColor {
return str
}

if len(ansi.String()) == 0 {
return str
Expand Down
2 changes: 1 addition & 1 deletion editor/test/statik/statik.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 9 additions & 11 deletions editor/test/test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,16 +37,16 @@ var (
passtail = regexp.MustCompile(`(?m)^([ \t]*)--- PASS: Test.*`)
skiptail = regexp.MustCompile(`(?m)^([ \t]*)--- SKIP: Test.*`)
failtail = regexp.MustCompile(`(?m)^([ \t]*)--- FAIL: Test.*`)
passlonely = regexp.MustCompile(`(?m)^PASS$`)
faillonely = regexp.MustCompile(`(?m)^FAIL$`)
passlonely = regexp.MustCompile(`(?m)^PASS[ \t]*$`)
faillonely = regexp.MustCompile(`(?m)^FAIL[ \t]*$`)

okPath = regexp.MustCompile(`(?m)^ok[ \t]+([^ \t]+)[ \t]*(?:[\d\.]+\w+|\(cached\))?[ \t]*(?:[ \t]+(coverage:[ \t]+\d+\.\d+% of statements)[ \t]*)?(?:` + noTestPattern + `)?$`)
failPath = regexp.MustCompile(`(?m)^FAIL[ \t]+[^ \t]+[ \t]+(?:[\d\.]+\w+|\[build failed\])$`)
notestPath = regexp.MustCompile(`(?m)^\?[ \t]+[^ \t]+` + noTestPattern + `$`)

coverage = regexp.MustCompile(`(?m)^coverage: ((\d+)\.\d)+% of statements?$`)

filename = regexp.MustCompile(`(?m)([^[ \t]:]+\.go):(\d+)`)
filename = regexp.MustCompile(`(?m)([^\s:]+\.go)((?::\d+){1,2})`)
emptyline = regexp.MustCompile(`(?m)^[ \t]*\r?\n`)
importpath = regexp.MustCompile(`(?m)^# ([^ ]+)(?: \[[^ \[\]]+\])?$`)

Expand Down Expand Up @@ -185,24 +185,22 @@ func (e *test) Edit(line string) (string, error) {
Exp: passlonely,
Func: func(s string) string {
processed = true
style = config.C.PassPackageStyle

return style.Apply("PASS")
return config.C.PassPackageStyle.Apply("PASS")
},
},
editor.RegexRepl{
Exp: faillonely,
Func: func(s string) string {
processed = true
style = config.C.FailPackageStyle

return style.Apply("FAIL")
return config.C.FailPackageStyle.Apply("FAIL")
},
},

editor.RegexRepl{
Exp: filename,
Repl: config.C.FileStyle.Apply(`$1`) + ":" + config.C.LineStyle.Apply(`$2`),
Exp: filename,
Func: func(s string) string {
return filename.ReplaceAllString(s, config.C.FileStyle.Apply(`$1`)+config.C.LineStyle.Apply(`$2`)) + e.prevLineStyle.ANSI().String()
},
},
)

Expand Down
24 changes: 9 additions & 15 deletions sample/out_colored.txt
Original file line number Diff line number Diff line change
@@ -1,54 +1,48 @@
BUILD| github.com/kyoh86/richgo/sample/buildfail [github.com/kyoh86/richgo/sample/buildfail.test]
 | sample/buildfail/buildfail_test.go:8:7: t.Foo undefined (type *testing.T has no field or method Foo)
 | sample/buildfail/buildfail_test.go:8:7: t.Foo undefined (type *testing.T has no field or method Foo)
START| SampleNG
 | sample_ng_test.go:9: It's not OK... :(
 | sample_ng_test.go:9: It's not OK... :(
START| SampleNG/SubtestNG
 | sample_ng_test.go:13: It's also not OK... :(
 | sample_ng_test.go:13: It's also not OK... :(
FAIL | SampleNG (0.00s)
FAIL | SampleNG/SubtestNG (0.00s)
START| SampleOK
 | sample_ok_test.go:11: It's OK!
 | sample_ok_test.go:11: It's OK!
START| SampleOK/SubtestOK
 | time:2017-01-01T01:01:01+09:00
 | sample_ok_test.go:15: It's also OK!
 | sample_ok_test.go:15: It's also OK!
PASS | SampleOK (0.00s)
PASS | SampleOK/SubtestOK (0.00s)
START| SampleSkip
 | sample_skip_test.go:8: 
 | sample_skip_test.go:8: 
SKIP | SampleSkip (0.00s)
START| SampleSkipSub
START| SampleSkipSub/SubtestSkip
 | sample_skip_test.go:13: 
 | sample_skip_test.go:13: 
PASS | SampleSkipSub (0.00s)
SKIP | SampleSkipSub/SubtestSkip (0.00s)
START| SampleTimeout
START| SampleTimeout/SubtestTimeout
PASS | SampleTimeout (3.00s)
PASS | SampleTimeout/SubtestTimeout (3.00s)
FAIL
 | coverage: [no statements]
FAIL | github.com/kyoh86/richgo/sample 3.097s
FAIL | github.com/kyoh86/richgo/sample 3.003s
FAIL | github.com/kyoh86/richgo/sample/buildfail [build failed]
START| Cover05
PASS | Cover05 (0.00s)
PASS
COVER| 50.0% [#####_____]
PASS | github.com/kyoh86/richgo/sample/cover05 coverage: 50.0% of statements
START| CoverAll
PASS | CoverAll (0.00s)
PASS
COVER| 100.0% [##########]
PASS | github.com/kyoh86/richgo/sample/coverall coverage: 100.0% of statements
 | testing: warning: no tests to run
PASS
COVER| 0.0% [__________]
PASS | github.com/kyoh86/richgo/sample/emptytest coverage: 0.0% of statements
START| Nocover
 | nocover_test.go:8: accept
 | nocover_test.go:8: accept
PASS | Nocover (0.00s)
PASS
COVER| 0.0% [__________]
PASS | github.com/kyoh86/richgo/sample/nocover coverage: 0.0% of statements
SKIP | github.com/kyoh86/richgo/sample/notest [no test files]
FAIL
exit status 2
2 changes: 1 addition & 1 deletion sample/out_raw.txt
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ time:2017-01-01T01:01:01+09:00
--- PASS: TestSampleTimeout/SubtestTimeout (3.00s)
FAIL
coverage: [no statements]
FAIL github.com/kyoh86/richgo/sample 3.097s
FAIL github.com/kyoh86/richgo/sample 3.003s
FAIL github.com/kyoh86/richgo/sample/buildfail [build failed]
=== RUN TestCover05
--- PASS: TestCover05 (0.00s)
Expand Down

0 comments on commit f080aca

Please sign in to comment.