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

Coverage testing and tests. #109

Open
wants to merge 19 commits into
base: master
Choose a base branch
from
Open
1 change: 1 addition & 0 deletions README.md
Expand Up @@ -6,6 +6,7 @@ Lip Gloss
<a href="https://github.com/charmbracelet/lipgloss/releases"><img src="https://img.shields.io/github/release/charmbracelet/lipgloss.svg" alt="Latest Release"></a>
<a href="https://pkg.go.dev/github.com/charmbracelet/lipgloss?tab=doc"><img src="https://godoc.org/github.com/golang/gddo?status.svg" alt="GoDoc"></a>
<a href="https://github.com/charmbracelet/lipgloss/actions"><img src="https://github.com/charmbracelet/lipgloss/workflows/build/badge.svg" alt="Build Status"></a>
<a href="https://coveralls.io/github/charmbracelet/lipgloss"><img src="https://coveralls.io/repos/github/charmbracelet/lipgloss/badge.svg" alt="Coverage Status"></a>
</p>

Style definitions for nice terminal layouts. Built with TUIs in mind.
Expand Down
58 changes: 56 additions & 2 deletions color_test.go
@@ -1,6 +1,7 @@
package lipgloss

import (
"fmt"
"image/color"
"testing"

Expand Down Expand Up @@ -53,8 +54,6 @@ func TestSetColorProfile(t *testing.T) {
}

func TestHexToColor(t *testing.T) {
t.Parallel()

tt := []struct {
input string
expected uint
Expand All @@ -63,6 +62,10 @@ func TestHexToColor(t *testing.T) {
"#FF0000",
0xFF0000,
},
{
"#ff0000",
0xFF0000,
},
{
"#00F",
0x0000FF,
Expand All @@ -75,6 +78,10 @@ func TestHexToColor(t *testing.T) {
"invalid color",
0x0,
},
{
"",
0x0,
},
}

for i, tc := range tt {
Expand Down Expand Up @@ -272,3 +279,50 @@ func hexToByte(b byte) byte {
// Invalid, but just return 0.
return 0
}

func Example_colors() {
colors := []struct {
name string
c TerminalColor
}{
{"none", NoColor{}},
{"ansi", Color("2")},
{"rgb", Color("#abc")},
{"adapt", AdaptiveColor{"#abc", "#def"}},
}

// We'll change terminal settings, so save the current settings
// to recover them afterwards.
curProfile := ColorProfile()
darkBg := HasDarkBackground()
defer SetColorProfile(curProfile)
defer SetHasDarkBackground(darkBg)

// Force color output.
SetColorProfile(termenv.TrueColor)

for _, b := range []bool{false, true} {
fmt.Println("dark:", b)
SetHasDarkBackground(b)
for _, c := range colors {
r, g, b, a := c.c.RGBA()
fmt.Printf("%s: %v / #%04x-%04x-%04x-%04x\n", c.name, c.c.color(), r, g, b, a)
}
}

// Note: RGBA output for lipgloss.Color("12")
// (ANSI colors) does not work yet.
// See issue: https://github.com/charmbracelet/lipgloss/issues/110

// Output:
// dark: false
// none: <nil> / #0000-0000-0000-ffff
// ansi: #008000 / #0000-8080-0000-ffff
// rgb: #abc / #aaaa-bbbb-cccc-ffff
// adapt: #abc / #aaaa-bbbb-cccc-ffff
// dark: true
// none: <nil> / #0000-0000-0000-ffff
// ansi: #008000 / #0000-8080-0000-ffff
// rgb: #abc / #aaaa-bbbb-cccc-ffff
// adapt: #def / #dddd-eeee-ffff-ffff
}
15 changes: 13 additions & 2 deletions get.go
Expand Up @@ -185,6 +185,11 @@ func (s Style) GetMarginLeft() int {
return s.getAsInt(marginLeftKey)
}

// GetMarginBackground returns the style's margin background color.
func (s Style) GetMarginBackground() TerminalColor {
return s.getAsColor(marginBackgroundKey)
}

// GetHorizontalMargins returns the style's left and right margins. Unset
// values are measured as 0.
func (s Style) GetHorizontalMargins() int {
Expand Down Expand Up @@ -287,16 +292,22 @@ func (s Style) GetBorderLeftBackground() TerminalColor {
return s.getAsColor(borderLeftBackgroundKey)
}

// GetBorderTopWidth returns the width of the top border. If borders contain
// GetBorderTopSize returns the width of the top border. If borders contain
// runes of varying widths, the widest rune is returned. If no border exists on
// the top edge, 0 is returned.
func (s Style) GetBorderTopWidth() int {
func (s Style) GetBorderTopSize() int {
if !s.getAsBool(borderTopKey, false) {
return 0
}
return s.getBorderStyle().GetTopSize()
}

// GetBorderTopWidth is the old name of GetBorderTopSize.
// Preserved for compatibility.
func (s Style) GetBorderTopWidth() int {
return s.GetBorderTopSize()
}

// GetBorderLeftSize returns the width of the left border. If borders contain
// runes of varying widths, the widest rune is returned. If no border exists on
// the left edge, 0 is returned.
Expand Down
2 changes: 2 additions & 0 deletions go.mod
Expand Up @@ -3,6 +3,8 @@ module github.com/charmbracelet/lipgloss
go 1.15

require (
github.com/cockroachdb/datadriven v1.0.2
github.com/knz/lipgloss-convert v0.2.0
github.com/mattn/go-runewidth v0.0.14
github.com/muesli/reflow v0.2.1-0.20210115123740-9e1d0d53df68
github.com/muesli/termenv v0.11.1-0.20220204035834-5ac8409525e0
Expand Down
19 changes: 18 additions & 1 deletion go.sum
@@ -1,6 +1,19 @@
github.com/charmbracelet/lipgloss v0.6.0/go.mod h1:tHh2wr34xcHjC2HCXIlGSG1jaDF0S0atAUvBMP6Ppuk=
github.com/cockroachdb/datadriven v1.0.2 h1:H9MtNqVoVhvd9nCBwOyDjUEdZCREqbIdCJD93PBm/jA=
github.com/cockroachdb/datadriven v1.0.2/go.mod h1:a9RdTaap04u637JoCzcUoIcDmvwSUtcUFtT/C3kJlTU=
github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/knz/lipgloss-convert v0.2.0 h1:i8XqITC65kKPSTslA1XrIUrDvKKwKeJt/NjGk/6RQc0=
github.com/knz/lipgloss-convert v0.2.0/go.mod h1:MMjNmSKywIG34aHj/nf4tJypUJl11VYmvJvL4kFvkRM=
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
github.com/kr/pretty v0.3.0 h1:WgNl7dwNpEZ6jJ9k1snq4pZsg7DOEN8hP9Xw0Tsjwk0=
github.com/kr/pretty v0.3.0/go.mod h1:640gp4NfQd8pI5XOwp5fnNeVWj67G7CFk/SaSQn7NBk=
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
github.com/lucasb-eyer/go-colorful v1.2.0 h1:1nnpGOrhyZZuNyfu1QjKiUICQ74+3FNCN69Aj6K7nkY=
github.com/lucasb-eyer/go-colorful v1.2.0/go.mod h1:R4dSotOR9KMtayYi1e77YzuveK+i7ruzyGqttikkLy0=
github.com/mattn/go-isatty v0.0.14 h1:yVuAays6BHfxijgZPzw+3Zlu5yQgKGP2/hcQbHb7S9Y=
Expand All @@ -18,6 +31,8 @@ github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZN
github.com/rivo/uniseg v0.1.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=
github.com/rivo/uniseg v0.2.0 h1:S1pD9weZBuJdFmowNwbpi7BJ8TNftyUImj/0WQi72jY=
github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=
github.com/rogpeppe/go-internal v1.6.1 h1:/FiVV8dS/e+YqF2JvO3yXRFbBLTIuSDkuC7aBOAvL+k=
github.com/rogpeppe/go-internal v1.6.1/go.mod h1:xXDCJY+GAPziupqXw64V24skbSoqbTEfhy4qGm1nDQc=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
Expand All @@ -27,8 +42,10 @@ github.com/stretchr/testify v1.8.1 h1:w7B6lhMri9wdJUVmEZPGGhZzrYTPvgJArz7wNPgYKs
github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c h1:F1jZWGFhYfh0Ci55sIpILtKKK8p3i2/krTr0H1rg74I=
golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 h1:qIbj1fsPNlZgppZ+VLlY7N33q108Sa+fhmuc+sWQYwY=
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/errgo.v2 v2.1.0/go.mod h1:hNsd1EY+bozCKY1Ytp96fpM3vjJbqLJn88ws8XvfDNI=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
117 changes: 96 additions & 21 deletions join_test.go
@@ -1,24 +1,99 @@
package lipgloss

import "testing"

func TestJoinVertical(t *testing.T) {
type test struct {
name string
result string
expected string
}
tests := []test{
{"por0", JoinVertical(0, "A", "BBBB"), "A \nBBBB"},
{"pos1", JoinVertical(1, "A", "BBBB"), " A\nBBBB"},
{"pos0.25", JoinVertical(0.25, "A", "BBBB"), " A \nBBBB"},
}

for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
if test.result != test.expected {
t.Errorf("Got \n%s\n, expected \n%s\n", test.result, test.expected)
}
})
}
func Example_special_case() {
out(JoinVertical(Left))
out(JoinVertical(Left, ""))
out(JoinVertical(Left, "hello"))
out(JoinHorizontal(Top))
out(JoinHorizontal(Top, ""))
out(JoinHorizontal(Top, "hello"))

// Output:
// 🛇
//
// 🛇
//
// hello🛇
//
// 🛇
//
// 🛇
//
// hello🛇
}

func Example_vertical() {
blockA := "AAA\nAA"
blockB := "BBB\nBBBBB\nBB"
blockC := "C"

out(JoinVertical(Left, blockA, blockB, blockC))
out(JoinVertical(Center, blockA, blockB, blockC))
out(JoinVertical(Right, blockA, blockB, blockC))
out(JoinVertical(0.25, blockA, blockB, blockC))

// Output:
// AAA__␤
// AA___␤
// BBB__␤
// BBBBB␤
// BB___␤
// C____🛇
//
// _AAA_␤
// __AA_␤
// _BBB_␤
// BBBBB␤
// __BB_␤
// __C__🛇
//
// __AAA␤
// ___AA␤
// __BBB␤
// BBBBB␤
// ___BB␤
// ____C🛇
//
// _AAA_␤
// _AA__␤
// _BBB_␤
// BBBBB␤
// _BB__␤
// _C___🛇
}

func Example_horizontal() {
blockA := "AAA\nAA\n\n\n"
blockB := "BBB\nBBBBB\nBB"
blockC := "C"

out(JoinHorizontal(Top, blockA, blockB, blockC))
out(JoinHorizontal(Center, blockA, blockB, blockC))
out(JoinHorizontal(Bottom, blockA, blockB, blockC))
out(JoinHorizontal(0.25, blockA, blockB, blockC))

// Output:
// AAABBB__C␤
// AA_BBBBB_␤
// ___BB____␤
// _________␤
// _________🛇
//
// AAA______␤
// AA_BBB___␤
// ___BBBBBC␤
// ___BB____␤
// _________🛇
//
// AAA______␤
// AA_______␤
// ___BBB___␤
// ___BBBBB_␤
// ___BB___C🛇
//
// AAA______␤
// AA_BBB__C␤
// ___BBBBB_␤
// ___BB____␤
// _________🛇
}