package lipgloss
import (
)
const (
NBSP = '\u00A0'
tabWidthDefault = 4
)
type propKey int64
const (
boldKey propKey = 1 << iota
italicKey
strikethroughKey
reverseKey
blinkKey
faintKey
underlineSpacesKey
strikethroughSpacesKey
colorWhitespaceKey
underlineKey
foregroundKey
backgroundKey
underlineColorKey
widthKey
heightKey
alignHorizontalKey
alignVerticalKey
paddingTopKey
paddingRightKey
paddingBottomKey
paddingLeftKey
paddingCharKey
marginTopKey
marginRightKey
marginBottomKey
marginLeftKey
marginBackgroundKey
marginCharKey
borderStyleKey
borderTopKey
borderRightKey
borderBottomKey
borderLeftKey
borderTopForegroundKey
borderRightForegroundKey
borderBottomForegroundKey
borderLeftForegroundKey
borderForegroundBlendKey
borderForegroundBlendOffsetKey
borderTopBackgroundKey
borderRightBackgroundKey
borderBottomBackgroundKey
borderLeftBackgroundKey
inlineKey
maxWidthKey
maxHeightKey
tabWidthKey
transformKey
linkKey
linkParamsKey
)
type props int64
func ( props) ( propKey) props {
return | props()
}
func ( props) ( propKey) props {
return &^ props()
}
func ( props) ( propKey) bool {
return &props() != 0
}
type Underline = ansi.Underline
const (
UnderlineNone = ansi.UnderlineNone
UnderlineSingle = ansi.UnderlineSingle
UnderlineDouble = ansi.UnderlineDouble
UnderlineCurly = ansi.UnderlineCurly
UnderlineDotted = ansi.UnderlineDotted
UnderlineDashed = ansi.UnderlineDashed
)
func () Style {
return Style{}
}
type Style struct {
props props
value string
link, linkParams string
attrs int64
fgColor color.Color
bgColor color.Color
ulColor color.Color
ul Underline
width int
height int
alignHorizontal Position
alignVertical Position
paddingTop int
paddingRight int
paddingBottom int
paddingLeft int
paddingChar rune
marginTop int
marginRight int
marginBottom int
marginLeft int
marginBgColor color.Color
marginChar rune
borderStyle Border
borderTopFgColor color.Color
borderRightFgColor color.Color
borderBottomFgColor color.Color
borderLeftFgColor color.Color
borderBlendFgColor []color.Color
borderForegroundBlendOffset int
borderTopBgColor color.Color
borderRightBgColor color.Color
borderBottomBgColor color.Color
borderLeftBgColor color.Color
maxWidth int
maxHeight int
tabWidth int
transform func(string) string
}
func ( ...string) string {
return strings.Join(, " ")
}
func ( Style) ( ...string) Style {
.value = joinString(...)
return
}
func ( Style) () string {
return .value
}
func ( Style) () string {
return .Render()
}
func ( Style) () Style {
return
}
func ( Style) ( Style) Style {
for := boldKey; <= transformKey; <<= 1 {
if !.isSet() {
continue
}
switch {
case marginTopKey, marginRightKey, marginBottomKey, marginLeftKey:
continue
case paddingTopKey, paddingRightKey, paddingBottomKey, paddingLeftKey:
continue
case backgroundKey:
if !.isSet(marginBackgroundKey) && !.isSet(marginBackgroundKey) {
.set(marginBackgroundKey, .bgColor)
}
}
if .isSet() {
continue
}
.setFrom(, )
}
return
}
func ( Style) ( ...string) string {
if .value != "" {
= append([]string{.value}, ...)
}
var (
= joinString(...)
ansi.Style
ansi.Style
ansi.Style
= .getAsBool(boldKey, false)
= .getAsBool(italicKey, false)
= .getAsBool(strikethroughKey, false)
= .getAsBool(reverseKey, false)
= .getAsBool(blinkKey, false)
= .getAsBool(faintKey, false)
= .getAsColor(foregroundKey)
= .getAsColor(backgroundKey)
= .getAsColor(underlineColorKey)
= .ul != UnderlineNone
= .getAsInt(widthKey)
= .getAsInt(heightKey)
= .getAsPosition(alignHorizontalKey)
= .getAsPosition(alignVerticalKey)
= .getAsInt(paddingTopKey)
= .getAsInt(paddingRightKey)
= .getAsInt(paddingBottomKey)
= .getAsInt(paddingLeftKey)
= .GetHorizontalBorderSize()
= .GetVerticalBorderSize()
= .getAsBool(colorWhitespaceKey, true)
= .getAsBool(inlineKey, false)
= .getAsInt(maxWidthKey)
= .getAsInt(maxHeightKey)
= .getAsBool(underlineSpacesKey, false) || ( && .getAsBool(underlineSpacesKey, true))
= .getAsBool(strikethroughSpacesKey, false) || ( && .getAsBool(strikethroughSpacesKey, true))
=
= ( && !) || ( && !) || ||
= .getAsTransform(transformKey)
, = .GetHyperlink()
)
if != nil {
= ()
}
if .props == 0 {
return .maybeConvertTabs()
}
if {
= .Bold()
}
if {
= .Italic(true)
}
if {
= .Underline(true)
}
if {
= .Reverse(true)
= .Reverse(true)
}
if {
= .Blink(true)
}
if {
= .Faint()
}
if != noColor {
= .ForegroundColor()
if {
= .ForegroundColor()
}
if {
= .ForegroundColor()
}
}
if != noColor {
= .BackgroundColor()
if {
= .BackgroundColor()
}
if {
= .BackgroundColor()
}
}
if != noColor {
= .UnderlineColor()
if {
= .UnderlineColor()
}
if {
= .UnderlineColor()
}
}
if {
= .UnderlineStyle(.ul)
}
if {
= .Strikethrough(true)
}
if {
= .Underline(true)
}
if {
= .Strikethrough(true)
}
= .maybeConvertTabs()
= strings.ReplaceAll(, "\r\n", "\n")
if {
= strings.ReplaceAll(, "\n", " ")
}
-=
-=
if ! && > 0 {
:= - -
= Wrap(, , "")
}
{
var strings.Builder
:= true
for := range strings.SplitSeq(, "\n") {
if {
= false
} else {
.WriteRune('\n')
}
if {
var byte
for len() > 0 {
, , , := ansi.DecodeSequence(, , nil)
if == 0 {
break
}
switch {
case == 0:
.WriteString()
case isSpaceSequence():
.WriteString(.Styled())
default:
.WriteString(.Styled())
}
= [:]
=
}
} else {
.WriteString(.Styled())
}
}
= .String()
if len() > 0 {
= ansi.SetHyperlink(, ) + + ansi.ResetHyperlink()
}
}
if ! {
:= .paddingChar
if == 0 {
= ' '
}
if > 0 {
var *ansi.Style
if || {
= &
}
= padLeft(, , , )
}
if > 0 {
var *ansi.Style
if || {
= &
}
= padRight(, , , )
}
if > 0 {
= strings.Repeat("\n", ) +
}
if > 0 {
+= strings.Repeat("\n", )
}
}
if > 0 {
= alignTextVertical(, , , nil)
}
{
:= strings.Count(, "\n")
if != 0 || != 0 {
var *ansi.Style
if || {
= &
}
= alignTextHorizontal(, , , )
}
}
if ! {
= .applyBorder()
= .applyMargins(, )
}
if > 0 {
:= strings.Split(, "\n")
for := range {
[] = ansi.Truncate([], , "")
}
= strings.Join(, "\n")
}
if > 0 {
:= strings.Split(, "\n")
:= min(, len())
if len() > 0 {
= strings.Join([:], "\n")
}
}
return
}
func ( string) bool {
for , := range {
if !unicode.IsSpace() {
return false
}
}
return != ""
}
func ( Style) ( string) string {
:= tabWidthDefault
if .isSet(tabWidthKey) {
= .getAsInt(tabWidthKey)
}
switch {
case -1:
return
case 0:
return strings.ReplaceAll(, "\t", "")
default:
return strings.ReplaceAll(, "\t", strings.Repeat(" ", ))
}
}
func ( Style) ( string, bool) string {
var (
= .getAsInt(marginTopKey)
= .getAsInt(marginRightKey)
= .getAsInt(marginBottomKey)
= .getAsInt(marginLeftKey)
ansi.Style
)
:= .getAsColor(marginBackgroundKey)
if != noColor {
= .BackgroundColor()
}
:= .marginChar
if == 0 {
= ' '
}
= padLeft(, , &, )
= padRight(, , &, )
if ! {
, := getLines()
:= strings.Repeat(" ", )
if > 0 {
:= .Styled()
= strings.Repeat(+"\n", ) +
}
if > 0 {
:= .Styled()
+= strings.Repeat("\n"+, )
}
}
return
}
func ( string, int, *ansi.Style, rune) string {
return pad(, -, , )
}
func ( string, int, *ansi.Style, rune) string {
return pad(, , , )
}
func ( string, int, *ansi.Style, rune) string {
if == 0 {
return
}
:= strings.Repeat(string(), abs())
if != nil {
= .Styled()
}
:= strings.Builder{}
:= true
for := range strings.SplitSeq(, "\n") {
if {
= false
} else {
.WriteRune('\n')
}
switch {
case > 0:
.WriteString()
.WriteString()
default:
.WriteString()
.WriteString()
}
}
return .String()
}
func ( int) int {
if < 0 {
return -
}
return
}