Skip to content

Commit

Permalink
Working on word wrapping the title
Browse files Browse the repository at this point in the history
  • Loading branch information
vshah23 committed Dec 5, 2022
1 parent 6bb0cf5 commit 04bc9c3
Showing 1 changed file with 79 additions and 9 deletions.
88 changes: 79 additions & 9 deletions log/event_print.go
Expand Up @@ -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) {
Expand Down Expand Up @@ -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
Expand All @@ -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 {
Expand Down

0 comments on commit 04bc9c3

Please sign in to comment.