mirror of
https://github.com/charmbracelet/lipgloss.git
synced 2024-09-11 22:17:12 +03:00
Add forground options to whitespace generator
This commit is contained in:
parent
f24a617e3a
commit
1692c92c70
@ -19,24 +19,50 @@ func (w whitespace) render(width int) string {
|
||||
w.chars = " "
|
||||
}
|
||||
|
||||
charWidth := ansi.PrintableRuneWidth(w.chars)
|
||||
line := strings.Repeat(w.chars, width/charWidth)
|
||||
r := []rune(w.chars)
|
||||
j := 0
|
||||
b := strings.Builder{}
|
||||
|
||||
// Fill in gaps with spaces
|
||||
short := width - ansi.PrintableRuneWidth(line)
|
||||
if short > 0 {
|
||||
line += strings.Repeat(" ", short)
|
||||
// Cycle through runes and print them into the whitespace.
|
||||
for i := 0; i < width; {
|
||||
b.WriteRune(r[j])
|
||||
j++
|
||||
if j >= len(r) {
|
||||
j = 0
|
||||
}
|
||||
i += ansi.PrintableRuneWidth(string(r[j]))
|
||||
}
|
||||
|
||||
return w.style.Styled(line)
|
||||
// Fill any extra gaps white spaces. This might be necessary if any runes
|
||||
// are more than one cell wide, which could leave a one-rune gap.
|
||||
short := width - ansi.PrintableRuneWidth(b.String())
|
||||
if short > 0 {
|
||||
b.WriteString(strings.Repeat(" ", short))
|
||||
}
|
||||
|
||||
return w.style.Styled(b.String())
|
||||
}
|
||||
|
||||
// WhiteSpaceOption sets a styling rule for rendering whitespace.
|
||||
type WhitespaceOption func(*whitespace)
|
||||
|
||||
// WithWhitespaceForeground sets the color of the characters in the whitespace.
|
||||
func WithWhitespaceForeground(c ColorType) WhitespaceOption {
|
||||
return func(w *whitespace) {
|
||||
w.style = w.style.Foreground(c.color())
|
||||
}
|
||||
}
|
||||
|
||||
// WithWhiteSpaceBackground sets the background color of the whitespace.
|
||||
func WithWhitespaceBackground(c Color) WhitespaceOption {
|
||||
func WithWhitespaceBackground(c ColorType) WhitespaceOption {
|
||||
return func(w *whitespace) {
|
||||
w.style = w.style.Background(c.color())
|
||||
}
|
||||
}
|
||||
|
||||
// WithWhitespaceChars sets the characters to be rendered in the whitespace.
|
||||
func WithWhitespaceChars(s string) WhitespaceOption {
|
||||
return func(w *whitespace) {
|
||||
w.chars = s
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user