45 lines
990 B
Go
Raw Permalink Normal View History

2021-07-23 19:59:33 -07:00
package terminal_utils
import (
2022-03-06 17:07:05 -08:00
"strings"
"time"
2021-07-23 19:59:33 -07:00
)
/**
* Format a timestamp in human-readable form.
*/
func FormatDate(t time.Time) string {
2022-03-06 17:07:05 -08:00
return t.Format("Jan 2, 2006 15:04:05")
2021-07-23 19:59:33 -07:00
}
/**
* Wrap lines to fixed width, while respecting word breaks
*/
func WrapParagraph(paragraph string, width int) []string {
2022-03-06 17:07:05 -08:00
var lines []string
i := 0
for i < len(paragraph)-width {
// Find a word break at the end of the line to avoid splitting up words
end := i + width
for end > i && paragraph[end] != ' ' { // Look for a space, starting at the end
end -= 1
}
lines = append(lines, paragraph[i:end])
i = end + 1
}
lines = append(lines, paragraph[i:])
return lines
2021-07-23 19:59:33 -07:00
}
/**
* Return the text as a wrapped, indented block
*/
func WrapText(text string, width int) string {
2022-03-06 17:07:05 -08:00
paragraphs := strings.Split(text, "\n")
var lines []string
for _, paragraph := range paragraphs {
lines = append(lines, WrapParagraph(paragraph, width)...)
}
return strings.Join(lines, "\n ")
2021-07-23 19:59:33 -07:00
}