mirror of https://github.com/hashicorp/terraform
We now require the output to accept UTF-8 and we can determine how wide the terminal (if any) is, so here we begin to make use of that for the "terraform plan" command. The horizontal rule is now made of box drawing characters instead of hyphens and fills the whole terminal width. The paragraphs of text in the output are now also wrapped to fill the terminal width, instead of the hard-wrapping we did before. This is just a start down the road of making better use of the terminal capabilities. Lots of other commands could benefit from updates like these too.b-dev-overrides-plan-warning
parent
d2c3403ab6
commit
e6a516d87e
@ -0,0 +1,55 @@
|
||||
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 {
|
||||
rule := strings.Repeat("─", width)
|
||||
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 == 0 {
|
||||
// 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))
|
||||
}
|
||||
if i > 0 {
|
||||
buf.WriteByte('\n') // reintroduce the newlines we skipped in Scan
|
||||
}
|
||||
buf.WriteString(line)
|
||||
}
|
||||
return buf.String()
|
||||
}
|
||||
Loading…
Reference in new issue