diff options
author | Jesse Duffield <jessedduffield@gmail.com> | 2022-03-19 16:34:46 +1100 |
---|---|---|
committer | Jesse Duffield <jessedduffield@gmail.com> | 2022-03-24 20:14:41 +1100 |
commit | bf4f06ab4e6ceefe388e0efefcc553526f3d96c2 (patch) | |
tree | b4cfdb71e31fa7d960722f192a56e5fb1a483e5e /pkg/commands/patch | |
parent | eda8f4a5d4302691d99efd066f9851809c984bc0 (diff) |
more generics
Diffstat (limited to 'pkg/commands/patch')
-rw-r--r-- | pkg/commands/patch/patch_manager.go | 25 | ||||
-rw-r--r-- | pkg/commands/patch/patch_parser.go | 28 |
2 files changed, 27 insertions, 26 deletions
diff --git a/pkg/commands/patch/patch_manager.go b/pkg/commands/patch/patch_manager.go index 1282356f8..4fb6507e6 100644 --- a/pkg/commands/patch/patch_manager.go +++ b/pkg/commands/patch/patch_manager.go @@ -4,6 +4,8 @@ import ( "sort" "strings" + "github.com/jesseduffield/generics/maps" + "github.com/jesseduffield/generics/slices" "github.com/samber/lo" "github.com/sirupsen/logrus" ) @@ -72,8 +74,9 @@ func (p *PatchManager) Start(from, to string, reverse bool, canRebase bool) { func (p *PatchManager) addFileWhole(info *fileInfo) { info.mode = WHOLE lineCount := len(strings.Split(info.diff, "\n")) - info.includedLineIndices = make([]int, lineCount) // add every line index + // TODO: add tests and then use lo.Range to simplify + info.includedLineIndices = make([]int, lineCount) for i := 0; i < lineCount; i++ { info.includedLineIndices[i] = i } @@ -192,21 +195,15 @@ func (p *PatchManager) RenderPatchForFile(filename string, plain bool, reverse b func (p *PatchManager) renderEachFilePatch(plain bool) []string { // sort files by name then iterate through and render each patch - filenames := make([]string, len(p.fileInfoMap)) - index := 0 - for filename := range p.fileInfoMap { - filenames[index] = filename - index++ - } + filenames := maps.Keys(p.fileInfoMap) sort.Strings(filenames) - output := []string{} - for _, filename := range filenames { - patch := p.RenderPatchForFile(filename, plain, false, true) - if patch != "" { - output = append(output, patch) - } - } + patches := slices.Map(filenames, func(filename string) string { + return p.RenderPatchForFile(filename, plain, false, true) + }) + output := slices.Filter(patches, func(patch string) bool { + return patch != "" + }) return output } diff --git a/pkg/commands/patch/patch_parser.go b/pkg/commands/patch/patch_parser.go index 3810d8a29..097f01329 100644 --- a/pkg/commands/patch/patch_parser.go +++ b/pkg/commands/patch/patch_parser.go @@ -4,9 +4,9 @@ import ( "regexp" "strings" + "github.com/jesseduffield/generics/slices" "github.com/jesseduffield/lazygit/pkg/gui/style" "github.com/jesseduffield/lazygit/pkg/theme" - "github.com/jesseduffield/lazygit/pkg/utils" "github.com/samber/lo" "github.com/sirupsen/logrus" ) @@ -184,16 +184,21 @@ func parsePatch(patch string) ([]int, []int, []*PatchLine) { // Render returns the coloured string of the diff with any selected lines highlighted func (p *PatchParser) Render(firstLineIndex int, lastLineIndex int, incLineIndices []int) string { - renderedLines := make([]string, len(p.PatchLines)) - for index, patchLine := range p.PatchLines { + contentToDisplay := slices.Some(p.PatchLines, func(line *PatchLine) bool { + return line.Content != "" + }) + if !contentToDisplay { + return "" + } + + renderedLines := lo.Map(p.PatchLines, func(patchLine *PatchLine, index int) string { selected := index >= firstLineIndex && index <= lastLineIndex included := lo.Contains(incLineIndices, index) - renderedLines[index] = patchLine.render(selected, included) - } + return patchLine.render(selected, included) + }) + result := strings.Join(renderedLines, "\n") - if strings.TrimSpace(utils.Decolorise(result)) == "" { - return "" - } + return result } @@ -202,10 +207,9 @@ func (p *PatchParser) Render(firstLineIndex int, lastLineIndex int, incLineIndic func (p *PatchParser) PlainRenderLines(firstLineIndex, lastLineIndex int) string { linesToCopy := p.PatchLines[firstLineIndex : lastLineIndex+1] - renderedLines := make([]string, len(linesToCopy)) - for index, line := range linesToCopy { - renderedLines[index] = line.Content - } + renderedLines := slices.Map(linesToCopy, func(line *PatchLine) string { + return line.Content + }) return strings.Join(renderedLines, "\n") } |