Skip to content

Commit

Permalink
Unit testing for position and whitespace.
Browse files Browse the repository at this point in the history
  • Loading branch information
knz committed Sep 3, 2022
1 parent 4688554 commit ae23be4
Showing 1 changed file with 174 additions and 0 deletions.
174 changes: 174 additions & 0 deletions position_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,174 @@
package lipgloss

import (
"fmt"
"strings"

"github.com/muesli/termenv"
)

func revealNL(o string) string {
// Make newlines visible.
o = strings.ReplaceAll(o, "\n", "␀\n")
o = strings.ReplaceAll(o, " ", "_")
o = strings.ReplaceAll(o, "\t", ">-->")
o = strings.ReplaceAll(o, "\x1b", "^[")
// Add a "no newline at end" marker if there was no newline at the end.
if len(o) == 0 || o[len(o)-1] != '\n' {
o += "πŸ›‡"
}
return o
}

func out(o string) {
fmt.Println(revealNL(o))
fmt.Println()
}

func Example_position() {
fmt.Println("wide:")
out(PlaceHorizontal(3, Left, "hello"))

fmt.Println("horizontal:")
out(PlaceHorizontal(10, Left, "hello"))
out(PlaceHorizontal(10, Right, "hello"))
out(PlaceHorizontal(10, Center, "hello"))

fmt.Println("multiline:")
out(PlaceHorizontal(10, Left, "hello\nworld"))

fmt.Println("tall:")
out(PlaceVertical(1, Top, "hello\nworld"))

fmt.Println("vertical:")
out(PlaceVertical(3, Top, "hello"))
out(PlaceVertical(3, Bottom, "hello"))
out(PlaceVertical(3, Center, "hello"))

fmt.Println("both:")
out(Place(10, 3, Left, Top, "hello"))
out(Place(10, 3, Center, Top, "hello"))
out(Place(10, 3, Right, Top, "hello"))
out(Place(10, 3, Left, Center, "hello"))
out(Place(10, 3, Center, Center, "hello"))
out(Place(10, 3, Right, Center, "hello"))
out(Place(10, 3, Left, Bottom, "hello"))
out(Place(10, 3, Center, Bottom, "hello"))
out(Place(10, 3, Right, Bottom, "hello"))

// Output:
// wide:
// helloπŸ›‡
//
// horizontal:
// hello_____πŸ›‡
//
// _____helloπŸ›‡
//
// __hello___πŸ›‡
//
// multiline:
// hello_____␀
// world_____πŸ›‡
//
// tall:
// hello␀
// worldπŸ›‡
//
// vertical:
// hello␀
// _____␀
// _____πŸ›‡
//
// _____␀
// _____␀
// helloπŸ›‡
//
// _____␀
// hello␀
// _____πŸ›‡
//
// both:
// hello_____␀
// __________␀
// __________πŸ›‡
//
// __hello___␀
// __________␀
// __________πŸ›‡
//
// _____hello␀
// __________␀
// __________πŸ›‡
//
// __________␀
// hello_____␀
// __________πŸ›‡
//
// __________␀
// __hello___␀
// __________πŸ›‡
//
// __________␀
// _____hello␀
// __________πŸ›‡
//
// __________␀
// __________␀
// hello_____πŸ›‡
//
// __________␀
// __________␀
// __hello___πŸ›‡
//
// __________␀
// __________␀
// _____helloπŸ›‡
}

func Example_ws_chars() {
out(Place(10, 3, Center, Center, "hello",
WithWhitespaceChars("."),
))
out(Place(10, 3, Center, Center, "hello",
WithWhitespaceChars("β˜ƒ"),
))
out(Place(10, 3, Center, Center, "hello",
WithWhitespaceChars("12"),
))
out(Place(10, 3, Center, Center, "hello",
WithWhitespaceChars("⏩"),
))

// Output:
// ..........␀
// ..hello...␀
// ..........πŸ›‡
//
//β˜ƒβ˜ƒβ˜ƒβ˜ƒβ˜ƒβ˜ƒβ˜ƒβ˜ƒβ˜ƒβ˜ƒβ€
//β˜ƒβ˜ƒhelloβ˜ƒβ˜ƒβ˜ƒβ€
//β˜ƒβ˜ƒβ˜ƒβ˜ƒβ˜ƒβ˜ƒβ˜ƒβ˜ƒβ˜ƒβ˜ƒπŸ›‡
//
// 1212121212␀
// 12hello121␀
// 1212121212πŸ›‡
//
// ⏩⏩⏩⏩⏩⏩␀
// ⏩hello⏩⏩␀
// β©β©β©β©β©β©πŸ›‡
}

func Example_color() {
// Force color output.
curProfile := ColorProfile()
defer SetColorProfile(curProfile)
SetColorProfile(termenv.ANSI)

out(PlaceHorizontal(10, Left, "hello", WithWhitespaceForeground(Color("1"))))
out(PlaceHorizontal(10, Left, "hello", WithWhitespaceBackground(Color("2"))))

// Output:
// hello^[[31m_____^[[0mπŸ›‡
//
// hello^[[42m_____^[[0mπŸ›‡
}

0 comments on commit ae23be4

Please sign in to comment.