opentofu/internal/command/format/trivia.go
Martin Atkins ffe056bacb Move command/ to internal/command/
This is part of a general effort to move all of Terraform's non-library
package surface under internal in order to reinforce that these are for
internal use within Terraform only.

If you were previously importing packages under this prefix into an
external codebase, you could pin to an earlier release tag as an interim
solution until you've make a plan to achieve the same functionality some
other way.
2021-05-17 14:09:07 -07:00

59 lines
1.9 KiB
Go

package format
import (
"strings"
"github.com/mitchellh/colorstring"
wordwrap "github.com/mitchellh/go-wordwrap"
)
// HorizontalRule returns a newline character followed by a number of
// horizontal line characters to fill the given width.
//
// If the given colorize has colors enabled, the rule will also be given a
// dark grey color to attempt to visually de-emphasize it for sighted users.
//
// This is intended for printing to the UI via mitchellh/cli.UI.Output, or
// similar, which will automatically append a trailing newline too.
func HorizontalRule(color *colorstring.Colorize, width int) string {
if width <= 1 {
return "\n"
}
rule := strings.Repeat("─", width-1)
if color == nil { // sometimes unit tests don't populate this properly
return "\n" + rule
}
return color.Color("[dark_gray]\n" + rule)
}
// WordWrap takes a string containing unbroken lines of text and inserts
// newline characters to try to make the text fit within the given width.
//
// The string can already contain newline characters, for example if you are
// trying to render multiple paragraphs of text. (In that case, our usual
// style would be to have _two_ newline characters as the paragraph separator.)
//
// As a special case, any line that begins with at least one space will be left
// unbroken. This allows including literal segments in the output, such as
// code snippets or filenames, where word wrapping would be confusing.
func WordWrap(str string, width int) string {
if width <= 1 {
// Silly edge case. We'll just return the original string to avoid
// panicking or doing other weird stuff.
return str
}
var buf strings.Builder
lines := strings.Split(str, "\n")
for i, line := range lines {
if !strings.HasPrefix(line, " ") {
line = wordwrap.WrapString(line, uint(width-1))
}
if i > 0 {
buf.WriteByte('\n') // reintroduce the newlines we skipped in Scan
}
buf.WriteString(line)
}
return buf.String()
}