diff options
author | Stefan Haller <stefan@haller-berlin.de> | 2024-06-11 17:23:42 +0200 |
---|---|---|
committer | Stefan Haller <stefan@haller-berlin.de> | 2024-06-23 12:33:15 +0200 |
commit | dbc21af3b11940018c25a44dc3ccd33e4ebc0a06 (patch) | |
tree | 53b6c03c8c693d9cf5b77ff21e1e3f091d1437bb | |
parent | 6f8244e3fe23cc44019e2dee49fe67a365538e30 (diff) |
Extract function wrapMessageToWidth
This steals even more code from `gocui.lineWrap`.
We'll make use of this in the next commit.
-rw-r--r-- | pkg/gui/controllers/helpers/confirmation_helper.go | 27 |
1 files changed, 21 insertions, 6 deletions
diff --git a/pkg/gui/controllers/helpers/confirmation_helper.go b/pkg/gui/controllers/helpers/confirmation_helper.go index 1e60b5f08..7801081fb 100644 --- a/pkg/gui/controllers/helpers/confirmation_helper.go +++ b/pkg/gui/controllers/helpers/confirmation_helper.go @@ -63,15 +63,20 @@ func (self *ConfirmationHelper) DeactivateConfirmationPrompt() { // Temporary hack: we're just duplicating the logic in `gocui.lineWrap` func getMessageHeight(wrap bool, message string, width int) int { + return len(wrapMessageToWidth(wrap, message, width)) +} + +func wrapMessageToWidth(wrap bool, message string, width int) []string { + lines := strings.Split(message, "\n") if !wrap { - return len(strings.Split(message, "\n")) + return lines } - lineCount := 0 - lines := strings.Split(message, "\n") + wrappedLines := make([]string, 0, len(lines)) for _, line := range lines { n := 0 + offset := 0 lastWhitespaceIndex := -1 for i, currChr := range line { rw := runewidth.RuneWidth(currChr) @@ -79,28 +84,38 @@ func getMessageHeight(wrap bool, message string, width int) int { if n > width { if currChr == ' ' { + wrappedLines = append(wrappedLines, line[offset:i]) + offset = i + 1 n = 0 } else if currChr == '-' { + wrappedLines = append(wrappedLines, line[offset:i]) + offset = i n = rw } else if lastWhitespaceIndex != -1 && lastWhitespaceIndex+1 != i { if line[lastWhitespaceIndex] == '-' { + wrappedLines = append(wrappedLines, line[offset:lastWhitespaceIndex+1]) + offset = lastWhitespaceIndex + 1 n = i - lastWhitespaceIndex } else { + wrappedLines = append(wrappedLines, line[offset:lastWhitespaceIndex]) + offset = lastWhitespaceIndex + 1 n = i - lastWhitespaceIndex + 1 } } else { + wrappedLines = append(wrappedLines, line[offset:i]) + offset = i n = rw } - lineCount++ lastWhitespaceIndex = -1 } else if currChr == ' ' || currChr == '-' { lastWhitespaceIndex = i } } - lineCount++ + + wrappedLines = append(wrappedLines, line[offset:]) } - return lineCount + return wrappedLines } func (self *ConfirmationHelper) getPopupPanelDimensions(wrap bool, prompt string) (int, int, int, int) { |