mirror of
https://github.com/anotherhadi/spilltea.git
synced 2026-05-20 09:42:34 +02:00
Center lines for asciimoji+text
Signed-off-by: Hadi <112569860+anotherhadi@users.noreply.github.com>
This commit is contained in:
+20
-1
@@ -1,6 +1,10 @@
|
||||
package util
|
||||
|
||||
import "strings"
|
||||
import (
|
||||
"strings"
|
||||
|
||||
"charm.land/lipgloss/v2"
|
||||
)
|
||||
|
||||
func Truncate(s string, max int) string {
|
||||
if len(s) <= max {
|
||||
@@ -9,6 +13,21 @@ func Truncate(s string, max int) string {
|
||||
return s[:max-1] + "…"
|
||||
}
|
||||
|
||||
// CenterLines centers each line horizontally relative to the longest one.
|
||||
func CenterLines(lines ...string) string {
|
||||
maxWidth := 0
|
||||
for _, l := range lines {
|
||||
if w := lipgloss.Width(l); w > maxWidth {
|
||||
maxWidth = w
|
||||
}
|
||||
}
|
||||
centered := make([]string, len(lines))
|
||||
for i, l := range lines {
|
||||
centered[i] = lipgloss.PlaceHorizontal(maxWidth, lipgloss.Center, l)
|
||||
}
|
||||
return strings.Join(centered, "\n")
|
||||
}
|
||||
|
||||
// InferScheme returns "http" for port 80, "https" otherwise.
|
||||
func InferScheme(host string) string {
|
||||
if strings.HasSuffix(host, ":80") {
|
||||
|
||||
Reference in New Issue
Block a user