Skip to content

Commit

Permalink
refactor: remove Sequence and cleanup
Browse files Browse the repository at this point in the history
Replace Sequence with profile logic checks
  • Loading branch information
aymanbagabas committed Mar 29, 2024
1 parent 886e382 commit bc4b597
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 110 deletions.
4 changes: 2 additions & 2 deletions align.go
Expand Up @@ -9,7 +9,7 @@ import (
// Perform text alignment. If the string is multi-lined, we also make all lines
// the same width by padding them with spaces. If a style is passed, use that
// to style the spaces added.
func alignTextHorizontal(str string, pos Position, width int, style *Sequence) string {
func alignTextHorizontal(str string, pos Position, width int, style *ansi.Style) string {
lines, widestLine := getLines(str)
var b strings.Builder

Expand Down Expand Up @@ -58,7 +58,7 @@ func alignTextHorizontal(str string, pos Position, width int, style *Sequence) s
return b.String()
}

func alignTextVertical(str string, pos Position, height int, _ *Sequence) string {
func alignTextVertical(str string, pos Position, height int, _ *ansi.Style) string {
strHeight := strings.Count(str, "\n") + 1
if height < strHeight {
return str
Expand Down
7 changes: 4 additions & 3 deletions borders.go
Expand Up @@ -406,12 +406,13 @@ func (s Style) styleBorder(border string, fg, bg TerminalColor) string {
return border
}

style := s.r.ColorProfile().Sequence()
p := s.r.ColorProfile()

if fg != noColor {
var style ansi.Style
if fg != noColor && p > Ascii {
style = style.ForegroundColor(fg.color(s.r))
}
if bg != noColor {
if bg != noColor && p > Ascii {
style = style.BackgroundColor(bg.color(s.r))
}

Expand Down
2 changes: 1 addition & 1 deletion env.go
Expand Up @@ -14,7 +14,7 @@ import (
// If NO_COLOR is set, this will return true, ignoring CLICOLOR/CLICOLOR_FORCE
// If CLICOLOR=="0", it will be true only if CLICOLOR_FORCE is also "0" or is unset.
func envNoColor(env map[string]string) bool {
return env["NO_COLOR"] != "" || (env["CLICOLOR"] == "0" && !cliColorForced(env))
return isTrue(env["NO_COLOR"]) || (!isTrue(env["CLICOLOR"]) && !cliColorForced(env))
}

// EnvColorProfile returns the color profile based on environment variables set
Expand Down
75 changes: 0 additions & 75 deletions profile.go
Expand Up @@ -26,81 +26,6 @@ const (
TrueColor
)

// Sequence returns a style sequence for the profile.
func (p Profile) Sequence() Sequence {
return Sequence{Profile: p}
}

// Sequence represents a text ANSI sequence style.
type Sequence struct {
ansi.Style
Profile
}

// Styled returns a styled string.
func (s Sequence) Styled(str string) string {
if s.Profile <= NoTTY {
return str
}
return s.Style.Styled(str)
}

// Bold returns a sequence with bold enabled.
func (s Sequence) Bold() Sequence {
return Sequence{s.Style.Bold(), s.Profile}
}

// Italic returns a sequence with italic enabled.
func (s Sequence) Italic() Sequence {
return Sequence{s.Style.Italic(), s.Profile}
}

// Underline returns a sequence with underline enabled.
func (s Sequence) Underline() Sequence {
return Sequence{s.Style.Underline(), s.Profile}
}

// Strikethrough returns a sequence with strikethrough enabled.
func (s Sequence) Strikethrough() Sequence {
return Sequence{s.Style.Strikethrough(), s.Profile}
}

// Inverse returns a sequence with inverse enabled.
func (s Sequence) Reverse() Sequence {
return Sequence{s.Style.Reverse(), s.Profile}
}

// SlowBlink returns a sequence with slow blink enabled.
func (s Sequence) SlowBlink() Sequence {
return Sequence{s.Style.SlowBlink(), s.Profile}
}

// RapidBlink returns a sequence with rapid blink enabled.
func (s Sequence) RapidBlink() Sequence {
return Sequence{s.Style.RapidBlink(), s.Profile}
}

// Faint returns a sequence with faint enabled.
func (s Sequence) Faint() Sequence {
return Sequence{s.Style.Faint(), s.Profile}
}

// ForegroundColor returns a sequence with the foreground color set.
func (s Sequence) ForegroundColor(c ansi.Color) Sequence {
if s.Profile <= Ascii {
return s
}
return Sequence{s.Style.ForegroundColor(c), s.Profile}
}

// BackgroundColor returns a sequence with the background color set.
func (s Sequence) BackgroundColor(c ansi.Color) Sequence {
if s.Profile <= Ascii {
return s
}
return Sequence{s.Style.BackgroundColor(c), s.Profile}
}

// Convert transforms a given Color to a Color supported within the Profile.
func (p Profile) Convert(c ansi.Color) ansi.Color {
if p <= Ascii {
Expand Down
55 changes: 31 additions & 24 deletions style.go
Expand Up @@ -185,17 +185,17 @@ func (s Style) Render(strs ...string) string {
str = joinString(strs...)

p = s.r.ColorProfile()
te = p.Sequence()
teSpace = p.Sequence()
teWhitespace = p.Sequence()

bold = s.getAsBool(boldKey, false)
italic = s.getAsBool(italicKey, false)
underline = s.getAsBool(underlineKey, false)
strikethrough = s.getAsBool(strikethroughKey, false)
reverse = s.getAsBool(reverseKey, false)
blink = s.getAsBool(blinkKey, false)
faint = s.getAsBool(faintKey, false)
te ansi.Style
teSpace ansi.Style
teWhitespace ansi.Style

bold = s.getAsBool(boldKey, false) && p >= Ascii
italic = s.getAsBool(italicKey, false) && p >= Ascii
underline = s.getAsBool(underlineKey, false) && p >= Ascii
strikethrough = s.getAsBool(strikethroughKey, false) && p >= Ascii
reverse = s.getAsBool(reverseKey, false) && p >= Ascii
blink = s.getAsBool(blinkKey, false) && p >= Ascii
faint = s.getAsBool(faintKey, false) && p >= Ascii

fg = s.getAsColor(foregroundKey)
bg = s.getAsColor(backgroundKey)
Expand Down Expand Up @@ -228,6 +228,12 @@ func (s Style) Render(strs ...string) string {
transform = s.getAsTransform(transformKey)
)

// Disable colors for Ascii and below profiles.
if p <= Ascii {
fg = noColor
bg = noColor
}

if transform != nil {
str = transform(str)
}
Expand Down Expand Up @@ -335,15 +341,15 @@ func (s Style) Render(strs ...string) string {
// Padding
if !inline {

Check failure on line 342 in style.go

View workflow job for this annotation

GitHub Actions / lint-soft

`if !inline` has complex nested blocks (complexity: 8) (nestif)
if leftPadding > 0 {
var st *Sequence
var st *ansi.Style
if colorWhitespace || styleWhitespace {
st = &teWhitespace
}
str = padLeft(str, leftPadding, st)
}

if rightPadding > 0 {
var st *Sequence
var st *ansi.Style
if colorWhitespace || styleWhitespace {
st = &teWhitespace
}
Expand Down Expand Up @@ -371,7 +377,7 @@ func (s Style) Render(strs ...string) string {
numLines := strings.Count(str, "\n")

if !(numLines == 0 && width == 0) {
var st *Sequence
var st *ansi.Style
if colorWhitespace || styleWhitespace {
st = &teWhitespace
}
Expand Down Expand Up @@ -429,48 +435,49 @@ func (s Style) applyMargins(str string, inline bool) string {
bottomMargin = s.getAsInt(marginBottomKey)
leftMargin = s.getAsInt(marginLeftKey)

styler = s.r.ColorProfile().Sequence()
p = s.r.ColorProfile()
style ansi.Style
)

bgc := s.getAsColor(marginBackgroundKey)
if bgc != noColor {
styler = styler.BackgroundColor(bgc.color(s.r))
if bgc != noColor && p > Ascii {
style = style.BackgroundColor(bgc.color(s.r))
}

// Add left and right margin
str = padLeft(str, leftMargin, &styler)
str = padRight(str, rightMargin, &styler)
str = padLeft(str, leftMargin, &style)
str = padRight(str, rightMargin, &style)

// Top/bottom margin
if !inline {
_, width := getLines(str)
spaces := strings.Repeat(" ", width)

if topMargin > 0 {
str = styler.Styled(strings.Repeat(spaces+"\n", topMargin)) + str
str = style.Styled(strings.Repeat(spaces+"\n", topMargin)) + str
}
if bottomMargin > 0 {
str += styler.Styled(strings.Repeat("\n"+spaces, bottomMargin))
str += style.Styled(strings.Repeat("\n"+spaces, bottomMargin))
}
}

return str
}

// Apply left padding.
func padLeft(str string, n int, style *Sequence) string {
func padLeft(str string, n int, style *ansi.Style) string {
return pad(str, -n, style)
}

// Apply right padding.
func padRight(str string, n int, style *Sequence) string {
func padRight(str string, n int, style *ansi.Style) string {
return pad(str, n, style)
}

// pad adds padding to either the left or right side of a string.
// Positive values add to the right side while negative values
// add to the left side.
func pad(str string, n int, style *Sequence) string {
func pad(str string, n int, style *ansi.Style) string {
if n == 0 {
return str
}
Expand Down
13 changes: 8 additions & 5 deletions whitespace.go
Expand Up @@ -10,16 +10,15 @@ import (
type whitespace struct {
re *Renderer
chars string
style Sequence
style ansi.Style
}

// newWhitespace creates a new whitespace renderer. The order of the options
// matters, if you're using WithWhitespaceRenderer, make sure it comes first as
// other options might depend on it.
func newWhitespace(r *Renderer, opts ...WhitespaceOption) *whitespace {
w := &whitespace{
re: r,
style: r.ColorProfile().Sequence(),
re: r,
}
for _, opt := range opts {
opt(w)
Expand Down Expand Up @@ -63,14 +62,18 @@ type WhitespaceOption func(*whitespace)
// WithWhitespaceForeground sets the color of the characters in the whitespace.
func WithWhitespaceForeground(c TerminalColor) WhitespaceOption {
return func(w *whitespace) {
w.style = w.style.ForegroundColor(c.color(w.re))
if w.re.ColorProfile() > Ascii {
w.style = w.style.ForegroundColor(c.color(w.re))
}
}
}

// WithWhitespaceBackground sets the background color of the whitespace.
func WithWhitespaceBackground(c TerminalColor) WhitespaceOption {
return func(w *whitespace) {
w.style = w.style.BackgroundColor(c.color(w.re))
if w.re.ColorProfile() > Ascii {
w.style = w.style.BackgroundColor(c.color(w.re))
}
}
}

Expand Down

0 comments on commit bc4b597

Please sign in to comment.