Skip to content

Commit

Permalink
.
Browse files Browse the repository at this point in the history
  • Loading branch information
lihaoyi committed Dec 5, 2021
1 parent 50e6dd1 commit 56a92e6
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 4 deletions.
16 changes: 13 additions & 3 deletions fansi/src/fansi/Fansi.scala
Original file line number Diff line number Diff line change
Expand Up @@ -354,15 +354,25 @@ object Str{
new fansi.Str(chars.clone(), colors.clone())
}

def apply(args: Str*) = {
def apply(args: Str*): fansi.Str = {
join(args)
}
def join(args: TraversableOnce[Str]) = {
val length = args.iterator.map(_.length).sum
def join(args: Iterable[Str], sep: fansi.Str = fansi.Str("")) = {
val length = args.iterator.map(_.length + sep.length).sum - sep.length
val chars = new Array[Char](length)
val colors = new Array[State](length)
var j = 0
for (arg <- args){

if (j != 0){
var k = 0
while (k < sep.length){
chars(j) = sep.getChar(k)
colors(j) = sep.getColors(k)
j += 1
k += 1
}
}
var i = 0
while (i < arg.length){
chars(j) = arg.getChar(i)
Expand Down
25 changes: 24 additions & 1 deletion fansi/test/src/fansi/FansiTests.scala
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,35 @@ object FansiTests extends TestSuite{
val expected = rgbOps ++ RTC ++ rgbOps ++ RTC

assert(concated == expected)

val concated2 = fansi.Str("hello", "world", "i am cow")
val concated3 = fansi.Str("helloworld", "i am cow")
assert(concated2 == concated3)

val applied = fansi.Str("hello")
assert(applied.plainText == "hello")
assert(applied.getColors.forall(_ == 0))
}
test("join"){
val concated = fansi.Str.join(Seq(fansi.Str(rgbOps), fansi.Str(rgbOps))).render
val expected = rgbOps ++ RTC ++ rgbOps ++ RTC

assert(concated == expected)

val concated2 = fansi.Str.join(Seq(fansi.Str(rgbOps), fansi.Str("xyz"))).render
val expected2 = rgbOps ++ RTC ++ "xyz"
assert(concated2 == expected2)

val concated3 = fansi.Str.join(Seq(fansi.Str(rgbOps)), sep = "lol").render
val expected3 = rgbOps ++ RTC
assert(concated3 == expected3)

val concated4 = fansi.Str.join(Seq(fansi.Str(rgbOps), fansi.Str("xyz")), sep = "lol").render
val expected4 = rgbOps ++ RTC ++ "lol" ++ "xyz"
assert(concated4 == expected4)

val concated5 = fansi.Str.join(Seq(fansi.Str(rgbOps), fansi.Str("xyz"), fansi.Str(rgbOps)), sep = "lol").render
val expected5 = rgbOps ++ RTC ++ "lol" ++ "xyz" ++ "lol" ++ rgbOps ++ RTC
assert(concated5 == expected5)
}
test("get"){
val str = fansi.Str(rgbOps)
Expand Down

0 comments on commit 56a92e6

Please sign in to comment.