1
0
mirror of https://github.com/jesseduffield/lazygit.git synced 2024-12-12 11:15:00 +02:00
lazygit/pkg/gui/mergeconflicts/rendering.go

54 lines
1.7 KiB
Go
Raw Normal View History

2021-04-18 10:07:10 +02:00
package mergeconflicts
import (
"bytes"
"github.com/jesseduffield/lazygit/pkg/gui/style"
2021-04-18 10:07:10 +02: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 04:54:28 +02:00
textStyle := theme.DefaultTextColor
if i == conflict.start || i == conflict.ancestor || i == conflict.target || i == conflict.end {
2021-07-31 04:54:28 +02:00
textStyle = style.FgRed
2021-04-18 10:07:10 +02:00
}
if hasFocus && state.conflictIndex < len(state.conflicts) && *state.conflicts[state.conflictIndex] == *conflict && shouldHighlightLine(i, conflict, state.conflictSelection) {
2021-08-01 08:02:49 +02:00
textStyle = textStyle.MergeStyle(theme.SelectedRangeBgColor).SetBold()
2021-04-18 10:07:10 +02:00
}
if i == conflict.end && len(remainingConflicts) > 0 {
conflict, remainingConflicts = shiftConflict(remainingConflicts)
}
2021-07-31 04:54:28 +02:00
outputBuffer.WriteString(textStyle.Sprint(line) + "\n")
2021-04-18 10:07:10 +02:00
}
return outputBuffer.String()
}
func shiftConflict(conflicts []*mergeConflict) (*mergeConflict, []*mergeConflict) {
return conflicts[0], conflicts[1:]
}
func shouldHighlightLine(index int, conflict *mergeConflict, selection Selection) bool {
switch selection {
case TOP:
if conflict.ancestor >= 0 {
return index >= conflict.start && index <= conflict.ancestor
} else {
return index >= conflict.start && index <= conflict.target
}
case MIDDLE:
return index >= conflict.ancestor && index <= conflict.target
case BOTTOM:
return index >= conflict.target && index <= conflict.end
default:
return false
}
2021-04-18 10:07:10 +02:00
}