MCPcopy Index your code
hub / github.com/cloudfoundry/cli / displayWrappingTableWithWidth

Method displayWrappingTableWithWidth

util/ui/ui.go:431–486  ·  view source on GitHub ↗
(prefix string, table [][]string, padding int)

Source from the content-addressed store, hash-verified

429}
430
431func (ui *UI) displayWrappingTableWithWidth(prefix string, table [][]string, padding int) {
432 ui.terminalLock.Lock()
433 defer ui.terminalLock.Unlock()
434
435 var columnPadding []int
436
437 rows := len(table)
438 columns := len(table[0])
439
440 for col := 0; col < columns-1; col++ {
441 var max int
442 for row := 0; row < rows; row++ {
443 if strLen := runewidth.StringWidth(table[row][col]); max < strLen {
444 max = strLen
445 }
446 }
447 columnPadding = append(columnPadding, max+padding)
448 }
449
450 spilloverPadding := len(prefix) + sum(columnPadding)
451 lastColumnWidth := ui.TerminalWidth - spilloverPadding
452
453 for row := 0; row < rows; row++ {
454 fmt.Fprint(ui.Out, prefix)
455
456 // for all columns except last, add cell value and padding
457 for col := 0; col < columns-1; col++ {
458 var addedPadding int
459 if col+1 != columns {
460 addedPadding = columnPadding[col] - runewidth.StringWidth(table[row][col])
461 }
462 fmt.Fprintf(ui.Out, "%s%s", table[row][col], strings.Repeat(" ", addedPadding))
463 }
464
465 // for last column, add each word individually. If the added word would make the column exceed terminal width, create a new line and add padding
466 words := strings.Split(table[row][columns-1], " ")
467 currentWidth := 0
468
469 for _, word := range words {
470 wordWidth := runewidth.StringWidth(word)
471 switch {
472 case currentWidth == 0:
473 currentWidth = wordWidth
474 fmt.Fprintf(ui.Out, "%s", word)
475 case (wordWidth + 1 + currentWidth) > lastColumnWidth:
476 fmt.Fprintf(ui.Out, "\n%s%s", strings.Repeat(" ", spilloverPadding), word)
477 currentWidth = wordWidth
478 default:
479 fmt.Fprintf(ui.Out, " %s", word)
480 currentWidth += wordWidth + 1
481 }
482 }
483
484 fmt.Fprintf(ui.Out, "\n")
485 }
486}
487
488func (ui *UI) modifyColor(text string, colorPrinter *color.Color) string {

Callers 1

DisplayKeyValueTableMethod · 0.95

Calls 1

sumFunction · 0.85

Tested by

no test coverage detected