mirror of
https://github.com/charmbracelet/lipgloss.git
synced 2024-08-17 18:00:46 +03:00
fix(whitespace): use position renderer
This commit is contained in:
parent
3fe14d0547
commit
8565b7428f
28
position.go
28
position.go
@ -34,13 +34,26 @@ const (
|
||||
// Place places a string or text block vertically in an unstyled box of a given
|
||||
// width or height.
|
||||
func Place(width, height int, hPos, vPos Position, str string, opts ...WhitespaceOption) string {
|
||||
return PlaceVertical(height, vPos, PlaceHorizontal(width, hPos, str, opts...), opts...)
|
||||
return renderer.Place(width, height, hPos, vPos, str, opts...)
|
||||
}
|
||||
|
||||
// Place places a string or text block vertically in an unstyled box of a given
|
||||
// width or height.
|
||||
func (r *Renderer) Place(width, height int, hPos, vPos Position, str string, opts ...WhitespaceOption) string {
|
||||
return r.PlaceVertical(height, vPos, r.PlaceHorizontal(width, hPos, str, opts...), opts...)
|
||||
}
|
||||
|
||||
// PlaceHorizontal places a string or text block horizontally in an unstyled
|
||||
// block of a given width. If the given width is shorter than the max width of
|
||||
// the string (measured by its longest line) this will be a noop.
|
||||
func PlaceHorizontal(width int, pos Position, str string, opts ...WhitespaceOption) string {
|
||||
return renderer.PlaceHorizontal(width, pos, str, opts...)
|
||||
}
|
||||
|
||||
// PlaceHorizontal places a string or text block horizontally in an unstyled
|
||||
// block of a given width. If the given width is shorter than the max width of
|
||||
// the string (measured by it's longest line) this will be a noöp.
|
||||
func (r *Renderer) PlaceHorizontal(width int, pos Position, str string, opts ...WhitespaceOption) string {
|
||||
lines, contentWidth := getLines(str)
|
||||
gap := width - contentWidth
|
||||
|
||||
@ -48,7 +61,8 @@ func PlaceHorizontal(width int, pos Position, str string, opts ...WhitespaceOpti
|
||||
return str
|
||||
}
|
||||
|
||||
ws := NewWhitespace(opts...)
|
||||
ws := newWhitespace(r, opts...)
|
||||
|
||||
var b strings.Builder
|
||||
for i, l := range lines {
|
||||
// Is this line shorter than the longest line?
|
||||
@ -87,6 +101,13 @@ func PlaceHorizontal(width int, pos Position, str string, opts ...WhitespaceOpti
|
||||
// of a given height. If the given height is shorter than the height of the
|
||||
// string (measured by its newlines) then this will be a noop.
|
||||
func PlaceVertical(height int, pos Position, str string, opts ...WhitespaceOption) string {
|
||||
return renderer.PlaceVertical(height, pos, str, opts...)
|
||||
}
|
||||
|
||||
// PlaceVertical places a string or text block vertically in an unstyled block
|
||||
// of a given height. If the given height is shorter than the height of the
|
||||
// string (measured by it's newlines) then this will be a noöp.
|
||||
func (r *Renderer) PlaceVertical(height int, pos Position, str string, opts ...WhitespaceOption) string {
|
||||
contentHeight := strings.Count(str, "\n") + 1
|
||||
gap := height - contentHeight
|
||||
|
||||
@ -94,7 +115,8 @@ func PlaceVertical(height int, pos Position, str string, opts ...WhitespaceOptio
|
||||
return str
|
||||
}
|
||||
|
||||
ws := NewWhitespace(opts...)
|
||||
ws := newWhitespace(r, opts...)
|
||||
|
||||
_, width := getLines(str)
|
||||
emptyLine := ws.render(width)
|
||||
b := strings.Builder{}
|
||||
|
@ -7,18 +7,18 @@ import (
|
||||
"github.com/muesli/termenv"
|
||||
)
|
||||
|
||||
// Whitespace is a whitespace renderer.
|
||||
type Whitespace struct {
|
||||
// whitespace is a whitespace renderer.
|
||||
type whitespace struct {
|
||||
re *Renderer
|
||||
style termenv.Style
|
||||
chars string
|
||||
}
|
||||
|
||||
// NewWhitespace creates a new whitespace renderer. The order of the options
|
||||
// newWhitespace creates a new whitespace renderer. The order of the options
|
||||
// matters, it you'r using WithWhitespaceRenderer, make sure it comes first as
|
||||
// other options might depend on it.
|
||||
func NewWhitespace(opts ...WhitespaceOption) *Whitespace {
|
||||
w := &Whitespace{re: renderer}
|
||||
func newWhitespace(r *Renderer, opts ...WhitespaceOption) *whitespace {
|
||||
w := &whitespace{re: r}
|
||||
for _, opt := range opts {
|
||||
opt(w)
|
||||
}
|
||||
@ -26,7 +26,7 @@ func NewWhitespace(opts ...WhitespaceOption) *Whitespace {
|
||||
}
|
||||
|
||||
// Render whitespaces.
|
||||
func (w Whitespace) render(width int) string {
|
||||
func (w whitespace) render(width int) string {
|
||||
if w.chars == "" {
|
||||
w.chars = " "
|
||||
}
|
||||
@ -56,32 +56,25 @@ func (w Whitespace) render(width int) string {
|
||||
}
|
||||
|
||||
// WhitespaceOption sets a styling rule for rendering whitespace.
|
||||
type WhitespaceOption func(*Whitespace)
|
||||
|
||||
// WithWhitespaceRenderer sets the lipgloss renderer to be used for rendering.
|
||||
func WithWhitespaceRenderer(r *Renderer) WhitespaceOption {
|
||||
return func(w *Whitespace) {
|
||||
w.re = r
|
||||
}
|
||||
}
|
||||
type WhitespaceOption func(*whitespace)
|
||||
|
||||
// WithWhitespaceForeground sets the color of the characters in the whitespace.
|
||||
func WithWhitespaceForeground(c TerminalColor) WhitespaceOption {
|
||||
return func(w *Whitespace) {
|
||||
return func(w *whitespace) {
|
||||
w.style = w.style.Foreground(c.color(w.re))
|
||||
}
|
||||
}
|
||||
|
||||
// WithWhitespaceBackground sets the background color of the whitespace.
|
||||
func WithWhitespaceBackground(c TerminalColor) WhitespaceOption {
|
||||
return func(w *Whitespace) {
|
||||
return func(w *whitespace) {
|
||||
w.style = w.style.Background(c.color(w.re))
|
||||
}
|
||||
}
|
||||
|
||||
// WithWhitespaceChars sets the characters to be rendered in the whitespace.
|
||||
func WithWhitespaceChars(s string) WhitespaceOption {
|
||||
return func(w *Whitespace) {
|
||||
return func(w *whitespace) {
|
||||
w.chars = s
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user