mirror of
https://github.com/charmbracelet/lipgloss.git
synced 2024-10-26 14:48:54 +03:00
Pad short lines when horizontally positioning
This commit is contained in:
parent
5ec7baa707
commit
f24a617e3a
18
position.go
18
position.go
@ -3,6 +3,8 @@ package lipgloss
|
||||
import (
|
||||
"math"
|
||||
"strings"
|
||||
|
||||
"github.com/muesli/reflow/ansi"
|
||||
)
|
||||
|
||||
// Position represents a position along a horizontal or vertical axis. It's in
|
||||
@ -53,20 +55,26 @@ func PlaceHorizontal(width int, pos Position, str string, opts ...WhitespaceOpti
|
||||
|
||||
var b strings.Builder
|
||||
for i, l := range lines {
|
||||
|
||||
// Is this line shorter than the longest line?
|
||||
short := max(0, contentWidth-ansi.PrintableRuneWidth(l))
|
||||
|
||||
switch pos {
|
||||
|
||||
case Left:
|
||||
b.WriteString(l)
|
||||
b.WriteString(ws.render(gap))
|
||||
b.WriteString(ws.render(gap + short))
|
||||
|
||||
case Right:
|
||||
b.WriteString(ws.render(gap))
|
||||
b.WriteString(ws.render(gap + short))
|
||||
b.WriteString(l)
|
||||
|
||||
default: // somewhere in the middle
|
||||
split := int(math.Round(float64(gap) * pos.value()))
|
||||
left := gap - split
|
||||
right := gap - left
|
||||
totalGap := gap + short
|
||||
|
||||
split := int(math.Round(float64(totalGap) * pos.value()))
|
||||
left := totalGap - split
|
||||
right := totalGap - left
|
||||
|
||||
b.WriteString(ws.render(left))
|
||||
b.WriteString(l)
|
||||
|
Loading…
Reference in New Issue
Block a user