2021-03-17 22:33:13 +03:00
|
|
|
package lipgloss
|
|
|
|
|
|
|
|
import (
|
2021-03-18 05:09:51 +03:00
|
|
|
"math"
|
2021-03-17 22:33:13 +03:00
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/muesli/reflow/ansi"
|
|
|
|
)
|
|
|
|
|
2021-03-18 03:28:59 +03:00
|
|
|
// JoinHorizontal is a utility function for horizontally joining two
|
2021-03-18 05:09:51 +03:00
|
|
|
// potentially multi-lined strings along a vertical axis. The first argument is
|
2021-03-18 23:59:48 +03:00
|
|
|
// the position, with 0 being all the way at the top and 1 being all the way
|
2021-03-18 05:09:51 +03:00
|
|
|
// at the bottom.
|
|
|
|
//
|
2024-03-14 18:00:06 +03:00
|
|
|
// If you just want to align to the top, center or bottom you may as well just
|
2021-04-06 09:00:27 +03:00
|
|
|
// use the helper constants Top, Center, and Bottom.
|
2021-03-17 22:33:13 +03:00
|
|
|
//
|
|
|
|
// Example:
|
|
|
|
//
|
2022-08-24 19:05:18 +03:00
|
|
|
// blockB := "...\n...\n..."
|
|
|
|
// blockA := "...\n...\n...\n...\n..."
|
2021-03-17 22:33:13 +03:00
|
|
|
//
|
2022-08-24 19:05:18 +03:00
|
|
|
// // Join 20% from the top
|
|
|
|
// str := lipgloss.JoinHorizontal(0.2, blockA, blockB)
|
2021-03-18 20:58:27 +03:00
|
|
|
//
|
2022-08-24 19:05:18 +03:00
|
|
|
// // Join on the top edge
|
|
|
|
// str := lipgloss.JoinHorizontal(lipgloss.Top, blockA, blockB)
|
2021-03-26 20:35:48 +03:00
|
|
|
func JoinHorizontal(pos Position, strs ...string) string {
|
2021-03-17 23:20:24 +03:00
|
|
|
if len(strs) == 0 {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
if len(strs) == 1 {
|
2021-03-25 04:20:38 +03:00
|
|
|
return strs[0]
|
2021-03-17 23:20:24 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
|
|
|
// Groups of strings broken into multiple lines
|
|
|
|
blocks = make([][]string, len(strs))
|
|
|
|
|
|
|
|
// Max line widths for the above text blocks
|
|
|
|
maxWidths = make([]int, len(strs))
|
|
|
|
|
|
|
|
// Height of the tallest block
|
|
|
|
maxHeight int
|
|
|
|
)
|
|
|
|
|
|
|
|
// Break text blocks into lines and get max widths for each text block
|
|
|
|
for i, str := range strs {
|
|
|
|
blocks[i], maxWidths[i] = getLines(str)
|
|
|
|
if len(blocks[i]) > maxHeight {
|
|
|
|
maxHeight = len(blocks[i])
|
|
|
|
}
|
|
|
|
}
|
2021-03-17 22:33:13 +03:00
|
|
|
|
|
|
|
// Add extra lines to make each side the same height
|
2021-03-17 23:20:24 +03:00
|
|
|
for i := range blocks {
|
|
|
|
if len(blocks[i]) >= maxHeight {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
extraLines := make([]string, maxHeight-len(blocks[i]))
|
2021-03-17 22:33:13 +03:00
|
|
|
|
2023-07-24 18:50:04 +03:00
|
|
|
switch pos { //nolint:exhaustive
|
2021-03-26 20:35:48 +03:00
|
|
|
case Top:
|
2021-03-18 05:09:51 +03:00
|
|
|
blocks[i] = append(blocks[i], extraLines...)
|
2021-03-17 22:33:13 +03:00
|
|
|
|
2021-03-26 20:35:48 +03:00
|
|
|
case Bottom:
|
2021-03-17 23:20:24 +03:00
|
|
|
blocks[i] = append(extraLines, blocks[i]...)
|
2021-03-17 22:33:13 +03:00
|
|
|
|
2021-03-18 05:09:51 +03:00
|
|
|
default: // Somewhere in the middle
|
|
|
|
n := len(extraLines)
|
2021-03-26 20:35:48 +03:00
|
|
|
split := int(math.Round(float64(n) * pos.value()))
|
2021-03-18 05:09:51 +03:00
|
|
|
top := n - split
|
|
|
|
bottom := n - top
|
|
|
|
|
|
|
|
blocks[i] = append(extraLines[top:], blocks[i]...)
|
|
|
|
blocks[i] = append(blocks[i], extraLines[bottom:]...)
|
2021-03-17 22:33:13 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Merge lines
|
|
|
|
var b strings.Builder
|
2021-03-17 23:20:24 +03:00
|
|
|
for i := range blocks[0] { // remember, all blocks have the same number of members now
|
|
|
|
for j, block := range blocks {
|
|
|
|
b.WriteString(block[i])
|
2021-03-25 04:20:38 +03:00
|
|
|
|
|
|
|
// Also make lines the same length
|
2021-03-17 23:20:24 +03:00
|
|
|
b.WriteString(strings.Repeat(" ", maxWidths[j]-ansi.PrintableRuneWidth(block[i])))
|
2021-03-17 22:33:13 +03:00
|
|
|
}
|
2021-03-26 02:05:54 +03:00
|
|
|
if i < len(blocks[0])-1 {
|
|
|
|
b.WriteRune('\n')
|
|
|
|
}
|
2021-03-17 22:33:13 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return b.String()
|
|
|
|
}
|
|
|
|
|
2021-03-18 05:09:51 +03:00
|
|
|
// JoinVertical is a utility function for vertically joining two potentially
|
|
|
|
// multi-lined strings along a horizontal axis. The first argument is the
|
2021-03-18 23:59:48 +03:00
|
|
|
// position, with 0 being all the way to the left and 1 being all the way to
|
2021-03-18 05:09:51 +03:00
|
|
|
// the right.
|
|
|
|
//
|
|
|
|
// If you just want to align to the left, right or center you may as well just
|
2021-04-06 09:00:27 +03:00
|
|
|
// use the helper constants Left, Center, and Right.
|
2021-03-18 05:09:51 +03:00
|
|
|
//
|
|
|
|
// Example:
|
|
|
|
//
|
2022-08-24 19:05:18 +03:00
|
|
|
// blockB := "...\n...\n..."
|
|
|
|
// blockA := "...\n...\n...\n...\n..."
|
2021-03-18 20:58:27 +03:00
|
|
|
//
|
2022-08-24 19:05:18 +03:00
|
|
|
// // Join 20% from the top
|
|
|
|
// str := lipgloss.JoinVertical(0.2, blockA, blockB)
|
2021-03-18 05:09:51 +03:00
|
|
|
//
|
2022-08-24 19:05:18 +03:00
|
|
|
// // Join on the right edge
|
|
|
|
// str := lipgloss.JoinVertical(lipgloss.Right, blockA, blockB)
|
2021-03-26 20:35:48 +03:00
|
|
|
func JoinVertical(pos Position, strs ...string) string {
|
2021-03-18 03:28:59 +03:00
|
|
|
if len(strs) == 0 {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
if len(strs) == 1 {
|
2021-03-25 04:20:38 +03:00
|
|
|
return strs[0]
|
2021-03-18 03:28:59 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
|
|
|
blocks = make([][]string, len(strs))
|
|
|
|
maxWidth int
|
|
|
|
)
|
|
|
|
|
|
|
|
for i := range strs {
|
|
|
|
var w int
|
|
|
|
blocks[i], w = getLines(strs[i])
|
|
|
|
if w > maxWidth {
|
|
|
|
maxWidth = w
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var b strings.Builder
|
|
|
|
for i, block := range blocks {
|
|
|
|
for j, line := range block {
|
|
|
|
w := maxWidth - ansi.PrintableRuneWidth(line)
|
|
|
|
|
2023-07-24 18:50:04 +03:00
|
|
|
switch pos { //nolint:exhaustive
|
2021-03-26 20:35:48 +03:00
|
|
|
case Left:
|
2021-03-18 05:09:51 +03:00
|
|
|
b.WriteString(line)
|
|
|
|
b.WriteString(strings.Repeat(" ", w))
|
|
|
|
|
2021-03-26 20:35:48 +03:00
|
|
|
case Right:
|
2021-03-18 05:09:51 +03:00
|
|
|
b.WriteString(strings.Repeat(" ", w))
|
|
|
|
b.WriteString(line)
|
|
|
|
|
|
|
|
default: // Somewhere in the middle
|
2021-03-18 03:28:59 +03:00
|
|
|
if w < 1 {
|
|
|
|
b.WriteString(line)
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
2021-03-26 20:35:48 +03:00
|
|
|
split := int(math.Round(float64(w) * pos.value()))
|
2021-11-22 15:17:10 +03:00
|
|
|
right := w - split
|
|
|
|
left := w - right
|
2021-03-18 05:09:51 +03:00
|
|
|
|
2021-03-18 20:58:27 +03:00
|
|
|
b.WriteString(strings.Repeat(" ", left))
|
2021-03-18 03:28:59 +03:00
|
|
|
b.WriteString(line)
|
2021-03-18 20:58:27 +03:00
|
|
|
b.WriteString(strings.Repeat(" ", right))
|
2021-03-18 03:28:59 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Write a newline as long as we're not on the last line of the
|
|
|
|
// last block.
|
|
|
|
if !(i == len(blocks)-1 && j == len(block)-1) {
|
|
|
|
b.WriteRune('\n')
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return b.String()
|
|
|
|
}
|