From 5d3b3c66566b7a559fd6d10b586108828b394906 Mon Sep 17 00:00:00 2001 From: Stefan Haller Date: Sun, 17 Nov 2024 19:07:53 +0100 Subject: [PATCH] Extract helper function This doesn't improve the code much in the current state, but we'll add some more code to this helper function in the next commit, which makes it worth it. --- pkg/utils/lines.go | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/pkg/utils/lines.go b/pkg/utils/lines.go index ec27d0955..9ca57d80d 100644 --- a/pkg/utils/lines.go +++ b/pkg/utils/lines.go @@ -124,6 +124,10 @@ func WrapViewLinesToWidth(wrap bool, text string, width int) []string { } } + appendWrappedLine := func(str string) { + wrappedLines = append(wrappedLines, str) + } + n := 0 offset := 0 lastWhitespaceIndex := -1 @@ -133,23 +137,23 @@ func WrapViewLinesToWidth(wrap bool, text string, width int) []string { if n > width { if currChr == ' ' { - wrappedLines = append(wrappedLines, line[offset:i]) + appendWrappedLine(line[offset:i]) offset = i + 1 n = 0 } else if currChr == '-' { - wrappedLines = append(wrappedLines, line[offset:i]) + appendWrappedLine(line[offset:i]) offset = i n = rw } else if lastWhitespaceIndex != -1 { if line[lastWhitespaceIndex] == '-' { - wrappedLines = append(wrappedLines, line[offset:lastWhitespaceIndex+1]) + appendWrappedLine(line[offset : lastWhitespaceIndex+1]) } else { - wrappedLines = append(wrappedLines, line[offset:lastWhitespaceIndex]) + appendWrappedLine(line[offset:lastWhitespaceIndex]) } offset = lastWhitespaceIndex + 1 n = runewidth.StringWidth(line[offset : i+1]) } else { - wrappedLines = append(wrappedLines, line[offset:i]) + appendWrappedLine(line[offset:i]) offset = i n = rw } @@ -159,7 +163,7 @@ func WrapViewLinesToWidth(wrap bool, text string, width int) []string { } } - wrappedLines = append(wrappedLines, line[offset:]) + appendWrappedLine(line[offset:]) } return wrappedLines