-
Hello - I'm having a hard time figuring out from the documentation how to position multiple components/elements within the terminal. More specifically, I'm trying to create a background component in my Bubble Tea / Lip Gloss app, which should show up behind everything else rendered with Lip Gloss. I'm also trying to figure out how to position other components in relation to each other. Here's a simplified example... Assume we have a 10x6 terminal, with the following components to be rendered using Lip Gloss:
In the final layout, all three components should be rendered, with the text component after the image component. So it should look like so:
How would I go about doing this using Lip Gloss? |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 2 replies
-
Here is my (perhaps not the most optimal?) solution. package main
import (
"fmt"
"os"
"strings"
"github.com/charmbracelet/lipgloss"
"golang.org/x/term"
)
var (
docStyle = lipgloss.NewStyle()
subtle = lipgloss.AdaptiveColor{Light: "#D9DCCF", Dark: "#383838"}
)
func main() {
width, height, _ := term.GetSize(int(os.Stdout.Fd())) // gets the width and height of the terminal
// our string builder which will save the final string to render
doc := strings.Builder{}
// replace the text and image per your requirements.
text := lipgloss.NewStyle().Width(50).Align(lipgloss.Center).Render("Hello!")
image := "/\\\n\\/"
ui := lipgloss.JoinVertical(lipgloss.Center, image, text)
backgroundDot := docStyle.Copy().Foreground(subtle).Render(".")
screen := lipgloss.Place(width, height,
lipgloss.Center, lipgloss.Center,
strings.ReplaceAll(ui, " ", backgroundDot), // we fill in the whitespace with dots to blend into the bg!
lipgloss.WithWhitespaceChars("."),
lipgloss.WithWhitespaceForeground(subtle),
)
doc.WriteString(screen + "\n\n")
if width > 0 {
docStyle = docStyle.MaxWidth(width)
}
fmt.Println(docStyle.Render(doc.String()))
} |
Beta Was this translation helpful? Give feedback.
-
I just found out about #65, and the pull request there is exactly what I need for overlaying the main UI on top of the background. Hopefully they can get that merged soon, as it would be an extremely useful feature to have in Lip Gloss. Apart from that bit of information, the replies here from @Gaurav-Gosain helped answer other parts of my question - thank you! I'm going to go ahead and mark this as resolved. |
Beta Was this translation helpful? Give feedback.
I just found out about #65, and the pull request there is exactly what I need for overlaying the main UI on top of the background. Hopefully they can get that merged soon, as it would be an extremely useful feature to have in Lip Gloss.
Apart from that bit of information, the replies here from @Gaurav-Gosain helped answer other parts of my question - thank you! I'm going to go ahead and mark this as resolved.