Giter Club home page Giter Club logo

Comments (4)

meowgorithm avatar meowgorithm commented on June 10, 2024 1

For now, I'd probably workaround this by simply placing the short column (or all columns) over the appropriate background color with lipgloss.Place (see example and docs).

Here’s how I'd fix my above example (in a real scenario I'd abstract the Place stuff and generalize it to work for all columns):

package main

import (
	"fmt"

	"github.com/charmbracelet/lipgloss"
)

func main() {
	const blue = lipgloss.Color("#0000FF")

	redStyle := lipgloss.NewStyle().
		Background(lipgloss.Color("#FF0000")).
		Width(10)

	greenStyle := lipgloss.NewStyle().
		Background(lipgloss.Color("#00FF00")).
		Width(10)

	outerStyle := lipgloss.NewStyle().
		Width(40).
		Background(blue)

	leftContent := greenStyle.Render("left text")
	middleContent := redStyle.Render("multi\nline\ncenter\ntext\nwow")
	rightContent := greenStyle.Render("right text")

	fmt.Println(outerStyle.Render(
		lipgloss.JoinHorizontal(
			lipgloss.Center,
			leftContent,
			middleContent,
			lipgloss.Place(
				lipgloss.Width(rightContent),            // width
				lipgloss.Height(middleContent),          // height
				lipgloss.Left,                           // x
				lipgloss.Center,                         // y
				rightContent,                            // content
				lipgloss.WithWhitespaceBackground(blue), // background
			),
		)))
}

Output:

image

from lipgloss.

drakenstar avatar drakenstar commented on June 10, 2024

Ok I dug into this and I think found the reason for this output. wordwrap.String called by Style.Render will not output spaces at the end of a line unless it finds a \n character before the limit. For example:

s := wordwrap.String("test  test  ", 6)
fmt.Print(s)
// "test\ntest"

s := wordwrap.String("test  \ntest  ", 6)
fmt.Print(s)
// "test  \ntest"

This is arguably surprising from their end, however lipgloss could workaround this by trimming whitespace this output if it is not styled by seeking backwards per line for \x1b[0m and trimming spaces after it.

I'm happy to PR this if there's agreement it should be fixed.

from lipgloss.

meowgorithm avatar meowgorithm commented on June 10, 2024

Okay yep, this is indeed a bug. I suspect the solution may be may complex than this, though we'll need to look into it further before we can have an opinion on the fix.

Here's a centered use case to further illustrate the issue.

image
package main

import (
	"fmt"

	"github.com/charmbracelet/lipgloss"
)

func main() {
	redStyle := lipgloss.NewStyle().
		Background(lipgloss.Color("#FF0000")).
		Width(10)

	greenStyle := lipgloss.NewStyle().
		Background(lipgloss.Color("#00FF00")).
		Width(10)

	outerStyle := lipgloss.NewStyle().
		Width(40).
		Background(lipgloss.Color("#0000FF"))

	fmt.Println(outerStyle.Render(lipgloss.JoinHorizontal(lipgloss.Center,
		greenStyle.Render("left text"),
		redStyle.Render("multi\nline\ncenter\ntext\nwow"),
		greenStyle.Render("right text"),
	)))
}

from lipgloss.

drakenstar avatar drakenstar commented on June 10, 2024

I've worked around this locally doing as I described above and trimming space characters from the end of a line if they are immediately preceded by a reset sequence.

However there's another harder case that workaround still doesn't solve:

redStyle := lipgloss.NewStyle().
		Background(lipgloss.Color("#FF0000")).
		Width(10)

	greenStyle := lipgloss.NewStyle().
		Background(lipgloss.Color("#00FF00")).
		Width(10)

	outerStyle := lipgloss.NewStyle().
		Width(40).
		Background(lipgloss.Color("#0000FF"))

	fmt.Println(outerStyle.Render(lipgloss.JoinHorizontal(0,
		redStyle.Render("multi\nline"),
		greenStyle.Render("left text"),
		redStyle.Render("multi\nline"),
	)))

Resulting in:

image

A more complete solve for this is probably inspecting each line for segments that have no styling any applying the current style to them.

from lipgloss.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. πŸ“ŠπŸ“ˆπŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.