RenderNewFile renders file content as an all-added diff (for untracked files).
(content, filename string, styles Styles, t theme.Theme, width int)
| 232 | |
| 233 | // RenderNewFile renders file content as an all-added diff (for untracked files). |
| 234 | func RenderNewFile(content, filename string, styles Styles, t theme.Theme, width int) string { |
| 235 | initChromaStyle(t.ChromaStyle) |
| 236 | |
| 237 | var b strings.Builder |
| 238 | codeWidth := width - lineNumWidth*2 - 3 |
| 239 | |
| 240 | for i, line := range strings.Split(content, "\n") { |
| 241 | num := i + 1 |
| 242 | nums := styles.DiffLineNumAdded.Render(" " + fmt.Sprintf("%4d", num)) |
| 243 | highlighted := highlightLine(line, filename, t.AddedBg) |
| 244 | prefix := styles.DiffAdded.Render("+ ") |
| 245 | contentWidth := lipgloss.Width(prefix) + lipgloss.Width(highlighted) |
| 246 | padding := "" |
| 247 | if pad := codeWidth - contentWidth; pad > 0 { |
| 248 | padding = styles.DiffAddedBg.Render(strings.Repeat(" ", pad)) |
| 249 | } |
| 250 | b.WriteString(nums + " " + prefix + highlighted + padding) |
| 251 | b.WriteByte('\n') |
| 252 | } |
| 253 | return b.String() |
| 254 | } |
| 255 | |
| 256 | // RenderBinaryFile renders a placeholder for binary files. |
| 257 | func RenderBinaryFile(styles Styles, width int) string { |