Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement Capitalization #118

Merged
merged 4 commits into from
Aug 25, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion ansi/baseelement.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package ansi
import (
"bytes"
"io"
"strings"
"text/template"

"github.com/muesli/termenv"
Expand Down Expand Up @@ -38,13 +39,21 @@ func renderText(w io.Writer, p termenv.Profile, rules StylePrimitive, s string)

out := termenv.String(s)

if rules.Upper != nil && *rules.Upper {
out = termenv.String(strings.ToUpper(s))
}
if rules.Lower != nil && *rules.Lower {
out = termenv.String(strings.ToLower(s))
}
if rules.Title != nil && *rules.Title {
out = termenv.String(strings.Title(s))
}
if rules.Color != nil {
out = out.Foreground(p.Color(*rules.Color))
}
if rules.BackgroundColor != nil {
out = out.Background(p.Color(*rules.BackgroundColor))
}

if rules.Underline != nil && *rules.Underline {
out = out.Underline()
}
Expand Down
15 changes: 15 additions & 0 deletions ansi/style.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@ type StylePrimitive struct {
BackgroundColor *string `json:"background_color,omitempty"`
Underline *bool `json:"underline,omitempty"`
Bold *bool `json:"bold,omitempty"`
Upper *bool `json:"upper,omitempty"`
Lower *bool `json:"lower,omitempty"`
Title *bool `json:"title,omitempty"`
Italic *bool `json:"italic,omitempty"`
CrossedOut *bool `json:"crossed_out,omitempty"`
Faint *bool `json:"faint,omitempty"`
Expand Down Expand Up @@ -151,6 +154,9 @@ func cascadeStyle(parent StyleBlock, child StyleBlock, toBlock bool) StyleBlock
s.BackgroundColor = parent.BackgroundColor
s.Underline = parent.Underline
s.Bold = parent.Bold
s.Upper = parent.Upper
s.Title = parent.Title
s.Lower = parent.Lower
s.Italic = parent.Italic
s.CrossedOut = parent.CrossedOut
s.Faint = parent.Faint
Expand Down Expand Up @@ -186,6 +192,15 @@ func cascadeStyle(parent StyleBlock, child StyleBlock, toBlock bool) StyleBlock
if child.Bold != nil {
s.Bold = child.Bold
}
if child.Upper != nil {
s.Upper = child.Upper
}
if child.Lower != nil {
s.Lower = child.Lower
}
if child.Title != nil {
s.Title = child.Title
}
if child.Italic != nil {
s.Italic = child.Italic
}
Expand Down
30 changes: 30 additions & 0 deletions glamour_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -188,3 +188,33 @@ func TestRenderHelpers(t *testing.T) {
string(td), b)
}
}

func TestCapitalization(t *testing.T) {
p := true
style := DarkStyleConfig
style.H1.Upper = &p
style.H2.Title = &p
style.H3.Lower = &p

r, err := NewTermRenderer(
WithStyles(style),
)
if err != nil {
t.Fatal(err)
}

b, err := r.Render("# everything is uppercase\n## everything is titled\n### everything is lowercase")
if err != nil {
t.Fatal(err)
}

// expected outcome
td, err := ioutil.ReadFile("testdata/capitalization.test")
if err != nil {
t.Fatal(err)
}

if string(td) != b {
t.Errorf("Rendered output doesn't match!\nExpected: `\n%s`\nGot: `\n%s`\n", td, b)
}
}
7 changes: 7 additions & 0 deletions testdata/capitalization.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@

  EVERYTHING IS UPPERCASE                                                    
                                                                             
 ## Everything Is Titled                                                     
                                                                             
 ### everything is lowercase