fix(whitespace): use position renderer

This commit is contained in:
Ayman Bagabas 2022-10-04 22:10:54 -04:00
parent 3fe14d0547
commit 8565b7428f
2 changed files with 35 additions and 20 deletions

View File

@ -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{}

View File

@ -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
}
}