mirror of
https://github.com/charmbracelet/crush.git
synced 2025-08-02 05:20:46 +03:00
refactor(diffview): simplify how we handle line endings
This commit is contained in:
@@ -193,6 +193,7 @@ func (dv *DiffView) clearSyntaxCache() {
|
||||
|
||||
// String returns the string representation of the DiffView.
|
||||
func (dv *DiffView) String() string {
|
||||
dv.normalizeLineEndings()
|
||||
dv.replaceTabs()
|
||||
if err := dv.computeDiff(); err != nil {
|
||||
return err.Error()
|
||||
@@ -227,6 +228,12 @@ func (dv *DiffView) String() string {
|
||||
}
|
||||
}
|
||||
|
||||
// normalizeLineEndings ensures the file contents use Unix-style line endings.
|
||||
func (dv *DiffView) normalizeLineEndings() {
|
||||
dv.before.content = strings.ReplaceAll(dv.before.content, "\r\n", "\n")
|
||||
dv.after.content = strings.ReplaceAll(dv.after.content, "\r\n", "\n")
|
||||
}
|
||||
|
||||
// replaceTabs replaces tabs in the before and after file contents with spaces
|
||||
// according to the specified tab width.
|
||||
func (dv *DiffView) replaceTabs() {
|
||||
@@ -396,8 +403,7 @@ func (dv *DiffView) renderUnified() string {
|
||||
shouldWrite := func() bool { return printedLines >= 0 }
|
||||
|
||||
getContent := func(in string, ls LineStyle) (content string, leadingEllipsis bool) {
|
||||
content = strings.ReplaceAll(in, "\r\n", "\n")
|
||||
content = strings.TrimSuffix(content, "\n")
|
||||
content = strings.TrimSuffix(in, "\n")
|
||||
content = dv.hightlightCode(content, ls.Code.GetBackground())
|
||||
content = ansi.GraphemeWidth.Cut(content, dv.xOffset, len(content))
|
||||
content = ansi.Truncate(content, dv.codeWidth, "…")
|
||||
@@ -520,8 +526,7 @@ func (dv *DiffView) renderSplit() string {
|
||||
shouldWrite := func() bool { return printedLines >= 0 }
|
||||
|
||||
getContent := func(in string, ls LineStyle) (content string, leadingEllipsis bool) {
|
||||
content = strings.ReplaceAll(in, "\r\n", "\n")
|
||||
content = strings.TrimSuffix(content, "\n")
|
||||
content = strings.TrimSuffix(in, "\n")
|
||||
content = dv.hightlightCode(content, ls.Code.GetBackground())
|
||||
content = ansi.GraphemeWidth.Cut(content, dv.xOffset, len(content))
|
||||
content = ansi.Truncate(content, dv.codeWidth, "…")
|
||||
|
||||
Reference in New Issue
Block a user