Skip to content
Open
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
57 changes: 19 additions & 38 deletions modules/cli/cmd/logs.go
Original file line number Diff line number Diff line change
Expand Up @@ -465,6 +465,21 @@ func printLogs(rootCtx context.Context, cmd *cobra.Command, cmdCfg *logsCmdConfi
writer.Flush()
}

var dotAssigner *dotColorAssigner
if cmdCfg.withDot {
assigner, err := newDotColorAssigner()
if err != nil {
fmt.Fprintf(cmd.OutOrStderr(), "warning: dot color persistence disabled: %v\n", err)
} else {
dotAssigner = assigner
defer func() {
if err := dotAssigner.Close(); err != nil {
fmt.Fprintf(cmd.OutOrStderr(), "warning: dot color persistence disabled: %v\n", err)
}
}()
}
}

// Write rows
var firstRecord, lastRecord *logs.LogRecord
for record := range stream.Records() {
Expand All @@ -480,7 +495,10 @@ func printLogs(rootCtx context.Context, cmd *cobra.Command, cmdCfg *logsCmdConfi
}

if cmdCfg.withDot {
dot := getDotIndicator(record.Source.ContainerID)
dot := ansiDot(dotColorFromHash(record.Source.ContainerID))
if dotAssigner != nil {
dot = dotAssigner.Dot(record.Source)
}
row = append(row, dot)
}

Expand Down Expand Up @@ -599,43 +617,6 @@ var logsCmd = &cobra.Command{
},
}

// Return ANSI color coded dot indicator based on container ID
func getDotIndicator(containerID string) string {
colors := []string{
"31m", // red
"32m", // green
"33m", // yellow
"34m", // blue
"35m", // magenta
"36m", // cyan
"91m", // bright red
"92m", // bright green
"93m", // bright yellow
"94m", // bright blue
"95m", // bright magenta
"96m", // bright cyan
"37m", // white
"90m", // gray
"97m", // bright white
}

// simple djb2 hash
hash := 5381
for _, val := range containerID {
hash = int(val) + ((hash << 5) + hash)
}

idx := hash % len(colors)

if idx < 0 {
idx = -idx
}

dot := fmt.Sprintf("\033[%s%s\033[0m", colors[idx], "\u25CF")

return dot
}

// Return table writer headers and col widths
func getTableWriterHeaders(cmdCfg *logsCmdConfig, sources []logs.LogSource) ([]string, []int) {
headers := []string{}
Expand Down
Loading