Skip to content

Commit

Permalink
reduce additional memory allocation
Browse files Browse the repository at this point in the history
  • Loading branch information
wjiec committed Aug 16, 2024
1 parent ba8d039 commit b5d6d4c
Showing 1 changed file with 11 additions and 3 deletions.
14 changes: 11 additions & 3 deletions internal/dao/pod.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@ import (
"errors"
"fmt"
"io"
"regexp"
"sync"
"time"

"github.com/derailed/k9s/internal"
"github.com/derailed/k9s/internal/client"
"github.com/derailed/k9s/internal/render"
"github.com/derailed/k9s/internal/watch"
"github.com/derailed/tview"
"github.com/rs/zerolog/log"
v1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
Expand All @@ -35,6 +35,8 @@ var (
_ Controller = (*Pod)(nil)
_ ContainsPodSpec = (*Pod)(nil)
_ ImageLister = (*Pod)(nil)

nonEscapePattern = regexp.MustCompile(`(\[[a-zA-Z0-9_,;: \-\."#]+\[*)\]`)
)

const (
Expand Down Expand Up @@ -381,8 +383,8 @@ func readLogs(ctx context.Context, wg *sync.WaitGroup, stream io.ReadCloser, out
r := bufio.NewReader(stream)
for {
var item *LogItem
if line, err := r.ReadString('\n'); err == nil {
item = opts.ToLogItem([]byte(tview.Escape(line)))
if line, err := r.ReadBytes('\n'); err == nil {
item = opts.ToLogItem(Escape(line))
} else {
if errors.Is(err, io.EOF) {
e := fmt.Errorf("Stream closed %w for %s", err, opts.Info())
Expand Down Expand Up @@ -519,3 +521,9 @@ func (p *Pod) Sanitize(ctx context.Context, ns string) (int, error) {

return count, nil
}

// Escape escapes the given bytes such that color and/or region tags are not
// recognized and substituted by the print functions of tview package.
func Escape(text []byte) []byte {
return nonEscapePattern.ReplaceAll(text, []byte("$1[]"))
}

0 comments on commit b5d6d4c

Please sign in to comment.