1
0
mirror of https://github.com/jesseduffield/lazygit.git synced 2025-01-26 05:37:18 +02:00
lazygit/pkg/gui/mergeconflicts/rendering.go

41 lines
1.4 KiB
Go
Raw Normal View History

2021-04-18 18:07:10 +10:00
package mergeconflicts
import (
"bytes"
"github.com/jesseduffield/lazygit/pkg/gui/style"
2021-04-18 18:07:10 +10:00
"github.com/jesseduffield/lazygit/pkg/theme"
"github.com/jesseduffield/lazygit/pkg/utils"
)
func ColoredConflictFile(content string, state *State, hasFocus bool) string {
if len(state.conflicts) == 0 {
return content
}
conflict, remainingConflicts := shiftConflict(state.conflicts)
var outputBuffer bytes.Buffer
for i, line := range utils.SplitLines(content) {
2021-07-31 12:54:28 +10:00
textStyle := theme.DefaultTextColor
2021-04-18 18:07:10 +10:00
if i == conflict.start || i == conflict.middle || i == conflict.end {
2021-07-31 12:54:28 +10:00
textStyle = style.FgRed
2021-04-18 18:07:10 +10:00
}
2021-04-18 18:07:10 +10:00
if hasFocus && state.conflictIndex < len(state.conflicts) && *state.conflicts[state.conflictIndex] == *conflict && shouldHighlightLine(i, conflict, state.conflictTop) {
2021-08-01 16:02:49 +10:00
textStyle = textStyle.MergeStyle(theme.SelectedRangeBgColor).SetBold()
2021-04-18 18:07:10 +10:00
}
if i == conflict.end && len(remainingConflicts) > 0 {
conflict, remainingConflicts = shiftConflict(remainingConflicts)
}
2021-07-31 12:54:28 +10:00
outputBuffer.WriteString(textStyle.Sprint(line) + "\n")
2021-04-18 18:07:10 +10:00
}
return outputBuffer.String()
}
func shiftConflict(conflicts []*mergeConflict) (*mergeConflict, []*mergeConflict) {
return conflicts[0], conflicts[1:]
}
func shouldHighlightLine(index int, conflict *mergeConflict, top bool) bool {
return (index >= conflict.start && index <= conflict.middle && top) || (index >= conflict.middle && index <= conflict.end && !top)
}