formatLine formats the line according to the information passed.
(lines2 []expandedLine, longestLine, titleLen int, sideMargin, title string, texts []string)
| 147 | |
| 148 | // formatLine formats the line according to the information passed. |
| 149 | func (b *Box) formatLine(lines2 []expandedLine, longestLine, titleLen int, sideMargin, title string, texts []string) ([]string, error) { |
| 150 | for i, line := range lines2 { |
| 151 | length := line.len |
| 152 | |
| 153 | // Use later |
| 154 | var space, oddSpace string |
| 155 | |
| 156 | // compute stripped width once |
| 157 | strippedWidth := runewidth.StringWidth(ansi.Strip(line.line)) |
| 158 | if strippedWidth < runewidth.StringWidth(line.line) { |
| 159 | length = strippedWidth |
| 160 | } |
| 161 | |
| 162 | // If current text is shorter than the longest one |
| 163 | // center the text, so it looks better |
| 164 | if length < longestLine { |
| 165 | // Difference between longest and current one |
| 166 | diff := longestLine - length |
| 167 | |
| 168 | // the spaces to add on each side |
| 169 | toAdd := diff / 2 |
| 170 | space = strings.Repeat(" ", toAdd) |
| 171 | |
| 172 | // If difference between the longest and current one |
| 173 | // is odd, we have to add one additional space before the last vertical separator |
| 174 | if diff%2 != 0 { |
| 175 | oddSpace = " " |
| 176 | } |
| 177 | } |
| 178 | |
| 179 | spacing := space + sideMargin |
| 180 | var format AlignType |
| 181 | |
| 182 | switch { |
| 183 | case i < titleLen && title != "" && b.titlePos == Inside: |
| 184 | format = centerAlign |
| 185 | default: |
| 186 | align, err := b.findAlign() |
| 187 | if err != nil { |
| 188 | return nil, err |
| 189 | } |
| 190 | format = AlignType(align) |
| 191 | } |
| 192 | |
| 193 | sep, err := applyColor(b.vertical, b.color) |
| 194 | if err != nil { |
| 195 | return nil, err |
| 196 | } |
| 197 | |
| 198 | formatted := fmt.Sprintf(string(format), sep, spacing, line.line, oddSpace, space, sideMargin) |
| 199 | texts = append(texts, formatted) |
| 200 | } |
| 201 | return texts, nil |
| 202 | } |
| 203 | |
| 204 | func (b *Box) findAlign() (string, error) { |
| 205 | switch b.contentAlign { |