Repository: derailed/k9s
File: internal/view/live_view.go (Go)

Code before:
}

// InCmdMode checks if prompt is active.
func (v *LiveView) InCmdMode() bool {
	return v.cmdBuff.InCmdMode()
}

// ResourceFailed notifies when their is an issue.
func (v *LiveView) ResourceFailed(err error) {
	v.text.SetTextAlign(tview.AlignCenter)
	x, _, w, _ := v.GetRect()
	v.text.SetText(cowTalk(err.Error(), x+w))
}

// ResourceChanged notifies when the filter changes.
func (v *LiveView) ResourceChanged(lines []string, matches fuzzy.Matches) {
	v.app.QueueUpdateDraw(func() {
		v.text.SetTextAlign(tview.AlignLeft)
		v.maxRegions = len(matches)
		ll := make([]string, len(lines))
		copy(ll, lines)
		offsetForLine := make(map[int]int)
		for i, m := range matches {
			loc, line := m.MatchedIndexes, ll[m.Index]
			offset := offsetForLine[m.Index]
			loc[0] += offset
			loc[1] += offset
			regionStr := fmt.Sprintf(`<<<"search_%d">>>%s<<<"">>>`, i, line[loc[0]:loc[1]])
			ll[m.Index] = line[:loc[0]] + regionStr + line[loc[1]:]
			offsetForLine[m.Index] += len(regionStr) - (loc[1] - loc[0])
		}

		if v.text.GetText(true) == "" {
			v.text.ScrollToBeginning()
		}

		v.text.SetText(colorizeYAML(v.app.Styles.Views().Yaml, strings.Join(ll, "\n")))
		v.text.Highlight()
		if v.currentRegion < v.maxRegions {
			v.text.Highlight("search_" + strconv.Itoa(v.currentRegion))
			v.text.ScrollToHighlight()
		}
		v.updateTitle()
	})
}

// BufferChanged indicates the buffer was changed.
func (v *LiveView) BufferChanged(_, _ string) {}

// BufferCompleted indicates input was accepted.
func (v *LiveView) BufferCompleted(text, _ string) {

Review comment (suggestion):
I get the use here but better to use parrallel assign ie loc[0], loc[1] = loc[0]+offset,loc[1]+offset. Makes it cleared imho as they are related. 

Code after:
	return nil
}

// InCmdMode checks if prompt is active.
func (v *LiveView) InCmdMode() bool {
	return v.cmdBuff.InCmdMode()
}

// ResourceFailed notifies when their is an issue.
func (v *LiveView) ResourceFailed(err error) {
	v.text.SetTextAlign(tview.AlignCenter)
	x, _, w, _ := v.GetRect()
	v.text.SetText(cowTalk(err.Error(), x+w))
}

func (*LiveView) linesWithRegions(lines []string, matches fuzzy.Matches) []string {
	ll := make([]string, len(lines))
	copy(ll, lines)
	offsetForLine := make(map[int]int)
	for i, m := range matches {
		loc, line := m.MatchedIndexes, ll[m.Index]
		offset := offsetForLine[m.Index]
		loc[0], loc[1] = loc[0]+offset, loc[1]+offset
		regionStr := `<<<"search_` + strconv.Itoa(i) + `">>>` + line[loc[0]:loc[1]] + `<<<"">>>`
		ll[m.Index] = line[:loc[0]] + regionStr + line[loc[1]:]
		offsetForLine[m.Index] += len(regionStr) - (loc[1] - loc[0])
	}
	return ll
}

// ResourceChanged notifies when the filter changes.
func (v *LiveView) ResourceChanged(lines []string, matches fuzzy.Matches) {
	v.app.QueueUpdateDraw(func() {
		defer func(t time.Time) {
			log.Debug().Msgf("Live view render time: %v", time.Since(t))
		}(time.Now())

		v.text.SetTextAlign(tview.AlignLeft)
		v.maxRegions = len(matches)

		if v.text.GetText(true) == "" {
			v.text.ScrollToBeginning()
		}

		lines = v.linesWithRegions(lines, matches)

		v.text.SetText(colorizeYAML(v.app.Styles.Views().Yaml, strings.Join(lines, "\n")))
		v.text.Highlight()
		if v.currentRegion < v.maxRegions {
			v.text.Highlight("search_" + strconv.Itoa(v.currentRegion))
			v.text.ScrollToHighlight()