From 04bc9c32657cf670808bd31feff17b8301ed360e Mon Sep 17 00:00:00 2001 From: vikas Date: Mon, 5 Dec 2022 11:45:23 -0600 Subject: [PATCH] Working on word wrapping the title --- log/event_print.go | 88 +++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 79 insertions(+), 9 deletions(-) diff --git a/log/event_print.go b/log/event_print.go index de9b572a6..fe8f85330 100644 --- a/log/event_print.go +++ b/log/event_print.go @@ -137,7 +137,15 @@ func getSummaryFooterRow(status int, title, reason string, duration int64, depre footerTitle := getFooterTitle(level, title, reason, deprecated, footerTitleBoxWidth) executionTime := getFooterExecutionTime(duration) - return fmt.Sprintf("|%s|%s|%s|", icon, footerTitle, executionTime) + footerRow := fmt.Sprintf("|%s|", icon) + + for _, s := range footerTitle { + footerRow = fmt.Sprintf("%s%s|", footerRow, s) + } + + footerRow = fmt.Sprintf("%s%s|", footerRow, executionTime) + + return footerRow } func transformStatusToIconAndLevel(status int) (string, corelog.Level) { @@ -176,7 +184,7 @@ func transformStatusToIconAndLevel(status int) (string, corelog.Level) { // reason information. // This implementation leaves the prefix and suffix intact so they are always visible and instead it truncates just the // step title. -func getFooterTitle(level corelog.Level, title, reason string, deprecated bool, width int) string { +func getFooterTitle(level corelog.Level, title, reason string, deprecated bool, width int) []string { // Deduct the leading and trailing space from the available width actualWidth := width - 2 availableTitleWidth := actualWidth @@ -196,20 +204,82 @@ func getFooterTitle(level corelog.Level, title, reason string, deprecated bool, title = fmt.Sprintf("%s (%s)", title, reason) } + var output []string = []string{} + if deprecated { title = strings.TrimPrefix(title, deprecatedPrefix) title = strings.TrimSpace(title) - // Deduct the deprecated prefix length and the space between them to only shorten the title - actualWidth = actualWidth - len(deprecatedPrefix) - 1 - widthConstrainedTitle := widthConstrainedString(title, actualWidth) - title = fmt.Sprintf("%s %s", corelog.AddColor(corelog.ErrorLevel, deprecatedPrefix), corelog.AddColor(level, widthConstrainedTitle)) + start, end := 0, actualWidth-1 + for { + if end <= len(title) { + //find the space where we can insert a line break + for end > start && string(title[end]) != " " { + end-- + } + + var wordWrapTitle string + if start >= end { + end = start + actualWidth + } + + if string(title[end]) != " " { + //replace the space with a linebreak + wordWrapTitle = title[start : end-1] + } else { + wordWrapTitle = title[start:end] + } + + //add the line to our output array + output = append(output, fmt.Sprintf(" %s\n", wordWrapTitle)) + //repeat with the remaining title + start = end + 1 + end = start + actualWidth + } else { + // Deduct the deprecated prefix length and the space between them to only shorten the title + actualWidth = actualWidth - len(deprecatedPrefix) - 1 + widthConstrainedTitle := widthConstrainedString(title, actualWidth) + output = append(output, fmt.Sprintf(" %s %s ", corelog.AddColor(corelog.ErrorLevel, deprecatedPrefix), corelog.AddColor(level, widthConstrainedTitle))) + break + } + } } else { - title = widthConstrainedString(title, actualWidth) - title = corelog.AddColor(level, title) + start, end := 0, actualWidth-1 + for { + if end <= len(title) { + //find the space where we can insert a line break + for end > start && string(title[end]) != " " { + end-- + } + + var wordWrapTitle string + if start >= end { + end = start + actualWidth + } + + if string(title[end]) == " " { + //replace the space with a linebreak + wordWrapTitle = title[start:end] + } else { + wordWrapTitle = title[start : end+1] + } + + //add the line to our output array + output = append(output, fmt.Sprintf(" %s\n", wordWrapTitle)) + //repeat with the remaining title + start = end + 1 + end = start + actualWidth + } else { + wordWrapTitle := title[start:] + wordWrapTitle = widthConstrainedString(wordWrapTitle, actualWidth) + wordWrapTitle = corelog.AddColor(level, wordWrapTitle) + output = append(output, fmt.Sprintf(" %s ", wordWrapTitle)) + break + } + } } - return fmt.Sprintf(" %s ", title) + return output } func getFooterExecutionTime(duration int64) string {