()
| 59 | } |
| 60 | |
| 61 | func (self *patchPresenter) format() string { |
| 62 | // if we have no changes in our patch (i.e. no additions or deletions) then |
| 63 | // the patch is effectively empty and we can return an empty string |
| 64 | if !self.patch.ContainsChanges() { |
| 65 | return "" |
| 66 | } |
| 67 | |
| 68 | stringBuilder := &strings.Builder{} |
| 69 | lineIdx := 0 |
| 70 | appendLine := func(line string) { |
| 71 | _, _ = stringBuilder.WriteString(line + "\n") |
| 72 | |
| 73 | lineIdx++ |
| 74 | } |
| 75 | |
| 76 | for _, line := range self.patch.header { |
| 77 | // always passing false for 'included' here because header lines are not part of the patch |
| 78 | appendLine(self.formatLineAux(line, theme.DefaultTextColor.SetBold(), false)) |
| 79 | } |
| 80 | |
| 81 | for _, hunk := range self.patch.hunks { |
| 82 | appendLine( |
| 83 | self.formatLineAux( |
| 84 | hunk.formatHeaderStart(), |
| 85 | style.FgCyan, |
| 86 | false, |
| 87 | ) + |
| 88 | // we're splitting the line into two parts: the diff header and the context |
| 89 | // We explicitly pass 'included' as false for both because these are not part |
| 90 | // of the actual patch |
| 91 | self.formatLineAux( |
| 92 | hunk.headerContext, |
| 93 | theme.DefaultTextColor, |
| 94 | false, |
| 95 | ), |
| 96 | ) |
| 97 | |
| 98 | for _, line := range hunk.bodyLines { |
| 99 | style := self.patchLineStyle(line) |
| 100 | if line.IsChange() { |
| 101 | appendLine(self.formatLine(line.Content, style, lineIdx)) |
| 102 | } else { |
| 103 | appendLine(self.formatLineAux(line.Content, style, false)) |
| 104 | } |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | return stringBuilder.String() |
| 109 | } |
| 110 | |
| 111 | func (self *patchPresenter) patchLineStyle(patchLine *PatchLine) style.TextStyle { |
| 112 | switch patchLine.Kind { |
no test coverage detected