...

Source file src/github.com/jedib0t/go-pretty/v6/text/valign.go

Documentation: github.com/jedib0t/go-pretty/v6/text

     1  package text
     2  
     3  import "strings"
     4  
     5  // VAlign denotes how text is to be aligned vertically.
     6  type VAlign int
     7  
     8  // VAlign enumerations
     9  const (
    10  	VAlignDefault VAlign = iota // same as VAlignTop
    11  	VAlignTop                   // "top\n\n"
    12  	VAlignMiddle                // "\nmiddle\n"
    13  	VAlignBottom                // "\n\nbottom"
    14  )
    15  
    16  // Apply aligns the lines vertically. For ex.:
    17  //  * VAlignTop.Apply({"Game", "Of", "Thrones"},    5)
    18  // 	    returns {"Game", "Of", "Thrones", "", ""}
    19  //  * VAlignMiddle.Apply({"Game", "Of", "Thrones"}, 5)
    20  // 	    returns {"", "Game", "Of", "Thrones", ""}
    21  //  * VAlignBottom.Apply({"Game", "Of", "Thrones"}, 5)
    22  // 	    returns {"", "", "Game", "Of", "Thrones"}
    23  func (va VAlign) Apply(lines []string, maxLines int) []string {
    24  	if len(lines) == maxLines {
    25  		return lines
    26  	} else if len(lines) > maxLines {
    27  		maxLines = len(lines)
    28  	}
    29  
    30  	insertIdx := 0
    31  	if va == VAlignMiddle {
    32  		insertIdx = int(maxLines-len(lines)) / 2
    33  	} else if va == VAlignBottom {
    34  		insertIdx = maxLines - len(lines)
    35  	}
    36  
    37  	linesOut := strings.Split(strings.Repeat("\n", maxLines-1), "\n")
    38  	for idx, line := range lines {
    39  		linesOut[idx+insertIdx] = line
    40  	}
    41  	return linesOut
    42  }
    43  
    44  // ApplyStr aligns the string (of 1 or more lines) vertically. For ex.:
    45  //  * VAlignTop.ApplyStr("Game\nOf\nThrones",    5)
    46  // 	    returns {"Game", "Of", "Thrones", "", ""}
    47  //  * VAlignMiddle.ApplyStr("Game\nOf\nThrones", 5)
    48  // 	    returns {"", "Game", "Of", "Thrones", ""}
    49  //  * VAlignBottom.ApplyStr("Game\nOf\nThrones", 5)
    50  // 	    returns {"", "", "Game", "Of", "Thrones"}
    51  func (va VAlign) ApplyStr(text string, maxLines int) []string {
    52  	return va.Apply(strings.Split(text, "\n"), maxLines)
    53  }
    54  
    55  // HTMLProperty returns the equivalent HTML vertical-align tag property.
    56  func (va VAlign) HTMLProperty() string {
    57  	switch va {
    58  	case VAlignTop:
    59  		return "valign=\"top\""
    60  	case VAlignMiddle:
    61  		return "valign=\"middle\""
    62  	case VAlignBottom:
    63  		return "valign=\"bottom\""
    64  	default:
    65  		return ""
    66  	}
    67  }
    68  

View as plain text