mirror of
https://github.com/charmbracelet/lipgloss.git
synced 2024-10-26 22:57:49 +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
|
// Place places a string or text block vertically in an unstyled box of a given
|
||||||
// width or height.
|
// width or height.
|
||||||
func Place(width, height int, hPos, vPos Position, str string, opts ...WhitespaceOption) string {
|
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
|
// 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
|
// 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.
|
// the string (measured by its longest line) this will be a noop.
|
||||||
func PlaceHorizontal(width int, pos Position, str string, opts ...WhitespaceOption) string {
|
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)
|
lines, contentWidth := getLines(str)
|
||||||
gap := width - contentWidth
|
gap := width - contentWidth
|
||||||
|
|
||||||
@ -48,7 +61,8 @@ func PlaceHorizontal(width int, pos Position, str string, opts ...WhitespaceOpti
|
|||||||
return str
|
return str
|
||||||
}
|
}
|
||||||
|
|
||||||
ws := NewWhitespace(opts...)
|
ws := newWhitespace(r, opts...)
|
||||||
|
|
||||||
var b strings.Builder
|
var b strings.Builder
|
||||||
for i, l := range lines {
|
for i, l := range lines {
|
||||||
// Is this line shorter than the longest line?
|
// 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
|
// 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.
|
// string (measured by its newlines) then this will be a noop.
|
||||||
func PlaceVertical(height int, pos Position, str string, opts ...WhitespaceOption) string {
|
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
|
contentHeight := strings.Count(str, "\n") + 1
|
||||||
gap := height - contentHeight
|
gap := height - contentHeight
|
||||||
|
|
||||||
@ -94,7 +115,8 @@ func PlaceVertical(height int, pos Position, str string, opts ...WhitespaceOptio
|
|||||||
return str
|
return str
|
||||||
}
|
}
|
||||||
|
|
||||||
ws := NewWhitespace(opts...)
|
ws := newWhitespace(r, opts...)
|
||||||
|
|
||||||
_, width := getLines(str)
|
_, width := getLines(str)
|
||||||
emptyLine := ws.render(width)
|
emptyLine := ws.render(width)
|
||||||
b := strings.Builder{}
|
b := strings.Builder{}
|
||||||
|
@ -7,18 +7,18 @@ import (
|
|||||||
"github.com/muesli/termenv"
|
"github.com/muesli/termenv"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Whitespace is a whitespace renderer.
|
// whitespace is a whitespace renderer.
|
||||||
type Whitespace struct {
|
type whitespace struct {
|
||||||
re *Renderer
|
re *Renderer
|
||||||
style termenv.Style
|
style termenv.Style
|
||||||
chars string
|
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
|
// matters, it you'r using WithWhitespaceRenderer, make sure it comes first as
|
||||||
// other options might depend on it.
|
// other options might depend on it.
|
||||||
func NewWhitespace(opts ...WhitespaceOption) *Whitespace {
|
func newWhitespace(r *Renderer, opts ...WhitespaceOption) *whitespace {
|
||||||
w := &Whitespace{re: renderer}
|
w := &whitespace{re: r}
|
||||||
for _, opt := range opts {
|
for _, opt := range opts {
|
||||||
opt(w)
|
opt(w)
|
||||||
}
|
}
|
||||||
@ -26,7 +26,7 @@ func NewWhitespace(opts ...WhitespaceOption) *Whitespace {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Render whitespaces.
|
// Render whitespaces.
|
||||||
func (w Whitespace) render(width int) string {
|
func (w whitespace) render(width int) string {
|
||||||
if w.chars == "" {
|
if w.chars == "" {
|
||||||
w.chars = " "
|
w.chars = " "
|
||||||
}
|
}
|
||||||
@ -56,32 +56,25 @@ func (w Whitespace) render(width int) string {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// WhitespaceOption sets a styling rule for rendering whitespace.
|
// WhitespaceOption sets a styling rule for rendering whitespace.
|
||||||
type WhitespaceOption func(*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
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// WithWhitespaceForeground sets the color of the characters in the whitespace.
|
// WithWhitespaceForeground sets the color of the characters in the whitespace.
|
||||||
func WithWhitespaceForeground(c TerminalColor) WhitespaceOption {
|
func WithWhitespaceForeground(c TerminalColor) WhitespaceOption {
|
||||||
return func(w *Whitespace) {
|
return func(w *whitespace) {
|
||||||
w.style = w.style.Foreground(c.color(w.re))
|
w.style = w.style.Foreground(c.color(w.re))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// WithWhitespaceBackground sets the background color of the whitespace.
|
// WithWhitespaceBackground sets the background color of the whitespace.
|
||||||
func WithWhitespaceBackground(c TerminalColor) WhitespaceOption {
|
func WithWhitespaceBackground(c TerminalColor) WhitespaceOption {
|
||||||
return func(w *Whitespace) {
|
return func(w *whitespace) {
|
||||||
w.style = w.style.Background(c.color(w.re))
|
w.style = w.style.Background(c.color(w.re))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// WithWhitespaceChars sets the characters to be rendered in the whitespace.
|
// WithWhitespaceChars sets the characters to be rendered in the whitespace.
|
||||||
func WithWhitespaceChars(s string) WhitespaceOption {
|
func WithWhitespaceChars(s string) WhitespaceOption {
|
||||||
return func(w *Whitespace) {
|
return func(w *whitespace) {
|
||||||
w.chars = s
|
w.chars = s
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user