github.com/myhau/pulumi/pkg/v3@v3.70.2-0.20221116134521-f2775972e587/backend/display/tableutil.go (about) 1 // Copyright 2016-2018, Pulumi Corporation. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package display 16 17 import ( 18 "strings" 19 20 "github.com/pulumi/pulumi/sdk/v3/go/common/diag/colors" 21 "github.com/pulumi/pulumi/sdk/v3/go/common/util/contract" 22 ) 23 24 func columnHeader(msg string) string { 25 return colors.Underline + colors.BrightBlue + msg + colors.Reset 26 } 27 28 func messagePadding(message string, maxWidth, extraPadding int) string { 29 extraWhitespace := maxWidth - colors.MeasureColorizedString(message) 30 contract.Assertf(extraWhitespace >= 0, "Neg whitespace. %v %s", maxWidth, message) 31 32 // Place two spaces between all columns (except after the first column). The first 33 // column already has a ": " so it doesn't need the extra space. 34 extraWhitespace += extraPadding 35 36 return strings.Repeat(" ", extraWhitespace) 37 } 38 39 // Gets the padding necessary to prepend to a column in order to keep it aligned in the terminal. 40 func columnPadding(columns []string, columnIndex int, maxColumnWidths []int) string { 41 extraWhitespace := " " 42 if columnIndex >= 0 && len(maxColumnWidths) > 0 { 43 column := columns[columnIndex] 44 maxWidth := maxColumnWidths[columnIndex] 45 extraWhitespace = messagePadding(column, maxWidth, 2) 46 } 47 return extraWhitespace 48 } 49 50 // Gets the fully padded message to be shown. The message will always include the ID of the 51 // status, then some amount of optional padding, then some amount of msgWithColors, then the 52 // suffix. Importantly, if there isn't enough room to display all of that on the terminal, then 53 // the msg will be truncated to try to make it fit. 54 func renderRow(columns []string, maxColumnWidths []int) string { 55 var row strings.Builder 56 for i := 0; i < len(columns); i++ { 57 row.WriteString(columnPadding(columns, i-1, maxColumnWidths)) 58 row.WriteString(columns[i]) 59 } 60 return row.String() 61 }