Skip to content

Commit

Permalink
Add color to tags
Browse files Browse the repository at this point in the history
  • Loading branch information
leonardoaramaki committed Nov 30, 2021
1 parent 6eeecc2 commit 52980d1
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 12 deletions.
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ go 1.17
require github.com/fatih/color v1.13.0

require (
github.com/mattn/go-colorable v0.1.11 // indirect
github.com/mattn/go-colorable v0.1.12 // indirect
github.com/mattn/go-isatty v0.0.14 // indirect
golang.org/x/sys v0.0.0-20211117180635-dee7805ff2e1 // indirect
golang.org/x/sys v0.0.0-20211124211545-fe61309f8881 // indirect
)
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ github.com/fatih/color v1.13.0/go.mod h1:kLAiJbzzSOZDVNGyDpeOxJ47H46qBXwg5ILebYF
github.com/mattn/go-colorable v0.1.9/go.mod h1:u6P/XSegPjTcexA+o6vUJrdnUu04hMope9wVRipJSqc=
github.com/mattn/go-colorable v0.1.11 h1:nQ+aFkoE2TMGc0b68U2OKSexC+eq46+XwZzWXHRmPYs=
github.com/mattn/go-colorable v0.1.11/go.mod h1:u5H1YNBxpqRaxsYJYSkiCWKzEfiAb1Gb520KVy5xxl4=
github.com/mattn/go-colorable v0.1.12 h1:jF+Du6AlPIjs2BiUiQlKOX0rt3SujHxPnksPKZbaA40=
github.com/mattn/go-colorable v0.1.12/go.mod h1:u5H1YNBxpqRaxsYJYSkiCWKzEfiAb1Gb520KVy5xxl4=
github.com/mattn/go-isatty v0.0.12/go.mod h1:cbi8OIDigv2wuxKPP5vlRcQ1OAZbq2CE4Kysco4FUpU=
github.com/mattn/go-isatty v0.0.14 h1:yVuAays6BHfxijgZPzw+3Zlu5yQgKGP2/hcQbHb7S9Y=
github.com/mattn/go-isatty v0.0.14/go.mod h1:7GGIvUiUoEMVVmxf/4nioHXj79iQHKdU27kJ6hsGG94=
Expand All @@ -12,3 +14,5 @@ golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c/go.mod h1:oPkhp1MJrh7nUepCBc
golang.org/x/sys v0.0.0-20210927094055-39ccf1dd6fa6/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20211117180635-dee7805ff2e1 h1:kwrAHlwJ0DUBZwQ238v+Uod/3eZ8B2K5rYsUHBQvzmI=
golang.org/x/sys v0.0.0-20211117180635-dee7805ff2e1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20211124211545-fe61309f8881 h1:TyHqChC80pFkXWraUUf6RuB5IqFdQieMLwwCJokV2pc=
golang.org/x/sys v0.0.0-20211124211545-fe61309f8881/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
37 changes: 27 additions & 10 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"strconv"
"strings"

"github.com/fatih/color"
"github.com/leonardoaramaki/gocat/adb"
"github.com/leonardoaramaki/gocat/log"
"github.com/leonardoaramaki/gocat/ps"
Expand Down Expand Up @@ -134,24 +135,38 @@ func main() {
filteredTags[tag] = tag
}

knownTags := make(map[string]*color.Color)
lastUsed := []*color.Color{
color.New(color.FgRed),
color.New(color.FgGreen),
color.New(color.FgYellow),
color.New(color.FgBlue),
color.New(color.FgMagenta),
color.New(color.FgCyan),
}

brand := adb.GetProp(devices, "ro.product.manufacturer")
sdk := adb.GetProp(devices, "ro.build.version.sdk")
serialno := adb.GetProp(devices, "ro.serialno")
abi := adb.GetProp(devices, "ro.product.cpu.abi")

adb.Run(devices, func(output string) {
var tag, prio, message string

line := log.NewLine(output)
tag = line.Tag
message = strings.TrimSpace(line.Message) + "\n"

// leaving app
if line.Tag == "ActivityManager" && strings.HasPrefix(line.Message, "Killing "+pid) {
if tag == "ActivityManager" && strings.HasPrefix(line.Message, "Killing "+pid) {
pid = pidOf(devices, packageName)
for pid == "" {
pid = pidOf(packageName, devices)
}
}

// app got killed
if line.Tag == "Process" && line.Message == "Sending signal. PID: "+pid+" SIG: 9" {
if tag == "Process" && line.Message == "Sending signal. PID: "+pid+" SIG: 9" {
pid = pidOf(devices, packageName)
for pid == "" {
pid = pidOf(packageName, devices)
Expand All @@ -162,13 +177,7 @@ func main() {
return
}

var tag, prio, message string

message = strings.TrimSpace(line.Message) + "\n"

if !raw {
tag = line.Tag

if len(tag) > 0 && tag[len(tag)-1] == ':' {
tag = tag[:len(tag)-1]
}
Expand Down Expand Up @@ -202,18 +211,26 @@ func main() {
lastTag = tag
}

if _, ok := knownTags[tag]; !ok {
knownTags[tag] = lastUsed[0]
}
tagColor := knownTags[tag]
lastUsed = lastUsed[1:]
lastUsed = append(lastUsed, tagColor)

// if copy and paste friendly
if cp {
if tag != "" {
fmt.Printf("\n⤏ %s ", tag)
fmt.Printf("\n⤏ ")
tagColor.Printf("%s ", tag)
c := line.PriorityColor()
c.Printf("%s", prio)
fmt.Printf(" [%s][%s][%s][%s][%s] ", packageName, brand, sdk, serialno, abi)
fmt.Printf("\n\n")
}
fmt.Printf("%s", message)
} else {
fmt.Printf(indent, tag)
tagColor.Printf(indent, tag)
c := line.PriorityColor()
c.Printf("%s", prio)
fmt.Printf(" %s", message)
Expand Down

0 comments on commit 52980d1

Please sign in to comment.