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

42 lines
1.3 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
2021-08-25 12:43:57 +02:00
if conflict.isMarkerLine(i) {
2021-07-31 04:54:28 +02:00
textStyle = style.FgRed
2021-04-18 10:07:10 +02:00
}
2021-08-23 11:28:20 +02:00
if hasFocus && state.conflictIndex < len(state.conflicts) && *state.conflicts[state.conflictIndex] == *conflict && shouldHighlightLine(i, conflict, state.Selection()) {
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 {
2021-08-25 12:43:57 +02:00
selectionStart, selectionEnd := selection.bounds(conflict)
return index >= selectionStart && index <= selectionEnd
2021-04-18 10:07:10 +02:00
}