Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add legend to stack trace #68

Merged
merged 3 commits into from
Feb 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ Most recent version is listed first.

# v0.0.18
- Add ability to diff items: https://github.com/komuw/kama/pull/67
- Add legend to stack trace: https://github.com/komuw/kama/pull/68

# v0.0.17
- Add ability to dump items with circular references: https://github.com/komuw/kama/pull/66
Expand Down
5 changes: 1 addition & 4 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,7 @@ module github.com/komuw/kama

go 1.22

require (
golang.org/x/sys v0.17.0
golang.org/x/tools v0.18.0
)
require golang.org/x/tools v0.18.0

require (
github.com/google/go-cmp v0.6.0 // indirect
Expand Down
2 changes: 0 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@ golang.org/x/mod v0.15.0 h1:SernR4v+D55NyBH2QiEQrlBAnj1ECL6AGrA5+dPaMY8=
golang.org/x/mod v0.15.0/go.mod h1:hTbmBsO62+eylJbnUtE2MGJUyE7QWk4xUqPFrRgJ+7c=
golang.org/x/sync v0.6.0 h1:5BMeUDZ7vkXGfEr1x9B4bRcTH4lpkTkpdh0T/J+qjbQ=
golang.org/x/sync v0.6.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
golang.org/x/sys v0.17.0 h1:25cE3gD+tdBA7lp7QfhuV+rJiE9YXTcS3VG1SqssI/Y=
golang.org/x/sys v0.17.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/tools v0.18.0 h1:k8NLag8AGHnn+PHbl7g43CtqZAwG60vZkLqgyZgIHgQ=
golang.org/x/tools v0.18.0/go.mod h1:GL7B4CwcLLeo59yx/9UWWuNOW1n3VZ4f5axWfML7Lcg=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
Expand Down
32 changes: 21 additions & 11 deletions stack.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,28 +8,40 @@ import (
"regexp"
"runtime"
"strings"
)

"golang.org/x/sys/unix"
const (
compilerColor = "blue"
thirdPartyColor = "yellow"
yourColor = "red"
)

func stackp() {
goModCache := os.Getenv("GOMODCACHE")
re := regexp.MustCompile(`\d:`) // this pattern is the one created in `readLastLine()`

traces := getStackTrace()
if len(traces) > 0 {
printWithColor(
fmt.Sprintf("LEGEND:\n compiler: %s\n thirdParty: %s\n yours: %s\n", compilerColor, thirdPartyColor, yourColor),
"DEFAULT",
true,
)
}

for _, v := range traces {
if strings.Contains(v, "go/src/") {
// compiler
printWithColor(v, "cyan", false)
printWithColor(v, compilerColor, false)
} else if goModCache != "" && strings.Contains(v, goModCache) {
// third party
printWithColor(v, "magenta", false)
printWithColor(v, thirdPartyColor, false)
} else if re.MatchString(v) {
// this is code snippets
printWithColor(v, "green", false)
printWithColor(v, yourColor, false)
} else {
// your code
printWithColor(v, "green", true)
printWithColor(v, yourColor, true)
}
}
}
Expand Down Expand Up @@ -137,9 +149,9 @@ func setColor(code int, bold bool) {
func printWithColor(s, color string, bold bool) {
// TODO: should be iota
colors := map[string]int{
// Go compiler == cyan
// Third party == magenta
// Your code == green
// Go compiler == compilerColor
// Third party == thirdPartyColor
// Your code == yourColor

"black": 30,
"red": 31,
Expand Down Expand Up @@ -176,7 +188,5 @@ func noColor() bool {
return true
}

fd := os.Stdout.Fd()
_, err := unix.IoctlGetTermios(int(fd), unix.TCGETS)
return err == nil
return false
}
Loading